update ssh agent
All checks were successful
CI Pipeline / shasums (push) Successful in 23s
git-sync-with-mirror / git-sync (push) Successful in 39s
CI Pipeline / build (push) Successful in 3m13s

This commit is contained in:
Astound 2025-06-21 23:51:04 -05:00
parent 096ffbaed4
commit 94b7a71463
Signed by: kaiser
GPG Key ID: 97504AF0027B1A56
2 changed files with 70 additions and 66 deletions

View File

@ -25,4 +25,4 @@ f760432c3d76befad30588299eb2d1412d77b22fd850ffbd840c72123885d4e916a7e0b16e7048c5
fab9d339a99c7d2e1809d1c44f533523c6bfcdcc8d63c62b335ce7d4c666c8bdd7ac319316bf71f043163a3a0184e25ecfe1ee32724627424d042a05fa80ce77 ./tools/vconverter.sh fab9d339a99c7d2e1809d1c44f533523c6bfcdcc8d63c62b335ce7d4c666c8bdd7ac319316bf71f043163a3a0184e25ecfe1ee32724627424d042a05fa80ce77 ./tools/vconverter.sh
ee1d6d1f9b010318985f7154c2a9173c8f2ab6b637cd3c8d2a9b403e83470e15a273dcff326a84f035660807d7cfcf04efe5abc0495e25ae7339b8807899cf0d ./tools/listuser.sh ee1d6d1f9b010318985f7154c2a9173c8f2ab6b637cd3c8d2a9b403e83470e15a273dcff326a84f035660807d7cfcf04efe5abc0495e25ae7339b8807899cf0d ./tools/listuser.sh
243e3a076f1696bde1e464b479e221876177eb98c92415a09de8dc9e8d138e88e006eb9fa441ca1ab19d260cb3fd4de82dc54feae73453e229c3a8fdab3043f0 ./tools/virtualenv.sh 243e3a076f1696bde1e464b479e221876177eb98c92415a09de8dc9e8d138e88e006eb9fa441ca1ab19d260cb3fd4de82dc54feae73453e229c3a8fdab3043f0 ./tools/virtualenv.sh
1ba63accea347b96c30fcd4a2fa84c531836b082ed5ced035a8f30a0d738724f8f171ec85645779e682c8d0aa1d5f6c5b32e182454cdb8f0c85dde08a37a96ca ./tools/ssh-agent.sh 23bd34194c0d3df632957960191ee537e11f5e4915b563b375a70f4e759e79ea3ccb4e95f3f3a24b39f5c2e377cf6a1729a93e9954adf756c7de7532e737f0f1 ./tools/ssh-agent.sh

View File

@ -4,64 +4,76 @@
# SSH-AGENT # SSH-AGENT
#------------ #------------
function sshagent_start { function sshagent_start {
local key_path="$HOME/.ssh/id_ed25519" local ssh_dir="$HOME/.ssh"
local lifetime="5d" local lifetime="5d"
local key_path=""
# Parse options msg "Buscando claves privadas en $ssh_dir..." "Looking for private keys in $ssh_dir..."
while getopts "t:k:" opt; do mapfile -t keys < <(find "$ssh_dir" -type f -not -name "*.pub" -exec grep -l "PRIVATE KEY" {} \;)
case "$opt" in
t) lifetime="$OPTARG" ;; if [ "${#keys[@]}" -eq 0 ]; then
k) key_path="$OPTARG" ;; msg_err "No se encontraron claves privadas en $ssh_dir" "No private keys found in $ssh_dir"
*)
echo "Usage: sagent_start [-t lifetime] [-k key_path]"
return 1 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
case "$REPLY" in
''|*[!0-9]*)
msg "Entrada no válida. Solo números." "Invalid input. Numbers only."
;;
*)
if (( REPLY >= 1 && REPLY <= ${#keys[@]} )); then
key_path="$key"
break
elif (( REPLY == ${#keys[@]} + 1 )); then
msg "Operación cancelada." "Operation cancelled."
return 0
else
msg "Opción fuera de rango." "Option out of range."
fi
;; ;;
esac esac
done done
# Convert lifetime to seconds read -rp "$(msg $'\n¿Tiempo de vida del agente? (Ej: 5m, 2h, 1d) [5d]: ' $'\nAgent lifetime? (e.g., 5m, 2h, 1d) [5d]: ')" user_input
lifetime="${user_input:-5d}"
local num=${lifetime//[!0-9]/} local num=${lifetime//[!0-9]/}
local unit=${lifetime//[0-9]/} local unit=${lifetime//[0-9]/}
local seconds=0 local seconds=0
local human_lifetime=""
case "$unit" in case "$unit" in
s|"") seconds=$num ;; # default to seconds s|"") seconds=$num; human_lifetime="$num $(msg 'segundo(s)' 'second(s)')" ;;
m) seconds=$((num * 60)) ;; m) seconds=$((num * 60)); human_lifetime="$num $(msg 'minuto(s)' 'minute(s)')" ;;
h) seconds=$((num * 3600)) ;; h) seconds=$((num * 3600)); human_lifetime="$num $(msg 'hora(s)' 'hour(s)')" ;;
d) seconds=$((num * 86400)) ;; 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 return 1
;; ;;
esac esac
# Clean previous ssh credentials
(rm -rf /tmp/ssh-* > /dev/null) (rm -rf /tmp/ssh-* > /dev/null)
SSH_ENV="$HOME/.ssh/environment" 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}" ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
chmod 600 "${SSH_ENV}" chmod 600 "${SSH_ENV}"
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "${SSH_ENV}" > /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 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)" msg "Clave agregada correctamente: $key_path (vida: $human_lifetime)" \
"Key added successfully: $key_path (lifetime: $human_lifetime)"
else 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 fi
} }
function sshagent_stop { function sshagent_stop {
# clean previous ssh credentials
(rm -rf /tmp/ssh-* > /dev/null) (rm -rf /tmp/ssh-* > /dev/null)
ssh-agent -k > /dev/null ssh-agent -k > /dev/null
} }
@ -70,47 +82,45 @@ function sshagent_findsockets {
} }
function sshagent_testsocket { function sshagent_testsocket {
if [ ! -x "$(command -v ssh-add)" ] ; then if ! command -v ssh-add >/dev/null; then
echo "ssh-add is not available; agent testing aborted" msg_err "ssh-add no está disponible. Cancelando prueba de socket." \
"ssh-add is not available. Cancelling socket test."
return 1 return 1
fi fi
if [ X"$1" != X ] ; then case "$1" in
export SSH_AUTH_SOCK=$1 "") ;;
fi *) export SSH_AUTH_SOCK=$1 ;;
esac
if [ X"$SSH_AUTH_SOCK" = X ] ; then if [ -z "$SSH_AUTH_SOCK" ]; then return 2; fi
return 2
fi
if [ -S "$SSH_AUTH_SOCK" ]; then if [ -S "$SSH_AUTH_SOCK" ]; then
ssh-add -l > /dev/null ssh-add -l > /dev/null
if [ $? = 2 ] ; then case $? in
echo "Socket $SSH_AUTH_SOCK is dead! Deleting!" 2)
msg "Socket $SSH_AUTH_SOCK no responde. Eliminando..." \
"Socket $SSH_AUTH_SOCK is unresponsive. Removing..."
rm -f "$SSH_AUTH_SOCK" rm -f "$SSH_AUTH_SOCK"
return 4 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 return 0
fi ;;
esac
else 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 return 3
fi fi
} }
function sshagent_reload { function sshagent_reload {
# ssh agent sockets can be attached to a ssh daemon process or an local AGENTFOUND=0
# 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 if [ $AGENTFOUND = 0 ]; then
for agentsocket in $(sshagent_findsockets); do for agentsocket in $(sshagent_findsockets); do
if [ $AGENTFOUND != 0 ]; then break; fi if [ $AGENTFOUND != 0 ]; then break; fi
@ -118,17 +128,13 @@ function sshagent_reload {
done done
fi 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)" eval "$(ssh-agent)"
fi fi
# Clean up
unset AGENTFOUND unset AGENTFOUND
unset agentsocket unset agentsocket
# Finally, show what keys are currently in the agent
ssh-add -l ssh-add -l
} }
@ -136,9 +142,7 @@ if [[ -f "$HOME/.ssh/environment" ]]; then
sshagent_reload > /dev/null 2>&1 sshagent_reload > /dev/null 2>&1
fi fi
# Alias agents
alias sagent_start="sshagent_start" alias sagent_start="sshagent_start"
alias sagent_stop="sshagent_stop" alias sagent_stop="sshagent_stop"
# Clean up not global functions
unset -f sshagent_findsockets sshagent_testsocket unset -f sshagent_findsockets sshagent_testsocket