From 03809b7e5d60431281ec23b0c13afa52c98cd863 Mon Sep 17 00:00:00 2001 From: Astound Date: Sat, 21 Jun 2025 23:21:29 -0500 Subject: [PATCH] update ssh agent --- hyperterm/hyperterm.sha512 | 2 +- hyperterm/tools/ssh-agent.sh | 112 +++++++++++++++++------------------ 2 files changed, 54 insertions(+), 60 deletions(-) diff --git a/hyperterm/hyperterm.sha512 b/hyperterm/hyperterm.sha512 index 0eb3f7e..e4ad400 100644 --- a/hyperterm/hyperterm.sha512 +++ b/hyperterm/hyperterm.sha512 @@ -25,4 +25,4 @@ f760432c3d76befad30588299eb2d1412d77b22fd850ffbd840c72123885d4e916a7e0b16e7048c5 fab9d339a99c7d2e1809d1c44f533523c6bfcdcc8d63c62b335ce7d4c666c8bdd7ac319316bf71f043163a3a0184e25ecfe1ee32724627424d042a05fa80ce77 ./tools/vconverter.sh ee1d6d1f9b010318985f7154c2a9173c8f2ab6b637cd3c8d2a9b403e83470e15a273dcff326a84f035660807d7cfcf04efe5abc0495e25ae7339b8807899cf0d ./tools/listuser.sh 243e3a076f1696bde1e464b479e221876177eb98c92415a09de8dc9e8d138e88e006eb9fa441ca1ab19d260cb3fd4de82dc54feae73453e229c3a8fdab3043f0 ./tools/virtualenv.sh -1ba63accea347b96c30fcd4a2fa84c531836b082ed5ced035a8f30a0d738724f8f171ec85645779e682c8d0aa1d5f6c5b32e182454cdb8f0c85dde08a37a96ca ./tools/ssh-agent.sh +26f1e5868a01ae400be225dda3633a0fd494560d9cf472d78b98aa3edc0dc5ab5d75de3de5f2a89bb6537a8a63024edd02d6073d80c8cc50ba64334a9f5cdd37 ./tools/ssh-agent.sh diff --git a/hyperterm/tools/ssh-agent.sh b/hyperterm/tools/ssh-agent.sh index 3db6418..f991cb7 100644 --- a/hyperterm/tools/ssh-agent.sh +++ b/hyperterm/tools/ssh-agent.sh @@ -4,64 +4,68 @@ # SSH-AGENT #------------ function sshagent_start { - local key_path="$HOME/.ssh/id_ed25519" - local lifetime="5d" + local ssh_dir="$HOME/.ssh" + local lifetime="" + local key_path="" - # Parse options - while getopts "t:k:" opt; do - case "$opt" in - t) lifetime="$OPTARG" ;; - k) key_path="$OPTARG" ;; - *) - echo "Usage: sagent_start [-t lifetime] [-k key_path]" - return 1 - ;; - esac + msg "Buscando claves privadas en $ssh_dir..." "Looking for private keys in $ssh_dir..." + mapfile -t keys < <(find "$ssh_dir" -type f -not -name "*.pub" -exec grep -l "PRIVATE KEY" {} \;) + + if [ "${#keys[@]}" -eq 0 ]; then + msg_err "No se encontraron claves privadas en $ssh_dir" "No private keys found in $ssh_dir" + return 1 + fi + + msg "\nSelecciona la clave que deseas agregar al agente SSH:" "\nSelect the key you want to add to the SSH agent:" + select key in "${keys[@]}" "$(msg 'Cancelar' 'Cancel')"; do + if [[ "$REPLY" -ge 1 && "$REPLY" -le "${#keys[@]}" ]]; then + key_path="${keys[$REPLY-1]}" + break + elif [[ "$REPLY" -eq $((${#keys[@]} + 1)) ]]; then + msg "Operación cancelada." "Operation cancelled." + return 0 + else + msg "Opción inválida. Intenta de nuevo." "Invalid option. Please try again." + fi done - # Convert lifetime to seconds + read -rp "$(msg $'\n¿Tiempo de vida del agente (Ej: 5m, 2h, 1d)? ' $'\nAgent lifetime (e.g., 5m, 2h, 1d)? ')" lifetime + local num=${lifetime//[!0-9]/} local unit=${lifetime//[0-9]/} local seconds=0 + local human_lifetime="" case "$unit" in - s|"") seconds=$num ;; # default to seconds - m) seconds=$((num * 60)) ;; - h) seconds=$((num * 3600)) ;; - d) seconds=$((num * 86400)) ;; + s|"") seconds=$num; human_lifetime="$num $(msg 'segundo(s)' 'second(s)')" ;; + m) seconds=$((num * 60)); human_lifetime="$num $(msg 'minuto(s)' 'minute(s)')" ;; + h) seconds=$((num * 3600)); human_lifetime="$num $(msg 'hora(s)' 'hour(s)')" ;; + d) seconds=$((num * 86400)); human_lifetime="$num $(msg 'día(s)' 'day(s)')" ;; *) - echo "Invalid time unit. Use s, m, h, or d." + msg_err "Unidad de tiempo inválida. Usa s, m, h o d." "Invalid time unit. Use s, m, h or d." return 1 ;; esac - # Clean previous ssh credentials (rm -rf /tmp/ssh-* > /dev/null) SSH_ENV="$HOME/.ssh/environment" - printf '\e[1;36m%s\e[m\n' "Initialising new SSH agent..." + msg "\nInicializando nuevo agente SSH..." "\nInitializing new SSH agent..." ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" chmod 600 "${SSH_ENV}" # shellcheck source=/dev/null source "${SSH_ENV}" > /dev/null - if [[ ! -f "$key_path" ]]; then - printf '\e[1;31m%s\e[m\n' "SSH key not found at $key_path" - return 1 - fi - - if ssh-add -t "$seconds" "$key_path" >/dev/null 2>&1 ; then - printf '\e[1;36m%s\e[m\n' "SSH key added successfully: $key_path (lifetime: $lifetime = ${seconds}s)" + if ssh-add -t "$seconds" "$key_path" >/dev/null 2>&1; then + msg "Clave agregada correctamente: $key_path (vida: $human_lifetime)" \ + "Key added successfully: $key_path (lifetime: $human_lifetime)" else - printf '\e[1;31m%s\e[m\n' "Failed to add SSH key" + msg_err "Error al agregar la clave." "Failed to add SSH key." fi } function sshagent_stop { - - # clean previous ssh credentials (rm -rf /tmp/ssh-* > /dev/null) - ssh-agent -k > /dev/null } @@ -70,65 +74,57 @@ function sshagent_findsockets { } function sshagent_testsocket { - if [ ! -x "$(command -v ssh-add)" ] ; then - echo "ssh-add is not available; agent testing aborted" + if [ ! -x "$(command -v ssh-add)" ]; then + msg_err "ssh-add no está disponible. Cancelando prueba de socket." \ + "ssh-add is not available. Cancelling socket test." return 1 fi - if [ X"$1" != X ] ; then + if [ -n "$1" ]; then export SSH_AUTH_SOCK=$1 fi - if [ X"$SSH_AUTH_SOCK" = X ] ; then + if [ -z "$SSH_AUTH_SOCK" ]; then return 2 fi - if [ -S "$SSH_AUTH_SOCK" ] ; then + if [ -S "$SSH_AUTH_SOCK" ]; then ssh-add -l > /dev/null - if [ $? = 2 ] ; then - echo "Socket $SSH_AUTH_SOCK is dead! Deleting!" + if [ $? = 2 ]; then + msg "Socket $SSH_AUTH_SOCK está muerto. Eliminando..." \ + "Socket $SSH_AUTH_SOCK is dead. Removing..." rm -f "$SSH_AUTH_SOCK" return 4 else - echo "Found ssh-agent $SSH_AUTH_SOCK" + msg "Agente SSH encontrado en $SSH_AUTH_SOCK" \ + "Found SSH agent at $SSH_AUTH_SOCK" return 0 fi else - echo "$SSH_AUTH_SOCK is not a socket!" + msg_err "$SSH_AUTH_SOCK no es un socket válido." "$SSH_AUTH_SOCK is not a valid socket." return 3 fi } function sshagent_reload { - # ssh agent sockets can be attached to a ssh daemon process or an - # ssh-agent process. - AGENTFOUND=0 - # Attempt to find and use the ssh-agent in the current environment - if sshagent_testsocket ; then AGENTFOUND=1 ; fi + if sshagent_testsocket; then AGENTFOUND=1; fi - # If there is no agent in the environment, search /tmp for - # possible agents to reuse before starting a fresh ssh-agent - # process. - if [ $AGENTFOUND = 0 ] ; then - for agentsocket in $(sshagent_findsockets) ; do - if [ $AGENTFOUND != 0 ] ; then break ; fi - if sshagent_testsocket "$agentsocket" ; then AGENTFOUND=1 ; fi + if [ $AGENTFOUND = 0 ]; then + for agentsocket in $(sshagent_findsockets); do + if [ $AGENTFOUND != 0 ]; then break; fi + if sshagent_testsocket "$agentsocket"; then AGENTFOUND=1; fi done fi - # If at this point we still haven't located an agent, it's time to - # start a new one - if [ $AGENTFOUND = 0 ] ; then + if [ $AGENTFOUND = 0 ]; then eval "$(ssh-agent)" fi - # Clean up unset AGENTFOUND unset agentsocket - # Finally, show what keys are currently in the agent ssh-add -l } @@ -136,9 +132,7 @@ if [[ -f "$HOME/.ssh/environment" ]]; then sshagent_reload > /dev/null 2>&1 fi -# Alias agents alias sagent_start="sshagent_start" alias sagent_stop="sshagent_stop" -# Clean up not global functions unset -f sshagent_findsockets sshagent_testsocket