Skip to content

Instantly share code, notes, and snippets.

@hiway
Last active October 28, 2022 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiway/17542e177c4324b685360d11827a8ae6 to your computer and use it in GitHub Desktop.
Save hiway/17542e177c4324b685360d11827a8ae6 to your computer and use it in GitHub Desktop.
Streamlit Demo - WebUI to generate/access SSH key-pairs for hosts.
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.")
@hiway
Copy link
Author

hiway commented Oct 26, 2022

Screenshot 2022-10-26 at 17-35-55 Generate SSH Key-Pair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment