Fish shell或bash shell下的ssh-agent现有会话探测脚本

来自三线的随记
Admin讨论 | 贡献2022年6月9日 (四) 22:51的版本

一个随便糊的用于发现设备上现有的ssh-agent会话脚本,方便在不同的terminal tab中复用一个ssh-agent

适配: macOS + fish shell

#!/usr/bin/env fish

#######################################################
# Author: SanXian
# To find the exists ssh-agent connection or create
# a new ssh-agent connection for macOS with fish shell
#######################################################

#set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
set base_dir $TMPDIR

for i in (sh -c "ls -1 --color=never $base_dir/ssh-*/agent* 2>/dev/null")
  set agent_file (echo -n $i | grep --color=never -Eo "agent.*")
  echo "found: $agent_file"
  set PID (echo -n $agent_file | awk -F . '{print $2 + 1}')
  echo PID: $PID
  export  SSH_AGENT_PID=$PID
  export  SSH_AUTH_SOCK=$i
  env | grep -i ssh_
  exit
end

# ssh agent sock not found
echo "ssh agent sock file not found, creating"
eval (ssh-agent -c -t 4h)


Linux bash:Linux bash: