Skip to content

Instantly share code, notes, and snippets.

@pikulet
Created December 30, 2020 20:24
Show Gist options
  • Save pikulet/858098d91200518ea9cb636fff565015 to your computer and use it in GitHub Desktop.
Save pikulet/858098d91200518ea9cb636fff565015 to your computer and use it in GitHub Desktop.
port forward to access ncl experiments via rdp
#!/usr/bin/python3
import subprocess
import socket
import argparse
import os
# You may change this to your own ncl account
ncl_username = "username"
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return (s.connect_ex(('localhost', port)) == 0)
def main():
parser = argparse.ArgumentParser(description='Set up port forwarding for NCL testbed.')
parser.add_argument('--username', type=str, help='ssh username for your NCL testbed.', default=ncl_username)
args = parser.parse_args()
# Find a free local port
local_port = 10000
while (is_port_in_use(local_port)):
local_port += 1
remote_port = 12344
# Print hint
print("If this script freezes after you enter your password, then it is working! Keep it running here.")
print("Use the this command to make a rdesktop connection in another terminal:")
print("\trdesktop -a 16 localhost:" + str(local_port))
# Make comand
cmd = ["/usr/bin/ssh",
"-N",
"-L",
str(local_port)+":node.experiment.team.ncl.sg:"+str(remote_port),
args.username+"@users.ncl.sg"]
# Run~!
while (True):
try:
print(subprocess.check_output(cmd))
except (KeyboardInterrupt):
# Capture ctrl+c
exit()
print("An error occurred. Try again?")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment