update ssh agent
This commit is contained in:
parent
096ffbaed4
commit
03809b7e5d
@ -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
|
26f1e5868a01ae400be225dda3633a0fd494560d9cf472d78b98aa3edc0dc5ab5d75de3de5f2a89bb6537a8a63024edd02d6073d80c8cc50ba64334a9f5cdd37 ./tools/ssh-agent.sh
|
||||||
|
@ -4,64 +4,68 @@
|
|||||||
# 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=""
|
||||||
|
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
|
||||||
esac
|
|
||||||
|
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
|
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 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,46 +75,42 @@ function sshagent_findsockets {
|
|||||||
|
|
||||||
function sshagent_testsocket {
|
function sshagent_testsocket {
|
||||||
if [ ! -x "$(command -v ssh-add)" ]; then
|
if [ ! -x "$(command -v ssh-add)" ]; 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
|
if [ -n "$1" ]; then
|
||||||
export SSH_AUTH_SOCK=$1
|
export SSH_AUTH_SOCK=$1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ X"$SSH_AUTH_SOCK" = X ] ; then
|
if [ -z "$SSH_AUTH_SOCK" ]; then
|
||||||
return 2
|
return 2
|
||||||
fi
|
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
|
if [ $? = 2 ]; then
|
||||||
echo "Socket $SSH_AUTH_SOCK is dead! Deleting!"
|
msg "Socket $SSH_AUTH_SOCK está muerto. Eliminando..." \
|
||||||
|
"Socket $SSH_AUTH_SOCK is dead. Removing..."
|
||||||
rm -f "$SSH_AUTH_SOCK"
|
rm -f "$SSH_AUTH_SOCK"
|
||||||
return 4
|
return 4
|
||||||
else
|
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
|
fi
|
||||||
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
|
|
||||||
# ssh-agent process.
|
|
||||||
|
|
||||||
AGENTFOUND=0
|
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 +118,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 +132,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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user