Skip to content

Instantly share code, notes, and snippets.

@alexdlaird
Last active December 27, 2023 17:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexdlaird/9692dcd830f36312a007d828364559b5 to your computer and use it in GitHub Desktop.
Save alexdlaird/9692dcd830f36312a007d828364559b5 to your computer and use it in GitHub Desktop.
pyngrok - Colab SSH Example.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "pyngrok - Colab SSH Example.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOpHkzrRHRfQiwr2+eRD6RV",
"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/alexdlaird/9692dcd830f36312a007d828364559b5/pyngrok-colab-ssh-example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "zKDAYQi4wJbY",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"import getpass\n",
"\n",
"# Prompt for a password that will be set for the SSH root user\n",
"print(\"Enter a password for SSH access\")\n",
"password = getpass.getpass()\n",
"os.system(f\"echo root:{password} | chpasswd\")\n",
"\n",
"# Install\n",
"os.system(\"apt-get install -y openssh-server\")\n",
"\n",
"# Setup and start the SSH server\n",
"if not os.path.exists(\"/var/run/sshd\"):\n",
" os.makedirs(\"/var/run/sshd\")\n",
"\n",
"os.system(\"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config\")\n",
"os.system(\"echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config\")\n",
"\n",
"os.system(\"/usr/sbin/sshd\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "W8YjINsA3svL",
"colab_type": "code",
"colab": {}
},
"source": [
"from google.colab import drive\n",
"\n",
"drive.mount(\"/content/drive\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nwuh2gDAx6Mh",
"colab_type": "code",
"colab": {}
},
"source": [
"!pip install pyngrok"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hvTNe-Blx7vJ",
"colab_type": "code",
"colab": {}
},
"source": [
"import getpass\n",
"\n",
"from pyngrok import ngrok, conf\n",
"\n",
"print(\"Enter your authtoken, which can be copied from https://dashboard.ngrok.com/get-started/your-authtoken\")\n",
"conf.get_default().auth_token = getpass.getpass()\n",
"\n",
"# Open a TCP ngrok tunnel to the SSH server\n",
"connection_string = ngrok.connect(\"22\", \"tcp\").public_url\n",
"\n",
"ssh_url, port = connection_string.strip(\"tcp://\").split(\":\")\n",
"print(f\" * ngrok tunnel available, access with `ssh root@{ssh_url} -p{port}`\")"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment