[+] Update ssh keys script
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
|
||||
|
||||
def ansi_rgb(r: int, g: int, b: int, foreground: bool = True) -> str:
|
||||
"""
|
||||
Convert rgb color into ANSI escape code format
|
||||
|
||||
:param r:
|
||||
:param g:
|
||||
:param b:
|
||||
:param foreground: Whether the color applies to forground
|
||||
:return: Escape code
|
||||
"""
|
||||
c = '38' if foreground else '48'
|
||||
return f'\033[{c};2;{r};{g};{b}m'
|
||||
|
||||
|
||||
replacements = ["&0/\033[0;30m", "&1/\033[0;34m", "&2/\033[0;32m", "&3/\033[0;36m", "&4/\033[0;31m",
|
||||
"&5/\033[0;35m", "&6/\033[0;33m", "&7/\033[0;37m", "&8/\033[1;30m", "&9/\033[1;34m",
|
||||
"&a/\033[1;32m", "&b/\033[1;36m", "&c/\033[1;31m", "&d/\033[1;35m", "&e/\033[1;33m",
|
||||
"&f/\033[1;37m",
|
||||
"&r/\033[0m", "&l/\033[1m", "&o/\033[3m", "&n/\033[4m", "&-/\n"]
|
||||
replacements = [(r[:2], r[3:]) for r in replacements]
|
||||
|
||||
|
||||
def color(msg: str) -> str:
|
||||
"""
|
||||
Replace extended minecraft color codes in string
|
||||
|
||||
:param msg: Message with minecraft color codes
|
||||
:return: Message with escape codes
|
||||
"""
|
||||
for code, esc in replacements:
|
||||
msg = msg.replace(code, esc)
|
||||
|
||||
while '&gf(' in msg or '&gb(' in msg:
|
||||
i = msg.index('&gf(') if '&gf(' in msg else msg.index('&gb(')
|
||||
end = msg.index(')', i)
|
||||
code = msg[i + 4:end]
|
||||
fore = msg[i + 2] == 'f'
|
||||
|
||||
if code.startswith('#'):
|
||||
rgb = tuple(int(code.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
|
||||
else:
|
||||
code = code.replace(',', ' ').replace(';', ' ').replace(' ', ' ')
|
||||
rgb = tuple(int(c) for c in code.split(' '))
|
||||
|
||||
msg = msg[:i] + ansi_rgb(*rgb, foreground=fore) + msg[end + 1:]
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
def printc(msg: str):
|
||||
"""
|
||||
Print with color
|
||||
|
||||
:param msg: Message with minecraft color codes
|
||||
"""
|
||||
print(color(msg + '&r'))
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
import importlib
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from color_utils import printc
|
||||
|
||||
|
||||
def import_or_install(module: str, package: str | None = None):
|
||||
try:
|
||||
return importlib.import_module(module)
|
||||
except ModuleNotFoundError:
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", package or module])
|
||||
return importlib.import_module(module)
|
||||
|
||||
|
||||
requests = import_or_install('requests')
|
||||
|
||||
|
||||
GITHUB_USERS = ['hykilpikonna']
|
||||
KEYS_PATH = Path.home() / '.ssh' / 'authorized_keys'
|
||||
|
||||
|
||||
def fetch_keys(user: str) -> set[str]:
|
||||
resp = requests.get(f"https://github.com/{user}.keys").text
|
||||
return {f"{l} {user}@github" for l in resp.strip().splitlines()}
|
||||
|
||||
|
||||
def fetch_all_keys() -> set[str]:
|
||||
return {l for u in GITHUB_USERS for l in fetch_keys(u)}
|
||||
|
||||
|
||||
def normalize_key(key: str) -> str:
|
||||
"""
|
||||
Remove comment from keys
|
||||
"""
|
||||
return ' '.join(key.split(' ')[:2])
|
||||
|
||||
|
||||
def update_ssh_keys():
|
||||
all_keys = list(fetch_all_keys())
|
||||
normalized_keys = {normalize_key(k) for k in all_keys}
|
||||
|
||||
existing_keys = set(KEYS_PATH.read_text('utf-8').strip().splitlines())
|
||||
for k in existing_keys:
|
||||
if normalize_key(k) not in normalized_keys:
|
||||
all_keys.append(k)
|
||||
|
||||
len_diff = len(all_keys) - len(existing_keys)
|
||||
if len_diff > 0:
|
||||
printc(f"&aSSH Keys Updated! Added {len_diff} keys")
|
||||
|
||||
KEYS_PATH.write_text('\n'.join(all_keys))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
update_ssh_keys()
|
||||
+1
-1
@@ -47,7 +47,7 @@ alias compress-zst="tar -I 'zstd -T36 -19' --checkpoint=.1024 --totals -c -f"
|
||||
alias restart-kwin="DISPLAY=:0 setsid kwin_x11 --replace"
|
||||
|
||||
alias catt="echo 🐱"
|
||||
alias update-ssh-keys="curl -L https://github.com/Hykilpikonna.keys > ~/.ssh/authorized_keys"
|
||||
alias old-update-ssh-keys="curl -L https://github.com/Hykilpikonna.keys > ~/.ssh/authorized_keys"
|
||||
|
||||
export PATH="$SCR/bin:$PATH"
|
||||
export PATH="$HOME/.local/bin:$PATH"
|
||||
|
||||
Reference in New Issue
Block a user