Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. if __name__ == '__main__':
  4. import sys
  5. import test_ssh_base
  6. from twisted.internet.task import react
  7. react(test_ssh_base.main, sys.argv[1:])
  8.  
  9. import os, getpass
  10.  
  11. from twisted.python import log
  12. from twisted.python.filepath import FilePath
  13. from twisted.python.usage import Options
  14. from twisted.internet.defer import Deferred
  15. from twisted.internet.protocol import Factory, Protocol
  16. from twisted.internet.endpoints import UNIXClientEndpoint
  17. from twisted.conch.ssh.keys import EncryptedKeyError, Key
  18. from twisted.conch.client.knownhosts import KnownHostsFile
  19. from trigger.twister2 import *
  20.  
  21.  
  22. class EchoOptions(Options):
  23. optParameters = [
  24. ("host", "h", "localhost",
  25. "hostname of the SSH server to which to connect"),
  26. ("port", "p", 22,
  27. "port number of SSH server to which to connect", int),
  28. ("username", "u", getpass.getuser(),
  29. "username with which to authenticate with the SSH server"),
  30. ("identity", "i", None,
  31. "file from which to read a private key to use for authentication"),
  32. ("password", None, None,
  33. "password to use for authentication"),
  34. ("knownhosts", "k", "~/.ssh/known_hosts",
  35. "file containing known ssh server public key data"),
  36. ]
  37.  
  38. optFlags = [
  39. ["no-agent", None, "Disable use of key agent"],
  40. ]
  41.  
  42.  
  43.  
  44. class NoiseProtocol(Protocol):
  45. def connectionMade(self):
  46. self.finished = Deferred()
  47. self.strings = [b'show version | in uptime']
  48. self.sendNoise()
  49.  
  50.  
  51. def sendNoise(self):
  52. if self.strings:
  53. self.transport.write(self.strings.pop(0) + "\n")
  54. else:
  55. self.transport.loseConnection()
  56.  
  57.  
  58. def dataReceived(self, data):
  59. print "Server says:", data
  60. self.sendNoise()
  61.  
  62.  
  63. def connectionLost(self, reason):
  64. self.finished.callback(None)
  65.  
  66.  
  67.  
  68. def readKey(path):
  69. try:
  70. return Key.fromFile(path)
  71. except EncryptedKeyError:
  72. passphrase = getpass.getpass("%r keyphrase: " % (path,))
  73. return Key.fromFile(path, passphrase=passphrase)
  74.  
  75.  
  76.  
  77. class ConnectionParameters(object):
  78. def __init__(self, reactor, host, port, username, password,
  79. keys, knownHosts, agent):
  80. self.reactor = reactor
  81. self.host = host
  82. self.port = port
  83. self.username = username
  84. self.password = password
  85. self.keys = keys
  86. self.knownHosts = knownHosts
  87. self.agent = agent
  88.  
  89.  
  90. @classmethod
  91. def fromCommandLine(cls, reactor, argv):
  92. config = EchoOptions()
  93. config.parseOptions(argv)
  94.  
  95. keys = []
  96. if config["identity"]:
  97. keyPath = os.path.expanduser(config["identity"])
  98. if os.path.exists(keyPath):
  99. keys.append(readKey(keyPath))
  100.  
  101. knownHostsPath = FilePath(os.path.expanduser(config["knownhosts"]))
  102. if knownHostsPath.exists():
  103. knownHosts = KnownHostsFile.fromPath(knownHostsPath)
  104. else:
  105. knownHosts = None
  106.  
  107. if config["no-agent"] or "SSH_AUTH_SOCK" not in os.environ:
  108. agentEndpoint = None
  109. else:
  110. agentEndpoint = UNIXClientEndpoint(
  111. reactor, os.environ["SSH_AUTH_SOCK"])
  112.  
  113. return cls(
  114. reactor, config["host"], config["port"],
  115. config["username"], config["password"], keys,
  116. knownHosts, agentEndpoint)
  117.  
  118.  
  119. def getEndpoint(self):
  120. return TriggerSSHShellClientEndpointBase.newConnection(
  121. self.reactor, self.username, self.host, port=self.port,
  122. keys=self.keys, password=self.password, agentEndpoint=self.agent,
  123. knownHosts=self.knownHosts,
  124. )
  125.  
  126.  
  127.  
  128. def main(reactor, *argv):
  129. log.startLogging(sys.stdout, setStdout=0)
  130. parameters = ConnectionParameters.fromCommandLine(reactor, argv)
  131. endpoint = parameters.getEndpoint()
  132.  
  133.  
  134. factory = Factory()
  135. # factory = Factory.forProtocol(IoslikeSendExpect,
  136. # 'r1.demo.local',
  137. # [b'term leng 0', b'show version'],
  138. # )
  139. factory.protocol = IoslikeSendExpect
  140. # factory = TriggerSSHEndPointFactory
  141.  
  142. d = endpoint.connect(factory, [b'term leng 0', b'show version'])
  143. d.addCallback(lambda proto: proto.finished)
  144. return d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement