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

This commit is contained in:
Astound 2025-06-22 11:55:13 -05:00
parent 096ffbaed4
commit d4f83b9ac1
Signed by: kaiser
GPG Key ID: 97504AF0027B1A56
2 changed files with 94 additions and 90 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 1b5e0607990b07355ff851ad049066de4f6143333203153911cdd44092fa3934923988c36dcd589f73e820974f2c4e84fd61feead223ba71d23fde3540162b26 ./tools/ssh-agent.sh

View File

@ -1,68 +1,82 @@
#!/bin/bash #!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# This script manages the SSH agent, allowing you to start, stop, and reload it.
# Require script: core/lamguages.sh
# Usage:
# sagent_start - Start the SSH agent and add a private key.
# sagent_stop - Stop the SSH agent.
# sagent_reload - Reload the SSH agent and list added keys.
#------------
# SSH-AGENT
#------------
function sshagent_start { function sshagent_start {
local key_path="$HOME/.ssh/id_ed25519" local ssh_dir="$HOME/.ssh"
local lifetime="5d" local default_lifetime="5d"
local key_path=""
# Parse options local lifetime="$default_lifetime"
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
done
# Convert lifetime to seconds
local num=${lifetime//[!0-9]/}
local unit=${lifetime//[0-9]/}
local seconds=0 local seconds=0
local human_lifetime=""
case "$unit" in msg "Buscando claves privadas en $ssh_dir..." "Looking for private keys in $ssh_dir..."
s|"") seconds=$num ;; # default to seconds mapfile -t keys < <(find "$ssh_dir" -type f -not -name "*.pub" -exec grep -l "PRIVATE KEY" {} \;)
m) seconds=$((num * 60)) ;;
h) seconds=$((num * 3600)) ;;
d) seconds=$((num * 86400)) ;;
*)
echo "Invalid time unit. Use s, m, h, or d."
return 1
;;
esac
# Clean previous ssh credentials local key_count=${#keys[@]}
(rm -rf /tmp/ssh-* > /dev/null) if [ "$key_count" -eq 0 ]; then
msg_err "No se encontraron claves privadas en $ssh_dir" "No private keys found in $ssh_dir"
SSH_ENV="$HOME/.ssh/environment"
printf '\e[1;36m%s\e[m\n' "Initialising 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 return 1
elif [ "$key_count" -eq 1 ]; then
key_path="${keys[0]}"
msg "Una sola clave encontrada: $key_path" "Single key found: $key_path"
else
msg "Selecciona la clave que deseas agregar al agente SSH:" "Select the key you want to add to the SSH agent:"
select key in "${keys[@]}" "$(msg 'Cancelar' 'Cancel')"; do
if [[ "$REPLY" =~ ^[0-9]+$ ]]; then
if (( REPLY >= 1 && REPLY <= key_count )); then
key_path="$key"
break
elif (( REPLY == key_count + 1 )); then
msg "Operación cancelada." "Operation cancelled."
return 0
else
msg "Opción fuera de rango." "Option out of range."
fi
else
msg "Entrada no válida. Solo números." "Invalid input. Numbers only."
fi
done
fi fi
if ssh-add -t "$seconds" "$key_path" >/dev/null 2>&1 ; then read -rp "$(msg $'\n¿Tiempo de vida del agente? (Ej: 5m, 2h, 1d) [5d]: ' $'\nAgent lifetime? (e.g., 5m, 2h, 1d) [5d]: ')" user_input
printf '\e[1;36m%s\e[m\n' "SSH key added successfully: $key_path (lifetime: $lifetime = ${seconds}s)" lifetime="${user_input:-$default_lifetime}"
local num=${lifetime//[!0-9]/}
local unit=${lifetime//[0-9]/}
case "$unit" in
s|"") seconds=$num; human_lifetime="$num segundo(s)" ;;
m) seconds=$((num * 60)); human_lifetime="$num minuto(s)" ;;
h) seconds=$((num * 3600)); human_lifetime="$num hora(s)" ;;
d) seconds=$((num * 86400)); human_lifetime="$num día(s)" ;;
*) 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
rm -rf /tmp/ssh-* > /dev/null
SSH_ENV="$HOME/.ssh/environment"
msg "Inicializando nuevo agente SSH..." "Initializing 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 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 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 {
rm -rf /tmp/ssh-* > /dev/null
# clean previous ssh credentials
(rm -rf /tmp/ssh-* > /dev/null)
ssh-agent -k > /dev/null ssh-agent -k > /dev/null
msg "Agente SSH detenido." "SSH agent stopped."
} }
function sshagent_findsockets { function sshagent_findsockets {
@ -70,65 +84,57 @@ 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 [ -n "$1" ] && export SSH_AUTH_SOCK=$1
export SSH_AUTH_SOCK=$1 [ -z "$SSH_AUTH_SOCK" ] && return 2
fi
if [ X"$SSH_AUTH_SOCK" = X ] ; then if [ -S "$SSH_AUTH_SOCK" ]; then
return 2
fi
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)
rm -f "$SSH_AUTH_SOCK" msg "Socket $SSH_AUTH_SOCK no responde. Eliminando..." \
return 4 "Socket $SSH_AUTH_SOCK is unresponsive. Removing..."
else rm -f "$SSH_AUTH_SOCK"
echo "Found ssh-agent $SSH_AUTH_SOCK" return 4
return 0 ;;
fi 0)
msg "Agente SSH encontrado en $SSH_AUTH_SOCK" \
"Found SSH agent at $SSH_AUTH_SOCK"
return 0
;;
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 if sshagent_testsocket; then AGENTFOUND=1; fi
# Attempt to find and use the ssh-agent in the current environment if [ $AGENTFOUND -eq 0 ]; then
if sshagent_testsocket ; then AGENTFOUND=1 ; fi for agentsocket in $(sshagent_findsockets); do
if sshagent_testsocket "$agentsocket"; then
# If there is no agent in the environment, search /tmp for AGENTFOUND=1
# possible agents to reuse before starting a fresh ssh-agent break
# process. 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 done
fi fi
# If at this point we still haven't located an agent, it's time to if [ $AGENTFOUND -eq 0 ]; then
# start a new one
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