Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MyLocalMachine ----||----> MiddleMachine --(netcat)--> AnotherMachine
- ('localhost') (firewall) ('1.1.1.1') ('2.2.2.2')
- cli = paramiko.SSHClient()
- cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- proxy = paramiko.ProxyCommand('ssh user@1.1.1.1 nc 2.2.2.2 22')
- cli.connect(hostname='2.2.2.2', username='user', password='pass', sock=proxy)
- import paramiko
- class ParaProxy(paramiko.util.ClosingContextManager):
- def __init__(self, stdin, stdout, stderr):
- self.stdin = stdin
- self.stdout = stdout
- self.stderr = stderr
- def send(self, content):
- try:
- self.stdin.write(content)
- except IOError as exc:
- print('IOError exception.')
- return
- return len(content)
- def recv(self, size):
- buffer = b''
- while len(buffer) < size:
- buffer += self.stdout.read(size - len(buffer))
- return buffer
- def close(self):
- self.stdin.close()
- self.stdout.close()
- self.stderr.close()
- def settimeout(self, timeout):
- print('Attempt to set timeout to {} - ignoring.'.format(timeout))
- # Connecting to MiddleMachine and executing netcat
- mid_cli = paramiko.SSHClient()
- mid_cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- mid_cli.connect(hostname='1.1.1.1', username='user', password='pass')
- io_tupple = mid_cli.exec_command('nc 2.2.2.2 22')
- # Instantiate the 'masquerader' class
- proxy = ParaProxy(*io_tupple)
- # Connecting to AnotherMachine and executing... anything...
- end_cli = paramiko.SSHClient()
- end_cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- end_cli.connect(hostname='2.2.2.2', username='user', password='pass', sock=proxy)
- end_cli.exec_command('echo THANK GOD FINALLY')
- "ssh -f user@anothermachine -L 2000:localhost:22 -N"
- paramiko.connect(middlemachine, 2000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement