From 6fff4075d94a87997dba4539446ef696a36278cd Mon Sep 17 00:00:00 2001 From: Jesus Date: Sun, 18 May 2025 17:08:38 -0500 Subject: update --- hyperterm/tools/ssh-agent.sh | 47 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'hyperterm/tools/ssh-agent.sh') diff --git a/hyperterm/tools/ssh-agent.sh b/hyperterm/tools/ssh-agent.sh index 1012d81..3db6418 100644 --- a/hyperterm/tools/ssh-agent.sh +++ b/hyperterm/tools/ssh-agent.sh @@ -4,8 +4,38 @@ # SSH-AGENT #------------ function sshagent_start { - - # clean previous ssh credentials + 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 seconds=0 + + 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 + + # Clean previous ssh credentials (rm -rf /tmp/ssh-* > /dev/null) SSH_ENV="$HOME/.ssh/environment" @@ -15,11 +45,16 @@ function sshagent_start { # shellcheck source=/dev/null source "${SSH_ENV}" > /dev/null - # Set the default time to 5 days if not provided - local days="${1:-5d}" + if [[ ! -f "$key_path" ]]; then + printf '\e[1;31m%s\e[m\n' "SSH key not found at $key_path" + return 1 + fi - ssh-add -t "$days" - printf '\e[1;36m%s\e[m\n' "succeeded" + 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)" + else + printf '\e[1;31m%s\e[m\n' "Failed to add SSH key" + fi } function sshagent_stop { -- cgit v1.2.3