Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. stdin, stdout, stderr = ssh.exec_command("date")
  2. File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/client.py", line 404, in exec_command
  3. File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/channel.py", line 60, in _check
  4. File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/channel.py", line 229, in exec_command
  5. File "/usr/lib/python2.7/site-packages/paramiko-1.16.0-py2.7.egg/paramiko/channel.py", line 1086, in _wait_for_event
  6. paramiko.ssh_exception.SSHException: Channel closed.
  7.  
  8. import paramiko
  9. import sys, traceback
  10. import __builtin__
  11.  
  12. # Define variable for file inoput errors
  13. notfound = getattr(__builtin__,"IOError","FileNotFoundError")
  14.  
  15. # Define some Variables for SSH Connection
  16. ssh = paramiko.SSHClient()
  17. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  18.  
  19. # Define a list of credentials for the program to try
  20. credentials = [['blah', 'blah'], ['blah', 'blah'], ['blah', 'blah']]
  21.  
  22. # Define function to bring IP addresses from text file into an array
  23. def ip_get():
  24. try:
  25. ip_array = []
  26. with open("addresses.txt") as i:
  27. for line in i:
  28. ip = line.rstrip('rn')
  29. ip_array.append(ip)
  30. except notfound:
  31. print("n")
  32. print("Input error occured. Check input text file name and format.")
  33. print("n")
  34. sys.exit(0)
  35. except KeyboardInterrupt:
  36. print('Interrupted')
  37. sys.exit(0)
  38. except:
  39. print('Unknown Error')
  40. sys.exit(0)
  41. return ip_array
  42.  
  43. # Define function to SSH to IP address variable
  44. def ssh_connect(ip):
  45. for elem in credentials:
  46. try:
  47. ssh.connect(ip, username=elem[0],password=elem[1])
  48. stdin, stdout, stderr = ssh.exec_command("sh switch")
  49. OUTPUT = stdout.read()
  50. print(OUTPUT)
  51. break
  52. close()
  53. except paramiko.AuthenticationException:
  54. print("n")
  55. print("Connection Failed to " + ip + " using " + elem[0])
  56.  
  57. # Call function to get the array of IP addresses
  58. ip_array = ip_get()
  59.  
  60. # Call SSH connection function for each IP address in the array
  61. for i in range(0, len(ip_array)):
  62. print('Attempting connection to the following IP addresses:')
  63. print ip_array[i]
  64. ssh_connect(ip_array[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement