Last active
January 23, 2021 12:04
-
-
Save pn11/f9ae1c5b6c8933957ba66f6caaa68c9b to your computer and use it in GitHub Desktop.
ngrok を使って Google Colab に SSH ログインする。 cf. https://qiita.com/hazigin/items/c291adf5dc9ccc13d11f
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": "colab-ssh.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyPo3Viz+Rwv1X/SxqDb5TXO", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"accelerator": "TPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/pn11/f9ae1c5b6c8933957ba66f6caaa68c9b/colab-ssh.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6qBRSjqL-cgL" | |
}, | |
"source": [ | |
"import random, string, urllib.request, json, getpass, time" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XFv7Q_VzE9ur" | |
}, | |
"source": [ | |
"auth_token=\"\"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XO2FXBnm-i7I" | |
}, | |
"source": [ | |
"#Generate root password\n", | |
"password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "c8W42D-t-kub" | |
}, | |
"source": [ | |
"#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" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "eq5gVcaA-puN" | |
}, | |
"source": [ | |
"#Setup sshd\n", | |
"! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_0V1Fo0t-wx9" | |
}, | |
"source": [ | |
"#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" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uCXRIY8i-4zs" | |
}, | |
"source": [ | |
"#Run sshd\n", | |
"get_ipython().system_raw('/usr/sbin/sshd -D &')\n", | |
"time.sleep(5)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6Jx2q4H4_CnI" | |
}, | |
"source": [ | |
"#Create tunnel\n", | |
"get_ipython().system_raw(f\"./ngrok authtoken {auth_token} && ./ngrok tcp 22 &\")\n", | |
"time.sleep(5)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Q7wCLTIaENd3" | |
}, | |
"source": [ | |
"!ps -aux" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "1OVzHUHp_Iml" | |
}, | |
"source": [ | |
"#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": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "xzv0OZzrGLgX" | |
}, | |
"source": [ | |
"エラーになる場合は右上の Magene Sessions から使ってないセッションを terminate してみる (ngrok の制限で1セッションしか使えないため)\n", | |
"" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment