Last active
October 28, 2022 21:17
-
-
Save hiway/17542e177c4324b685360d11827a8ae6 to your computer and use it in GitHub Desktop.
Streamlit Demo - WebUI to generate/access SSH key-pairs for hosts.
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
from pathlib import Path | |
from typing import List | |
import pyperclip | |
import streamlit as st | |
from plumbum import local | |
SSH_KEYS_DIR = "~/.ssh/keys" | |
# --- API --- | |
def get_hostname() -> str: | |
return local["hostname"]().strip() | |
def get_username() -> str: | |
return local["whoami"]().strip() | |
def get_existing_ssh_key_names(ssh_keys_dir: str = SSH_KEYS_DIR) -> List[str]: | |
ssh_key_names = [] | |
for path in Path(ssh_keys_dir).expanduser().glob("*.pub"): | |
ssh_key_names.append(f"{path.name.replace('.pub', '')}") | |
return sorted(ssh_key_names) | |
def generate_ssh_key_pair(host: str, key_type: str, ssh_keys_dir: str = SSH_KEYS_DIR): | |
path = Path(ssh_keys_dir).expanduser() | |
path.mkdir(parents=True, exist_ok=True) | |
local["ssh-keygen"]( | |
"-t", | |
key_type, | |
"-f", | |
path / host, | |
"-N", | |
"", | |
"-C", | |
f"{get_username()}@{get_hostname()}", | |
) | |
def copy_to_clipboard(host: str, ssh_keys_dir: str = SSH_KEYS_DIR): | |
path = Path(ssh_keys_dir).expanduser() | |
public_key_path = path / f"{host}.pub" | |
pyperclip.copy(public_key_path.read_text()) | |
# --- Templates --- | |
def render_header(icon: str, title: str, subtitle: str = ""): | |
st.set_page_config( | |
page_title=title, | |
page_icon=icon, | |
layout="wide", | |
initial_sidebar_state="collapsed", | |
) | |
st.header(title + " " + icon) | |
st.subheader(subtitle) | |
def render_existing_hosts_form(): | |
existing_ssh_key_names = get_existing_ssh_key_names() | |
if existing_ssh_key_names: | |
st.write("Available SSH key-pairs:") | |
with st.form(key="ssh_hosts_available"): | |
txt_host = st.selectbox(label="Host", options=existing_ssh_key_names) | |
btn_clipboard = st.form_submit_button(label="Copy public key to clipboard") | |
return txt_host, btn_clipboard | |
else: | |
st.write("No existing SSH key-pairs found.") | |
return None, None | |
def render_new_host_form(): | |
st.write("Generate a new SSH key-pair:") | |
with st.form(key="ssh_host_new"): | |
txt_host = st.text_input(label="Host", placeholder="example") | |
txt_key_type = st.selectbox( | |
label="key_type", options=["ED25519", "RSA", "ECDSA"], index=0 | |
) | |
btn_continue = st.form_submit_button(label="Generate key-pair") | |
return txt_host, txt_key_type, btn_continue | |
# --- Main --- | |
render_header("🔐", "Generate SSH Key-Pair") | |
host, btn_clipboard = render_existing_hosts_form() | |
if btn_clipboard and host: | |
copy_to_clipboard(host) | |
new_host, key_type, btn_continue = render_new_host_form() | |
if btn_continue and new_host and key_type: | |
generate_ssh_key_pair(new_host, key_type) | |
copy_to_clipboard(new_host) | |
st.success(f"Copied public key for **{new_host}** to clipboard.") |
Author
hiway
commented
Oct 26, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment