Skip to content

Instantly share code, notes, and snippets.

@hanasuru
Last active December 23, 2021 15:59
Show Gist options
  • Save hanasuru/272bccb6dca46dcb51e4a78f47e1138c to your computer and use it in GitHub Desktop.
Save hanasuru/272bccb6dca46dcb51e4a78f47e1138c to your computer and use it in GitHub Desktop.
Basic Implementation of Remote SSH-FORWARDING of multiple port instance
#!/usr/bin/python2
from sys import argv
from os import system
CMD = 'autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3"'
DEF_SERVER = '0.0.0.0'
DEF_LOCAL = '127.0.0.1'
DEF_TUN = 30000
def parse_args(server_in, local_in, port):
global DEF_TUN
param =' -R {}:{}:{}:{}'.format(server_in, DEF_TUN, local_in, port)
DEF_TUN +=1
return param
def usage():
print 'USAGE : ./ssh_tun.py [OPTIONS]'
print 'OPTIONS'
print '[+] -i server_in\t: server interface; Default = 0.0.0.0'
print '[+] -I local_in\t\t: local interface; Default = localhost'
print '[+] -u user\t\t: server user'
print '[+] -h hostname\t\t: Server hostname'
print '[+] --key priv_key\t: Private key path'
print '[+] -p "ports"\t\t: Local open-ports'
def main():
server = argv[argv.index('-i') + 1] if '-I' in argv else DEF_SERVER
local = argv[argv.index('-I') + 1] if '-I' in argv else DEF_LOCAL
user = argv[argv.index('-u') + 1] if '-u' in argv else None
priv_key = argv[argv.index('--key') + 1] if '--key' in argv else None
hostname = argv[argv.index('-h') + 1] if '-h' in argv else None
port = argv[argv.index('-p') + 1] if '-p' in argv else ''
if user and priv_key and hostname and port:
try:
print '[+] Connecting'
params = ''
for i in port.split():
params += parse_args(server,local, i)
command = '{}{} {}@{} -i {}'.format(CMD, ''.join(params), user, hostname, priv_key)
print '[v] Connection established'
system(command)
except:
raise Exception('Error')
else:
usage()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment