Guest User

Untitled

a guest
Feb 4th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. from paramiko import client
  2. class ssh:
  3. client = None
  4. def __init__(self, address, username, password):
  5. # Let the user know we're connecting to the server
  6. print("Connecting to server.")
  7. # Create a new SSH client
  8. self.client = client.SSHClient()
  9. # 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 file
  10. self.client.set_missing_host_key_policy(client.AutoAddPolicy())
  11. # Make the connection
  12. self.client.connect(address, username=username, password=password, look_for_keys=False)
  13. def sendCommand(self, command):
  14. # Check if connection is made previously
  15. if (self.client):
  16. stdin, stdout, stderr = self.client.exec_command(command)
  17. while not stdout.channel.exit_status_ready():
  18. # Print stdout data when available
  19. if stdout.channel.recv_ready():
  20. # Retrieve the first 1024 bytes
  21. alldata = stdout.channel.recv(1024)
  22. while stdout.channel.recv_ready():
  23. # Retrieve the next 1024 bytes
  24. alldata += stdout.channel.recv(1024)
  25.  
  26. # Print as string with utf8 encoding
  27. print(str(alldata, "utf8"))
  28. else:
  29. print("Connection not opened.")
  30.  
  31. connection = ssh("localhost", "username", "password")
  32. connection.sendCommand("mkdir testputlet")
Add Comment
Please, Sign In to add comment