Guest User

Untitled

a guest
Jun 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. from twisted.conch import error
  2. from twisted.conch.ssh import transport, connection, keys, userauth, channel, common
  3. from twisted.internet import defer, protocol, reactor
  4.  
  5. class ClientCommandTransport(transport.SSHClientTransport):
  6. def __init__(self, username, password, command):
  7. self.username = username
  8. self.password = password
  9. self.command = command
  10.  
  11. def verifyHostKey(self, pubKey, fingerprint):
  12. # in a real app, you should verify that the fingerprint matches
  13. # the one you expected to get from this server
  14. return defer.succeed(True)
  15.  
  16. def connectionSecure(self):
  17. self.requestService(
  18. PasswordAuth(self.username, self.password,
  19. ClientConnection(self.command)))
  20.  
  21. class PasswordAuth(userauth.SSHUserAuthClient):
  22. def __init__(self, user, password, connection):
  23. userauth.SSHUserAuthClient.__init__(self, user, connection)
  24. self.password = password
  25.  
  26. def getPassword(self, prompt=None):
  27. return defer.succeed(self.password)
  28.  
  29. class ClientConnection(connection.SSHConnection):
  30. def __init__(self, cmd, *args, **kwargs):
  31. connection.SSHConnection.__init__(self)
  32. self.command = cmd
  33.  
  34. def serviceStarted(self):
  35. self.openChannel(CommandChannel(self.command, conn=self))
  36.  
  37. class CommandChannel(channel.SSHChannel):
  38. name = 'session'
  39.  
  40. def __init__(self, command, *args, **kwargs):
  41. channel.SSHChannel.__init__(self, *args, **kwargs)
  42. self.command = command
  43.  
  44. def channelOpen(self, data):
  45. self.conn.sendRequest(
  46. self, 'exec', common.NS(self.command), wantReply=True).addCallback(
  47. self._gotResponse)
  48.  
  49. def _gotResponse(self, _):
  50. self.conn.sendEOF(self)
  51.  
  52. def dataReceived(self, data):
  53. print data
  54.  
  55. def closed(self):
  56. reactor.stop()
  57.  
  58. class ClientCommandFactory(protocol.ClientFactory):
  59. def __init__(self, username, password, command):
  60. self.username = username
  61. self.password = password
  62. self.command = command
  63.  
  64. def buildProtocol(self, addr):
  65. protocol = ClientCommandTransport(
  66. self.username, self.password, self.command)
  67. return protocol
  68.  
  69. if __name__ == "__main__":
  70. import sys, getpass
  71. server = sys.argv[1]
  72. command = sys.argv[2]
  73. username = raw_input("Username: ")
  74. password = getpass.getpass("Password: ")
  75. factory = ClientCommandFactory(username, password, command)
  76. reactor.connectTCP(server, 22, factory)
  77. reactor.run()
Add Comment
Please, Sign In to add comment