Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. MyLocalMachine ----||----> MiddleMachine --(netcat)--> AnotherMachine
  2. ('localhost') (firewall) ('1.1.1.1') ('2.2.2.2')
  3.  
  4. cli = paramiko.SSHClient()
  5. cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  6. proxy = paramiko.ProxyCommand('ssh user@1.1.1.1 nc 2.2.2.2 22')
  7. cli.connect(hostname='2.2.2.2', username='user', password='pass', sock=proxy)
  8.  
  9. import paramiko
  10.  
  11.  
  12. class ParaProxy(paramiko.util.ClosingContextManager):
  13. def __init__(self, stdin, stdout, stderr):
  14. self.stdin = stdin
  15. self.stdout = stdout
  16. self.stderr = stderr
  17.  
  18. def send(self, content):
  19. try:
  20. self.stdin.write(content)
  21. except IOError as exc:
  22. print('IOError exception.')
  23. return
  24. return len(content)
  25.  
  26. def recv(self, size):
  27. buffer = b''
  28. while len(buffer) < size:
  29. buffer += self.stdout.read(size - len(buffer))
  30. return buffer
  31.  
  32. def close(self):
  33. self.stdin.close()
  34. self.stdout.close()
  35. self.stderr.close()
  36.  
  37. def settimeout(self, timeout):
  38. print('Attempt to set timeout to {} - ignoring.'.format(timeout))
  39.  
  40. # Connecting to MiddleMachine and executing netcat
  41. mid_cli = paramiko.SSHClient()
  42. mid_cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  43. mid_cli.connect(hostname='1.1.1.1', username='user', password='pass')
  44. io_tupple = mid_cli.exec_command('nc 2.2.2.2 22')
  45.  
  46. # Instantiate the 'masquerader' class
  47. proxy = ParaProxy(*io_tupple)
  48.  
  49. # Connecting to AnotherMachine and executing... anything...
  50. end_cli = paramiko.SSHClient()
  51. end_cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  52. end_cli.connect(hostname='2.2.2.2', username='user', password='pass', sock=proxy)
  53. end_cli.exec_command('echo THANK GOD FINALLY')
  54.  
  55. "ssh -f user@anothermachine -L 2000:localhost:22 -N"
  56.  
  57. paramiko.connect(middlemachine, 2000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement