diff --git a/.config/alacritty/alacritty.yml b/.config/alacritty/alacritty.yml index cbd1b31..a730042 100644 --- a/.config/alacritty/alacritty.yml +++ b/.config/alacritty/alacritty.yml @@ -155,6 +155,6 @@ schemes: # colors: *gruvbox_material_medium_dark # colors: *gruvbox_material_soft_dark -# colors: *gruvbox_material_hard_light +colors: *gruvbox_material_hard_light # colors: *gruvbox_material_medium_light -colors: *gruvbox_material_soft_light +# colors: *gruvbox_material_soft_light diff --git a/gist-read-write.ipynb b/gist-read-write.ipynb index 9349a0e..504bb47 100644 --- a/gist-read-write.ipynb +++ b/gist-read-write.ipynb @@ -2,97 +2,185 @@ "cells": [ { "cell_type": "code", - "execution_count": 8, - "id": "8492734d-e7ba-4732-b1bf-596956811d9b", + "execution_count": null, + "id": "f2ec458c-c7a2-42ee-8816-b5ea8f8c0759", + "metadata": {}, + "outputs": [], + "source": [ + "# each computer should " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2075b6f7-9c53-454b-a6af-818f223f24b4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Collecting python-dotenv\n", - " Downloading python_dotenv-0.20.0-py3-none-any.whl (17 kB)\n", - "Installing collected packages: python-dotenv\n", - "Successfully installed python-dotenv-0.20.0\n", - "\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n", - "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" + "python version: 3.9\n" ] + }, + { + "data": { + "text/plain": [ + "'id_rsa_mynetwork.pub'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" } ], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "2075b6f7-9c53-454b-a6af-818f223f24b4", - "metadata": {}, - "outputs": [], "source": [ + "# %pip install python-dotenv\n", + "# make a new token on:\n", + "# https://github.com/settings/tokens/new\n", + "# scope should only include gist\n", "'''\n", "# template .env file\n", "token='ghp_'\n", "gist_id='abc...123'\n", - "filename='.txt'\n", + "filename=''\n", "'''\n", "\n", - "# %pip install python-dotenv\n", - "\n", "from dotenv import load_dotenv\n", "import requests\n", "import json\n", "import os\n", + "from pathlib import Path\n", + "import sys\n", "\n", + "# check python version\n", + "version = sys.version_info\n", + "required_version = (3, 6)\n", + "if not version >= required_version:\n", + " raise EnvironmentError(\n", + " f'detected python version {version} - must be at least 3.6!'\n", + " )\n", + "print(f'python version: {version.major}.{version.minor}')\n", "\n", - "### write current machine ip address to gist file\n", + "### write current machiens public key to gist file\n", "\n", "# read env vars\n", "# S/O https://stackoverflow.com/a/61029741\n", "load_dotenv()\n", "token = os.getenv('token')\n", "gist_id = os.getenv('gist_id')\n", - "filename = os.getenv('filename')\n", - "\n", - "# fetch external ip address\n", - "# S/O https://stackoverflow.com/a/36205547\n", - "ip_addr = requests.get('https://api.ipify.org').content.decode('utf8')\n", - "\n", - "# request gist changes\n", - "# S/O https://stackoverflow.com/a/65761251\n", - "headers = {'Authorization': f'token {token}'}\n", - "request_url = f'https://api.github.com/gists/{gist_id}' \n", - "request_data = json.dumps({'files':{filename:{\"content\":ip_addr}}})\n", - "\n", - "r = requests.patch(\n", - " url=request_url,\n", - " data=request_data,\n", - " headers=headers)" + "ssh_filename = os.getenv('filename')\n", + "ssh_filename" ] }, { "cell_type": "code", - "execution_count": 42, - "id": "fcda799c-3feb-4591-9582-f6187994fe36", + "execution_count": 2, + "id": "0e534b35-8b71-4cb1-8dde-0eef9eb6dd36", "metadata": {}, "outputs": [], + "source": [ + "# get id_rsa_mynetwork public key\n", + "gist_filename = 'authorized-keys'\n", + "\n", + "\n", + "def append_gist(gist_id, data, append=False):\n", + " # request data\n", + " headers = {'Authorization': f'token {token}'}\n", + " request_url = f'https://api.github.com/gists/{gist_id}' \n", + "\n", + " # get gist contents (all-files)\n", + " request_data = json.dumps({'files':gist_filename})\n", + " r = requests.get(\n", + " url=request_url,\n", + " data=request_data,\n", + " headers=headers)\n", + " gist_content = r.json()\n", + " \n", + " # if gist file exists, get its data\n", + " if gist_filename in gist_content['files'].keys():\n", + " gist_text = gist_content['files'][gist_filename]['content']\n", + " # ... if not, set its data to empty-string\n", + " else:\n", + " gist_text = ''\n", + "\n", + " # if gist already contains data, exit\n", + " if data in gist_text:\n", + " return\n", + "\n", + " # append gist new contents to existing and patch\n", + " content = gist_text + '\\n'+ data\n", + " request_data = json.dumps({'files': {gist_filename: {\"content\": content}}})\n", + " r = requests.patch(\n", + " url=request_url,\n", + " data=request_data,\n", + " headers=headers)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "93ae1879-f245-4478-98fe-0a62e72c00f6", + "metadata": {}, + "outputs": [], + "source": [ + "# get ssh public key\n", + "ssh_pub = Path.home().joinpath(f'.ssh/{ssh_filename}').read_text()\n", + "\n", + "# write ssh key to gist\n", + "append_gist(\n", + " gist_id=gist_id,\n", + " data=ssh_pub)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fcda799c-3feb-4591-9582-f6187994fe36", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDm6wZlW45N+K24nw6uspMDofP6X+2T11XI8o1Z10gfieX1miJMCaDzlOvATYOKe/bzdkCR7vd8qNyKeJn/cSS95tvOCFi+LjryPzPp1bwuSqZSvtUIb9RD11exlXcXSawVEDmpOOElOiJs7lK7gulB4McIc/Y+ZxrSgo86/mFDOchaE2fWScC/QFVbNRRyuHa2/jlHtQW4ROODJIJXsu9OVMfrTrEdkXuwkNQ6d1KKKC5/IxbvmUtFC0vWEY7vuRVTBgGWv32n9RIhYePbpQeW/l5PwvrE+C3LmCgcQhi7y5NoZNvAoN3wAknQPAGIn0gZ5WukOUGlUkAdRwyJ007kXgiyfMVFV57HioO441rsVFCrOhYcQOMBhmO2a0V/y4aRG1hd0DJY/dBCzh8vVxYMq02h2ta+Sg89uxlcn4DAl5z7KqEkbPFJnLA67xUHvnvOLzKY5PXI7/3m8mbvOleeNGOOiBGRitLT+PbbVRxUg6yxVyL4T9ewOA9uMo0e3tSmaSEMBS2c8DJ4p1GVoFWKm0WqMF3GRWNUyzGEUZqcWblYjssjmx0G9L7lwI8JXHLXrxkhqmZyCE2atag7oWfrUirnQlcPaUX7BgpJbmKPTFK39jiOgij62y7DcbbQwKhSV6Bx8mRjPOaDVJuDKxuW0wIXdW2GY033V8gxPhbdRQ== dth@dth-MacBookAir\\n'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "### fetch gist file content\n", "\n", "headers = {'Authorization': f'token {token}'}\n", "request_url = f'https://api.github.com/gists/{gist_id}' \n", - "request_data = json.dumps({'files':{filename:{\"content\":ip_addr}}})\n", + "request_data = json.dumps({'files':gist_filename})\n", "r = requests.get(\n", " url=request_url,\n", " data=request_data,\n", " headers=headers)\n", - "gist_content = r.json()['files'][filename.strip()]['content']" + "gist_content = r.json()['files'][gist_filename]['content']\n", + "gist_content" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "07a9cbfe-8ab1-42d0-9810-cf2b68c97b5d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'94.147.46.129'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "gist_content" ] diff --git a/makefile b/makefile index 86bff3b..34f4f57 100644 --- a/makefile +++ b/makefile @@ -66,6 +66,10 @@ setup_server_ip_sync_python_env: git_overview: cd ~ && find . -name .git -type d -prune -setup_server_authorized_keys: +setup_server_mynetwork: mkdir -p ~/.ssh ln -s -f ~/dotfiles/.ssh/authorized_keys ~/.ssh/authorized_keys + +setup_client_mynetwork: + ssh-keygen -q -t rsa -b 4096 -N '' -f ~/.ssh/id_rsa_mynetwork <</dev/null 2>&1 + cat ~/.ssh/id_rsa_mynetwork.pub >> ~/dotfiles/.ssh/authorized_keys