Guest User

Untitled

a guest
Sep 11th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. How can i connect to server through ssh tunnel in python
  2. import paramiko
  3. ssh = paramiko.SSHClient()
  4.  
  5. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  6.  
  7. ssh.connect('<hostname>', username='<username>', password='<password>', key_filename='<path/to/openssh-private-key-file>')
  8.  
  9. stdin, stdout, stderr = ssh.exec_command('ls')
  10. print stdout.readlines()
  11. ssh.close()
  12.  
  13. from twisted.internet.protocol import ClientFactory
  14. from twisted.protocols.basic import LineReceiver
  15. from twisted.internet import reactor
  16. import sys
  17.  
  18. class EchoClient(LineReceiver):
  19. end="Bye-bye!"
  20. def connectionMade(self):
  21. self.sendLine("Hello, world!")
  22. self.sendLine("What a fine day it is.")
  23. self.sendLine(self.end)
  24.  
  25. def lineReceived(self, line):
  26. print "receive:", line
  27. if line==self.end:
  28. self.transport.loseConnection()
  29.  
  30. class EchoClientFactory(ClientFactory):
  31. protocol = EchoClient
  32.  
  33. def clientConnectionFailed(self, connector, reason):
  34. print 'connection failed:', reason.getErrorMessage()
  35. reactor.stop()
  36.  
  37. def clientConnectionLost(self, connector, reason):
  38. print 'connection lost:', reason.getErrorMessage()
  39. reactor.stop()
  40.  
  41. def main():
  42. factory = EchoClientFactory()
  43. reactor.connectTCP('localhost', 8000, factory)
  44. reactor.run()
  45.  
  46. if __name__ == '__main__':
  47. main()
Add Comment
Please, Sign In to add comment