update ssh agent
All checks were successful
CI Pipeline / shasums (push) Successful in 20s
git-sync-with-mirror / git-sync (push) Successful in 35s
CI Pipeline / build (push) Successful in 2m36s

This commit is contained in:
Astound 2025-06-22 12:13:13 -05:00
parent 096ffbaed4
commit a3c3eb4ea0
Signed by: kaiser
GPG Key ID: 97504AF0027B1A56
2 changed files with 95 additions and 90 deletions

View File

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

View File

@ -1,68 +1,83 @@
#!/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 {
local key_path="$HOME/.ssh/id_ed25519"
local lifetime="5d"
# 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
done
# Convert lifetime to seconds
local num=${lifetime//[!0-9]/}
local unit=${lifetime//[0-9]/}
local ssh_dir="$HOME/.ssh"
local default_lifetime="5d"
local key_path=""
local lifetime="$default_lifetime"
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)) ;;
*)
echo "Invalid time unit. Use s, m, h, or d."
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" {} \;)
# 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..."
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"
local key_count=${#keys[@]}
if [ "$key_count" -eq 0 ]; then
msg_err "No se encontraron claves privadas en $ssh_dir" "No private keys found in $ssh_dir"
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
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)"
read -rp "$(msg "¿Tiempo de vida del agente? (Ej: 5m, 2h, 1d) [${default_lifetime}]: " \
"Agent lifetime? (e.g., 5m, 2h, 1d) [${default_lifetime}]: ")" user_input
lifetime="${user_input:-$default_lifetime}"
local num=${lifetime//[!0-9]/}
local unit=${lifetime//[0-9]/}
case "$unit" in
s|"") seconds=$num; human_lifetime="$(msg "$num segundo(s)" "$num second(s)")" ;;
m) seconds=$((num * 60)); human_lifetime="$(msg "$num minuto(s)" "$num minute(s)")" ;;
h) seconds=$((num * 3600)); human_lifetime="$(msg "$num hora(s)" "$num hour(s)")" ;;
d) seconds=$((num * 86400)); human_lifetime="$(msg "$num día(s)" "$num day(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
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)
rm -rf /tmp/ssh-* > /dev/null
ssh-agent -k > /dev/null
msg "Agente SSH detenido." "SSH agent stopped."
}
function sshagent_findsockets {
@ -70,65 +85,57 @@ function sshagent_findsockets {
}
function sshagent_testsocket {
if [ ! -x "$(command -v ssh-add)" ] ; then
echo "ssh-add is not available; agent testing aborted"
if ! command -v ssh-add >/dev/null; 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
export SSH_AUTH_SOCK=$1
fi
[ -n "$1" ] && export SSH_AUTH_SOCK=$1
[ -z "$SSH_AUTH_SOCK" ] && return 2
if [ X"$SSH_AUTH_SOCK" = X ] ; 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!"
rm -f "$SSH_AUTH_SOCK"
return 4
else
echo "Found ssh-agent $SSH_AUTH_SOCK"
return 0
fi
case $? in
2)
msg "Socket $SSH_AUTH_SOCK no responde. Eliminando..." \
"Socket $SSH_AUTH_SOCK is unresponsive. Removing..."
rm -f "$SSH_AUTH_SOCK"
return 4
;;
0)
msg "Agente SSH encontrado en $SSH_AUTH_SOCK" \
"Found SSH agent at $SSH_AUTH_SOCK"
return 0
;;
esac
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.
local AGENTFOUND=0
AGENTFOUND=0
if sshagent_testsocket; then AGENTFOUND=1; fi
# Attempt to find and use the ssh-agent in the current environment
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 -eq 0 ]; then
for agentsocket in $(sshagent_findsockets); do
if sshagent_testsocket "$agentsocket"; then
AGENTFOUND=1
break
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 -eq 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 +143,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