Advertisement
Guest User

Untitled

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