diff options
author | Jesus <heckyel@hyperbola.info> | 2025-05-18 17:08:38 -0500 |
---|---|---|
committer | Jesus <heckyel@hyperbola.info> | 2025-05-18 17:08:38 -0500 |
commit | 6fff4075d94a87997dba4539446ef696a36278cd (patch) | |
tree | 78cc4188bfc30552e1579f10f2e1fe54eb13d441 /hyperterm/tools/ssh-agent.sh | |
parent | cc476600113c04cc8c2480417f36c61031840e31 (diff) | |
download | hyperterm-6fff4075d94a87997dba4539446ef696a36278cd.tar.lz hyperterm-6fff4075d94a87997dba4539446ef696a36278cd.tar.xz hyperterm-6fff4075d94a87997dba4539446ef696a36278cd.zip |
update
Diffstat (limited to 'hyperterm/tools/ssh-agent.sh')
-rw-r--r-- | hyperterm/tools/ssh-agent.sh | 47 |
1 files changed, 41 insertions, 6 deletions
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 { |