0% found this document useful (0 votes)
63 views

SSH in Colab - Ipynb

Uploaded by

uppermoonten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

SSH in Colab - Ipynb

Uploaded by

uppermoonten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

{

"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/ca2d3f2758d720d2684ec97cf68
4b6ba/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": []
}
]
}

You might also like