Last active
June 16, 2024 20:46
-
-
Save rava-dosa/ca2d3f2758d720d2684ec97cf684b6ba to your computer and use it in GitHub Desktop.
SSHing in colab
This file contains 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": "Untitled0.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyN1pqmVlqG5Yt6uK3gNiyft", | |
"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/rava-dosa/ca2d3f2758d720d2684ec97cf684b6ba/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4tygzZ_qs2w0" | |
}, | |
"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", | |
"#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 &')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "w9-C40wKjZaw" | |
}, | |
"source": [ | |
"#Setup sshd\n", | |
"# ! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null\n", | |
"! sudo apt-get install openssh-server\n", | |
"# ! sudo apt-get install openssh-server --fix-missing" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "rH9VlIBAjvBh" | |
}, | |
"source": [ | |
"# if install fail run this, uncomment and run the below line\n", | |
"! sudo apt-get install openssh-server --fix-missing" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JFo-KIIaj2Xe" | |
}, | |
"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": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "NA4SBV8Aj6rN" | |
}, | |
"source": [ | |
"get_ipython().system_raw('/usr/sbin/sshd -D &')" | |
], | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "i_N7u4LvkAjX" | |
}, | |
"source": [ | |
"# to check if ssh is running on port 22 or not or installed or not\n", | |
"! sudo apt install net-tools\n", | |
"! netstat -plant | grep ssh\n", | |
"! which sshd" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "j2oB-MC8kbBs" | |
}, | |
"source": [ | |
"# get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')\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", | |
"#Print root password\n", | |
"print(f'SSH password: {password}')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment