Last active
August 27, 2019 08:40
-
-
Save Extrodox/264d95381e14e8b82c4e6fd56d66a94a to your computer and use it in GitHub Desktop.
How can I ssh to google colaboratory VM?
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Untitled1.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Extrodox/264d95381e14e8b82c4e6fd56d66a94a/untitled1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fwf6kmq7LunX", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 87 | |
}, | |
"outputId": "8a7341fb-6894-4698-e8e3-5d9f3ce633ca" | |
}, | |
"source": [ | |
"import random, string, urllib.request, json, getpass\n", | |
"\n", | |
"#Generate root password\n", | |
"password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))\n", | |
"\n", | |
"#Download ngrok\n", | |
"! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n", | |
"! unzip -qq -n ngrok-stable-linux-amd64.zip\n", | |
"\n", | |
"#Setup sshd\n", | |
"! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null\n", | |
"\n", | |
"#Set root password\n", | |
"! echo root:$password | chpasswd\n", | |
"! mkdir -p /var/run/sshd\n", | |
"! echo \"PermitRootLogin yes\" >> /etc/ssh/sshd_config\n", | |
"! echo \"PasswordAuthentication yes\" >> /etc/ssh/sshd_config\n", | |
"! echo \"LD_LIBRARY_PATH=/usr/lib64-nvidia\" >> /root/.bashrc\n", | |
"! echo \"export LD_LIBRARY_PATH\" >> /root/.bashrc\n", | |
"\n", | |
"#Run sshd\n", | |
"get_ipython().system_raw('/usr/sbin/sshd -D &')\n", | |
"\n", | |
"#Ask token\n", | |
"print(\"Copy authtoken from https://dashboard.ngrok.com/auth\")\n", | |
"authtoken = getpass.getpass()\n", | |
"\n", | |
"#Create tunnel\n", | |
"get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')\n", | |
"\n", | |
"#Get public address and print connect command\n", | |
"with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:\n", | |
" data = json.loads(response.read().decode())\n", | |
" (host, port) = data['tunnels'][0]['public_url'][6:].split(':')\n", | |
" print(f'SSH command: ssh -p{port} root@{host}')\n", | |
"\n", | |
"#Print root password\n", | |
"print(f'Root password: {password}')" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Copy authtoken from https://dashboard.ngrok.com/auth\n", | |
"··········\n", | |
"SSH command: ssh -p16968 [email protected]\n", | |
"Root password: 0IAb6dVtDlRqAceg53QG\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "vlfSmJ3gL9MD", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment