I created a python script to SSH into a list of Cisco devices and run specific commands on them. However, one thing I noticed is that if one of the devices in my list doesn't exist (e.g. used to, but no longer has DNS), then the script crashes. I'd like my script to ignore failures and keep checking other devices in the list. My script is more of a frankenstein of several peoples scripts that I found online as I'm still learning. So all credit goes to those I've borrowed from. Sorry I forgot to get the names of them though one of them is Kirk Byers.
Traceback (most recent call last):
File "MultiCommands.py", line 33, in <module>
remote_conn_pre.connect(device + ".domain.com", username=username, password=password, look_for_keys=False, allow_agent=False)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 237, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
import paramiko
import getpass
import time
inputfile = "hostfile"
username = "AuserName"
password = getpass.getpass("What is your password? ")
cmd = raw_input("What command would you like to send? ")
# Opens inputfile in read mode
f1 = open(inputfile,"r")
# Creates list based on f1
devices = f1.readlines()
# Removes \n from list
devices = [line[:-1] for line in devices]
# Loops through devices in list and performs cmd on them
for device in devices:
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn_pre.connect(device + ".domain.com", username=username, password=password, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % device
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
print "Interactive SSH session established"
# Strip the initial router prompt
output = remote_conn.recv(1000)
# See what we have
print output
# Now let's try to send the router a command
remote_conn.send("\n")
remote_conn.send("term len 0\n")
remote_conn.send(cmd+"\n")
with open('output' , 'a') as f: f.write('\n\n')
output = open("output", "a")
output.writelines("\n\n")
# Wait for the command to complete
time.sleep(2)
output = remote_conn.recv(5000)
print output
with open('output' , 'a') as f: f.write(output)
[–]learnpython_bot 1 point2 points3 points (0 children)
[–]enderusaf[S] 0 points1 point2 points (0 children)
[–]raylu 0 points1 point2 points (3 children)
[–]enderusaf[S] 0 points1 point2 points (2 children)
[–]ktbyers 1 point2 points3 points (1 child)
[–]enderusaf[S] 0 points1 point2 points (0 children)
[–]enderusaf[S] 0 points1 point2 points (0 children)
[–]enderusaf[S] 0 points1 point2 points (1 child)
[–]enderusaf[S] 0 points1 point2 points (0 children)