Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. from paramiko import client
  2.  
  3. class ssh:
  4. client = None
  5.  
  6. def __init__ (self, server_addr,username, password, keyfile):
  7. print ("Establishing VM connection")
  8.  
  9. #Create ssh client.
  10. self.client = client.SSHClient ()
  11.  
  12. ## The following line is required if you want the script to be able to access a server that's not yet in the known_hosts.
  13. self.client.set_missing_host_key_policy (client.AutoAddPolicy())
  14.  
  15. #Make the connection.
  16. self.client.connect (server_addr, username=username, password=password, key_filename=keyfile);
  17.  
  18. def sendCommand (self, command):
  19. #Check if connection is made successfully.
  20. if (self.client) :
  21. stdin,stdout,stderr = self.client.exec_command (command)
  22. while not stdout.channel.exit_status_ready ():
  23. alldata = stdout.channel.recv (1024)
  24. while stdout.channel.recv_ready ():
  25. alldata += stdout.channel.recv(1024)
  26.  
  27. print (str(alldata))
  28. else:
  29. print "Connection is not opened"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement