Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Copyright (C) 2003-2007 Robey Pointer <robey@lag.net>
  4. #
  5. # This file is part of paramiko.
  6. #
  7. # Paramiko is free software; you can redistribute it and/or modify it under the
  8. # terms of the GNU Lesser General Public License as published by the Free
  9. # Software Foundation; either version 2.1 of the License, or (at your option)
  10. # any later version.
  11. #
  12. # Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
  13. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  15. # details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
  19. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20.  
  21. """
  22. Sample script showing how to do local port forwarding over paramiko.
  23.  
  24. This script connects to the requested SSH server and sets up local port
  25. forwarding (the openssh -L option) from a local port through a tunneled
  26. connection to a destination reachable from the SSH server machine.
  27. """
  28.  
  29. import getpass
  30. import os
  31. import socket
  32. import select
  33. import SocketServer
  34. import sys
  35. from optparse import OptionParser
  36.  
  37. import paramiko
  38.  
  39. SSH_PORT = 22
  40. DEFAULT_PORT = 4000
  41.  
  42. g_verbose = True
  43.  
  44.  
  45. class ForwardServer (SocketServer.ThreadingTCPServer):
  46. daemon_threads = True
  47. allow_reuse_address = True
  48.  
  49.  
  50. class Handler (SocketServer.BaseRequestHandler):
  51.  
  52. def handle(self):
  53. try:
  54. chan = self.ssh_transport.open_channel('direct-tcpip',
  55. (self.chain_host, self.chain_port),
  56. self.request.getpeername())
  57. except Exception, e:
  58. verbose('Incoming request to %s:%d failed: %s' % (self.chain_host,
  59. self.chain_port,
  60. repr(e)))
  61. return
  62. if chan is None:
  63. verbose('Incoming request to %s:%d was rejected by the SSH server.' %
  64. (self.chain_host, self.chain_port))
  65. return
  66.  
  67. verbose('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(),
  68. chan.getpeername(), (self.chain_host, self.chain_port)))
  69. while True:
  70. r, w, x = select.select([self.request, chan], [], [])
  71. if self.request in r:
  72. data = self.request.recv(1024)
  73. if len(data) == 0:
  74. break
  75. chan.send(data)
  76. if chan in r:
  77. data = chan.recv(1024)
  78. if len(data) == 0:
  79. break
  80. self.request.send(data)
  81. chan.close()
  82. self.request.close()
  83. verbose('Tunnel closed from %r' % (self.request.getpeername(),))
  84.  
  85.  
  86. def forward_tunnel(local_port, remote_host, remote_port, transport):
  87. # this is a little convoluted, but lets me configure things for the Handler
  88. # object. (SocketServer doesn't give Handlers any way to access the outer
  89. # server normally.)
  90. class SubHander (Handler):
  91. chain_host = remote_host
  92. chain_port = remote_port
  93. ssh_transport = transport
  94. ForwardServer(('', local_port), SubHander).serve_forever()
  95.  
  96.  
  97. def verbose(s):
  98. if g_verbose:
  99. print s
  100.  
  101.  
  102. HELP = """\
  103. Set up a forward tunnel across an SSH server, using paramiko. A local port
  104. (given with -p) is forwarded across an SSH session to an address:port from
  105. the SSH server. This is similar to the openssh -L option.
  106. """
  107.  
  108.  
  109. def get_host_port(spec, default_port):
  110. "parse 'hostname:22' into a host and port, with the port optional"
  111. args = (spec.split(':', 1) + [default_port])[:2]
  112. args[1] = int(args[1])
  113. return args[0], args[1]
  114.  
  115.  
  116. def parse_options():
  117. global g_verbose
  118.  
  119. parser = OptionParser(usage='usage: %prog [options] <ssh-server>[:<server-port>]',
  120. version='%prog 1.0', description=HELP)
  121. parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
  122. help='squelch all informational output')
  123. parser.add_option('-p', '--local-port', action='store', type='int', dest='port',
  124. default=DEFAULT_PORT,
  125. help='local port to forward (default: %d)' % DEFAULT_PORT)
  126. parser.add_option('-u', '--user', action='store', type='string', dest='user',
  127. default=getpass.getuser(),
  128. help='username for SSH authentication (default: %s)' % getpass.getuser())
  129. parser.add_option('-K', '--key', action='store', type='string', dest='keyfile',
  130. default=None,
  131. help='private key file to use for SSH authentication')
  132. parser.add_option('', '--no-key', action='store_false', dest='look_for_keys', default=True,
  133. help='don\'t look for or use a private key file')
  134. parser.add_option('-P', '--password', action='store_true', dest='readpass', default=False,
  135. help='read password (for key or password auth) from stdin')
  136. parser.add_option('-r', '--remote', action='store', type='string', dest='remote', default=None, metavar='host:port',
  137. help='remote host and port to forward to')
  138. options, args = parser.parse_args()
  139.  
  140. if len(args) != 1:
  141. parser.error('Incorrect number of arguments.')
  142. if options.remote is None:
  143. parser.error('Remote address required (-r).')
  144.  
  145. g_verbose = options.verbose
  146. server_host, server_port = get_host_port(args[0], SSH_PORT)
  147. remote_host, remote_port = get_host_port(options.remote, SSH_PORT)
  148. return options, (server_host, server_port), (remote_host, remote_port)
  149.  
  150.  
  151. def main():
  152. options, server, remote = parse_options()
  153.  
  154. password = None
  155. if options.readpass:
  156. password = getpass.getpass('Enter SSH password: ')
  157.  
  158. client = paramiko.SSHClient()
  159. client.load_system_host_keys()
  160. client.set_missing_host_key_policy(paramiko.WarningPolicy())
  161.  
  162. verbose('Connecting to ssh host %s:%d ...' % (server[0], server[1]))
  163. try:
  164. client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile,
  165. look_for_keys=options.look_for_keys, password=password)
  166. except Exception, e:
  167. print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)
  168. sys.exit(1)
  169.  
  170. verbose('Now forwarding port %d to %s:%d ...' % (options.port, remote[0], remote[1]))
  171.  
  172. try:
  173. forward_tunnel(options.port, remote[0], remote[1], client.get_transport())
  174. except KeyboardInterrupt:
  175. print 'C-c: Port forwarding stopped.'
  176. sys.exit(0)
  177.  
  178.  
  179. if __name__ == '__main__':
  180. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement