-
-
Save danricho/4c2fafb4de12b817b727027d01cab74e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""Colab to SSH | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/gist/smartm13/7978192956e79452f46020c277f53ec7/colab2ssh.ipynb | |
#Setup SSH port forwarding | |
""" | |
#1 - setup ssh/user | |
#Generate a random root password | |
import random, string | |
password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(30)) | |
password = 'toor' | |
run_sh = lambda cmd,retainspace="_space_",toscript=True:((toscript and open("/tmp/toscript.sh",'w').write(cmd)) and not toscript) or (__import__("subprocess").run("sh /tmp/toscript.sh".split() if toscript else [c.replace(retainspace," ") for c in cmd.split()], stdout=__import__("subprocess").PIPE).stdout.decode('utf-8').strip().split("\n")) | |
#Setup sshd | |
run_sh("apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen") | |
#Set root password | |
run_sh("echo root:$password | chpasswd".replace("$password",password)) | |
run_sh("mkdir -p /var/run/sshd") | |
run_sh('echo "PermitRootLogin yes" >> /etc/ssh/sshd_config') | |
run_sh('echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config') | |
print("username: root") | |
print("password: ", password) | |
#Run sshd | |
get_ipython().system_raw('/usr/sbin/sshd -D &') | |
# 2 - Download Ngrok | |
run_sh("wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip") | |
run_sh("unzip -qq -n ngrok-stable-linux-amd64.zip") | |
# 3 - setup Ngrok - authtoken | |
#print("Get your authtokens from https://dashboard.ngrok.com/auth") | |
#collection of some working authtokens | |
chalu1='1WXO8VheqmW0bly8k3nXZn5PRGd_xEviNuNJmtNNqiAqFyN6' | |
chalu2='1WXSLgMGAtFE1xjGcuB8L7r0LKg_PR65aD9WLuBDGhWitA9C' | |
authtokens = [chalu1,chalu2] # or __import__("getpass").getpass() | |
with open("/content/ngrok-launch.sh",'w') as f: | |
f.write("rm -rf /tmp/ngrok-mic.drop\n") | |
for autht in authtokens: | |
f.write(f"/content/ngrok authtoken {autht} && /content/ngrok tcp 22 && exit\n") | |
f.write("echo done>/tmp/ngrok-mic.drop\n") | |
#Create tunnel | |
print("Launching NGROK") | |
#get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &') | |
get_ipython().system_raw("bash /content/ngrok-launch.sh &") | |
import os | |
port='x' | |
while not port[0].isdigit(): | |
if port!='x': | |
print("Waiting for ngrok to launch with right key...") | |
port=run_sh("curl -s http://localhost:4040/api/tunnels | python -mjson.tool | grep public_url | awk -F: '{print $4}'| rev | cut -c 3- |rev") | |
if os.path.isfile("/tmp/ngrok-mic.drop"): | |
print("Ngrok Launch Failed") | |
break | |
print(f"Here are your ssh creds:\nssh [email protected] -p {port[0]}\n{password}") | |
with open("/content/ngrok-kill.sh",'w') as f:f.write("kill $(ps aux | grep './ngrok' | awk '{print $2}')") | |
print("Ending Note: To kill use /content/ngrok-kill.sh") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment