Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #!/usr/local/bin/python3.5
  2. import cmd
  3. import getpass
  4. import socket
  5. import time
  6. import logging
  7. import paramiko
  8.  
  9. def ssh_check():
  10. with open('switch.txt', 'r') as file:
  11. IPs = file.readlines()
  12. for IP in IPs:
  13. try:
  14. ssh = paramiko.SSHClient()
  15. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  16. ssh.connect(IP.strip(), username=User, password=Password)
  17. except paramiko.SSHException as e:
  18. print("{} Password is invalid:{}".format(IP.strip(), e))
  19. IPs.remove(IP)
  20. except paramiko.AuthenticationException:
  21. print("{} Authentication failed for some reason".format(IP.strip()))
  22. IPs.remove(IP)
  23. except socket.error as e:
  24. print("{} Socket connection failed: {}".format(IP.strip(), e))
  25. IPs.remove(IP)
  26. return IPs
  27.  
  28.  
  29. class command(cmd.Cmd):
  30. prompt = 'ssh >'
  31.  
  32. def do_run(self, command):
  33. hosts = ssh_check()
  34. for host in hosts:
  35. ssh = paramiko.SSHClient()
  36. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  37. ssh.connect(host.strip(), username=User, password=Password)
  38. logging.basicConfig(filename="sample.log", level=logging.DEBUG)
  39. channel_conn = ssh.invoke_shell()
  40. output = channel_conn.recv(1024)
  41. channel_conn.send(command + '\n')
  42. while not channel_conn.recv_ready():
  43. time.sleep(2)
  44. output = channel_conn.recv(4098)
  45. print('{} {}\n'.format(host.strip(), output.decode()))
  46. ssh.close()
  47.  
  48. def do_bye(self, arg):
  49. print('bye')
  50. return True
  51.  
  52. if __name__ == "__main__":
  53. User = input('Enter your username:')
  54. Password = getpass.getpass()
  55. command().cmdloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement