PageRenderTime 106ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/PuttyKnife/dist/connect.py

http://puttyknife.googlecode.com/
Python | 218 lines | 187 code | 8 blank | 23 comment | 21 complexity | a48763fa03027414774de507b8e0dd95 MD5 | raw file
Possible License(s): GPL-3.0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vi: ts=4 sw=4
  4. #-------------------------------------------------------------------------------
  5. # Program: PuttyKnife VPN - A PuTTY/ssh-based virtual private networking client
  6. # Module: connection supervisor
  7. # Desc: Stylus Toolbox is a GTK+ GUI front-end for Gutenprint's
  8. # escputil printer utility.
  9. # Author: Rob A. Shinn
  10. #
  11. # Legal: Copyright 2008 Rob A. Shinn. All rights reserved.
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #-------------------------------------------------------------------------------
  22. import sys
  23. import os
  24. import unittest
  25. #from pexpect import *
  26. from configuration import *
  27. from puttysession import *
  28. class Tunnel():
  29. def __init__(self,
  30. listen_addr=None,
  31. listen_port=None,
  32. host_addr=None,
  33. host_port=None,
  34. remote=False):
  35. self.listen_addr=listen_addr
  36. self.listen_port=listen_port
  37. self.host_addr=host_addr
  38. self.host_port=host_port
  39. self.remote=remote
  40. def __repr__(self):
  41. return "%s:%s -> %s:%s (%s)" % (self.listen_addr, self.listen_port,
  42. self.host_addr, self.host_port,
  43. self.remote)
  44. class Tunnels():
  45. def __init__(self):
  46. self.forwarders=[]
  47. def add_tunnel(self, tunnel):
  48. self.forwarders.append(tunnel)
  49. def get_list(self):
  50. return self.forwarders
  51. def __iter__(self):
  52. self.iter=self.forwarders.__iter__()
  53. return self
  54. def next(self):
  55. row = self.iter.next()
  56. return row
  57. class Connection():
  58. def __init__(self,
  59. host=None,
  60. port=None,
  61. username=None,
  62. password=None,
  63. keyfile=None,
  64. tunnels=None,
  65. ip_version=None,
  66. compression=False,
  67. verbose=False,
  68. x11=False,
  69. agent=False,
  70. useagent=True,
  71. shell=True,
  72. socks_port=None,
  73. ssh_version=None,
  74. pty=True,
  75. logfile=None,
  76. proxy_dns=1,
  77. proxy_host=None,
  78. proxy_port=None,
  79. proxy_method=None,
  80. proxy_username=None,
  81. proxy_password=None,
  82. proxy_command=None,
  83. ):
  84. self.command_line=None
  85. self.host=host
  86. self.port=port
  87. self.username=username
  88. self.password=password
  89. self.keyfile=keyfile
  90. self.tunnels=tunnels
  91. self.ip_version=ip_version
  92. self.ssh_version=ssh_version
  93. self.compression=compression
  94. self.verbose=verbose
  95. self.socks_port=socks_port
  96. self.x11=x11
  97. self.agent=agent
  98. self.pty=pty
  99. self.shell=shell
  100. self.useagent=useagent
  101. self.logfile=logfile
  102. self.proxy_host=proxy_host
  103. self.proxy_method=proxy_method
  104. self.proxy_port=proxy_port
  105. self.proxy_username=proxy_username
  106. self.proxy_password=proxy_password
  107. self.proxy_command=proxy_command
  108. self.proxy_dns=proxy_dns
  109. #self.create_putty_session('PuttyKnife_VPN_Session')
  110. def create_putty_session(self, filename):
  111. psf=putty_session(filename=filename,
  112. proxy_method=self.proxy_method,
  113. proxy_host=self.proxy_host,
  114. proxy_port=self.proxy_port,
  115. proxy_username=self.proxy_username,
  116. proxy_password=self.proxy_password,
  117. proxy_command=self.proxy_command,
  118. proxy_dns=self.proxy_dns)
  119. psf.optionxform=str
  120. psf.write()
  121. def write_log(self, msg):
  122. if self.logfile:
  123. self.logfile.write(msg)
  124. def __log_filter(self, msg):
  125. if self.logfile:
  126. self.__write_log(self, msg)
  127. return msg
  128. def open(self):
  129. switches='-v'
  130. if self.keyfile:
  131. switches='%s -i %s' % (switches,self.keyfile)
  132. if self.port:
  133. switches='%s -P %s' % (switches,self.port)
  134. if self.username:
  135. switches='%s -l %s' % (switches,self.username)
  136. if self.ip_version:
  137. switches='%s -%s' % (switches,self.ip_version)
  138. if self.compression:
  139. switches='%s -C' % switches
  140. if self.socks_port:
  141. switches='%s -D %s' % (switches,self.socks_port)
  142. if self.x11:
  143. switches='%s -X' % switches
  144. if self.agent:
  145. switches='%s -agent' % switches
  146. else:
  147. switches='%s -noagent' % switches
  148. if not self.shell:
  149. switches='%s -N' % switches
  150. if not self.pty:
  151. switches='%s -T' % switches
  152. for tunnel in self.tunnels:
  153. if tunnel.remote:
  154. switches='%s -R ' % switches
  155. else:
  156. switches='%s -L ' % switches
  157. if tunnel.listen_addr:
  158. switches='%s%s:' % (switches,tunnel.listen_addr)
  159. switches='%s%s:' % (switches,tunnel.listen_port)
  160. switches='%s%s:' % (switches,tunnel.host_addr)
  161. switches='%s%s' % (switches,tunnel.host_port)
  162. if self.host:
  163. command_line='plink %s %s' % (switches, self.host)
  164. else:
  165. sys.quit()
  166. child=spawn(command_line)
  167. self.write_log('Launching command: %s' % command_line)
  168. if self.password:
  169. child.expect('password:')
  170. child.send(self.password)
  171. else:
  172. child.expect('Opened channel for session')
  173. self.write_log(child.before)
  174. msg=''
  175. while child.isalive():
  176. try:
  177. ch=child.read(size=1)
  178. except TIMEOUT:
  179. pass
  180. if ch=='\n':
  181. self.write_log(msg)
  182. msg=''
  183. else:
  184. msg=msg+ch
  185. class __unittest__(unittest.TestCase):
  186. def setUp(self):
  187. connection=Connection()
  188. connection.host='brighid'
  189. connection.keyfile='/home/morgan/.ssh/id_rsa.ppk'
  190. connection.agent=False
  191. connection.logfile=logfile
  192. tunnel=Tunnel(listen_addr='127.0.0.1', listen_port=2222,
  193. host_addr='192.168.1.101', host_port=22, remote=False)
  194. tunnels=Tunnels()
  195. tunnels.add_tunnel(tunnel)
  196. tunnel=Tunnel(listen_addr='127.0.0.1', listen_port=9010,
  197. host_addr='192.168.1.101', host_port=901, remote=False)
  198. tunnels.add_tunnel(tunnel)
  199. connection.tunnels=tunnels
  200. self.connection=connection
  201. def runTest(self):
  202. self.connection.open()
  203. if __name__ == '__main__':
  204. from logger import *
  205. unittest.main()