Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import paramiko
  2. import os
  3. import sys
  4.  
  5. server_list = ['192.168.0.171', '192.168.0.171']
  6. commands = {}
  7.  
  8. class Remote:
  9.  
  10. def __init__(self, ssh_username='', ssh_password=''):
  11. self.ssh = paramiko.SSHClient()
  12. self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  13. self.ssh.load_system_host_keys()
  14. self.username = ssh_username
  15. self.password = ssh_password
  16.  
  17. def connect(self, server_list, cmd, key=False, ssh_keyfile=None):
  18.  
  19. for server in server_list:
  20. if key:
  21. sys.stdout.write('using SSH key')
  22. self.ssh.connect(server, key_filename=ssh_keyfile)
  23. else:
  24. self.ssh.connect(server, username=self.username, password=self.password)
  25. self.commander(server, cmd)
  26.  
  27. def commander(self, server, cmd):
  28. stdin, stdout, stderr = self.ssh.exec_command(cmd)
  29. response = stdout.read()
  30. sys.stdout.write("\n\t========Response from " + server + "========\n")
  31. sys.stdout.write("\n\t Command: " + cmd + "\n")
  32. sys.stdout.write(response)
  33.  
  34. def closure(self, server):
  35. s = server
  36. sys.stdout.write('\n\t !!!!Closing the connection to: %s !!!!\n ') % s
  37. self.ssh.close()
  38.  
  39.  
  40. if __name__ == "__main__":
  41. # username and password here
  42. R = Remote('pi', 'password')
  43. # for each server in the server list | do a ls
  44. R.connect(server_list, "ls", key=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement