kristulus07

konek ssh ngrok google colab

Nov 1st, 2020
6,510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import random, string, urllib.request, json, getpass
  2.  
  3. #Generate root password
  4. password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))
  5.  
  6. #Download ngrok
  7. ! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
  8. ! unzip -qq -n ngrok-stable-linux-amd64.zip
  9.  
  10. #Setup sshd
  11. ! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null
  12.  
  13. #Set root password
  14. ! echo root:$password | chpasswd
  15. ! mkdir -p /var/run/sshd
  16. ! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  17. ! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
  18. ! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc
  19. ! echo "export LD_LIBRARY_PATH" >> /root/.bashrc
  20.  
  21. #Run sshd
  22. get_ipython().system_raw('/usr/sbin/sshd -D &')
  23.  
  24. #Ask token
  25. print("Copy authtoken from https://dashboard.ngrok.com/auth")
  26. authtoken = getpass.getpass()
  27.  
  28. #Create tunnel
  29. get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')
  30.  
  31. #Get public address and print connect command
  32. with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:
  33. data = json.loads(response.read().decode())
  34. (host, port) = data['tunnels'][0]['public_url'][6:].split(':')
  35. print(f'SSH command: ssh -p{port} root@{host}')
  36.  
  37. #Print root password
  38. print(f'Root password: {password}')
Add Comment
Please, Sign In to add comment