zshrc: piv-agent now can re-use agents

This commit is contained in:
xiongdian.me 2023-01-21 15:10:43 +08:00
parent 0a602e5260
commit a4dca13893

40
.zshrc2
View File

@ -113,21 +113,41 @@ fi
gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX } gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX }
piv-agent() piv-agent()
{ {
eval $(ssh-agent -k) local agent_file="/tmp/piv-agent-$(whoami)"
if [[ -f $agent_file ]]; then
source $agent_file
fi
# they are: ubuntu, macos, respectively # they are: ubuntu, macos, respectively
local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" ) local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" )
for i in ${SO_PATHS[*]}; do local SO_FILE
if [[ -f "$i" ]]; then for SO_FILE in ${SO_PATHS[*]}; do
echo "using: $i" if [[ -f "$SO_FILE" ]]; then
eval $(ssh-agent -P "$i") break
ssh-add -s "$i"
echo "now available keys:"
ssh-add -l
return 0
fi fi
done done
echo "opensc-pkcs11.so not found" if [[ ! -f "$SO_FILE" ]]; then
echo "error: opensc-pkcs11.so not found"
return 1 return 1
fi
if ! ps -p "$SSH_AGENT_PID" > /dev/null; then
echo "launch agent using: $SO_FILE"
ret=$(ssh-agent -P "$SO_FILE")
echo "$ret" > $agent_file
eval "$ret"
if ! ps -p $SSH_AGENT_PID > /dev/null; then
echo "failed to launch agent"
return 1
fi
else
echo "using existing agent: $SSH_AGENT_PID"
fi
if ! ssh-add -l > /dev/null; then
echo "no keys found in this agent, let's add some"
ssh-add -s "$SO_FILE"
fi
pgrep ssh-agent | grep -v $SSH_AGENT_PID | xargs kill -9
echo "now available keys:"
ssh-add -l
} }
# key bindings # key bindings