If you get an error like
import webexteamssdk
ModuleNotFoundError: No module named 'webexteamssdk'
We need in install that module. You'll often see the advice :
pip install webexteamssdk
This didn't work for me. I had to use:
python -m pip install webexteamssdk
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Friday, 17 January 2020
Wednesday, 10 July 2013
python script to test VPN connectivity opens socket to IP addresses and ports
Quick script I wrote to test VPN connections. It reads in a list (data.csv) of IP's and ports and attempts to open a socket to them, then reports the result.
#!/usr/bin/env python
# Import some needed modules
import socket
import re
import sys
import csv
# Function that will take input of a customer name an ip address and a port number
# It will attempt to open a socket to that IP/port and report on success or failure
def test_connection(customer, address, port):
# Create a TCP socket
s = socket.socket()
# Set the socket timeout to 10 seconds
s.settimeout(15)
msg1 = "Attempting to connect to customer %s on IP %s and port %s" % (customer, address, port)
print msg1
try:
s.connect((address, port))
msg2 = "Connection to customer [ %s ] on IP [ %s ] and port [ %s ] was [ OK ]" % (customer, address, port)
pl = print_and_log_this(msg2)
# If we can connect return True
return True
except socket.error, e:
# If we can't connect and get an error return False
msg3 = "Connection to customer [ %s ] on IP [ %s ] and port [ %s ] has [ FAILED ] with error [ %s ]" % (customer, address, port, e)
pl = print_and_log_this(msg3)
return False
s.close()
# Function to print a message on screen and append it to a log file
def print_and_log_this (message):
print message
logfile = open("testipandportlog.txt", "a")
logentry = message + "\n"
logfile.write(logentry)
logfile.close()
# Main fucntion of the program (program starts here)
if __name__ == '__main__':
# Read in lines from the csv file, each line should have the following information
# Customer-name,IP-address,Port-number
# For each line in the CSV use the fuction created above to check connectivity
data = open("data.csv", "rb")
reader = csv.reader(data)
for line in reader:
c = line[0]
a = line[1]
p = eval(line[2]) # convert string to int
check = test_connection(c, a, p)
# Close our handle on the file
data.close()
#exit()
Wednesday, 20 June 2012
python script to connect to linux servers and run a command
This is a sloppy script I wrote to get a job done. The job was to collect the host name, list of users and the wheel group from a list of linux servers. It could be easily edited to run other commands. You need to have your password set the same on each server. My username is hard coded. You also need to have a list of servers in /data/servers.txt
import os
# Read servers in from file
f = file("./data/servers.txt","r")
servers = []
servers = f.readlines()
# set up commands to use later
cmd1 = 'hostname'
cmd2 = 'cat /etc/passwd | sort'
cmd3 = 'grep wheel /etc/group'
sshcmd = 'ssh -p 22 user.name@'
# Loop through the list of servers, connect to each and run the commands that were setup above
for x in servers:
x = x.rstrip()
one = sshcmd + x + " " + cmd1 + " >> " + x + ".txt"
runone = os.system(one)
print runone
two = sshcmd + x + " " + cmd2 + " | cut -d: -f 1,3,6 | grep \"[5-9][0-9][0-9]\" | cut -d: -f1 >> " + x + ".txt"
print two
runtwo = os.system(two)
print runtwo
three = sshcmd + x + " " + cmd3 + " >> "+x+".txt"
runthree = os.system(three)
print runthree
An alternative to this would be to use cssh, but that was not available to me.
Subscribe to:
Posts (Atom)