Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stdin, stdout, stderr = ssh.exec_command("date")
- File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/client.py", line 404, in exec_command
- File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/channel.py", line 60, in _check
- File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/channel.py", line 229, in exec_command
- File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/channel.py", line 1086, in _wait_for_event
- paramiko.ssh_exception.SSHException: Channel closed.
- import paramiko
- import sys, traceback
- import __builtin__
- # Define variable for file inoput errors
- notfound = getattr(__builtin__,"IOError","FileNotFoundError")
- # Define some Variables for SSH Connection
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- # Define a list of credentials for the program to try
- credentials = [['blah', 'blah'], ['blah', 'blah'], ['blah', 'blah']]
- # Define function to bring IP addresses from text file into an array
- def ip_get():
- try:
- ip_array = []
- with open("addresses.txt") as i:
- for line in i:
- ip = line.rstrip('rn')
- ip_array.append(ip)
- except notfound:
- print("n")
- print("Input error occured. Check input text file name and format.")
- print("n")
- sys.exit(0)
- except KeyboardInterrupt:
- print('Interrupted')
- sys.exit(0)
- except:
- print('Unknown Error')
- sys.exit(0)
- return ip_array
- # Define function to SSH to IP address variable
- def ssh_connect(ip):
- for elem in credentials:
- try:
- ssh.connect(ip, username=elem[0],password=elem[1])
- stdin, stdout, stderr = ssh.exec_command("sh switch")
- OUTPUT = stdout.read()
- print(OUTPUT)
- break
- close()
- except paramiko.AuthenticationException:
- print("n")
- print("Connection Failed to " + ip + " using " + elem[0])
- # Call function to get the array of IP addresses
- ip_array = ip_get()
- # Call SSH connection function for each IP address in the array
- for i in range(0, len(ip_array)):
- print('Attempting connection to the following IP addresses:')
- print ip_array[i]
- ssh_connect(ip_array[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement