Good day, I have read through the similar topics all day long but unfortunately could not find the answer suitable to my situation. So here is what I'm trying to do. I have the shell script with quite a few functions and one of the function supposes to call ssh-agent:
sh_agent_run () {
case "$(pidof ssh-agent | wc -w)" in
0) echo "SSH agent is not running. Startting SSH agent."
eval `ssh-agent -s`
ssh-add ${ssh_key}
;;
1) echo "SSH agent is running. Nothing to do."
;;
*) echo "Too much instances of SSH agent is running. Stopping SSH agent instances and running just one"
while pidof ssh-agent; do
echo "Stopping ssh-agent..."
killall -9 ssh-agent
sleep 1
done
echo "Starting valid SSH agent instance"
eval `ssh-agent -s`
ssh-add ${ssh_key}
;;
esac
}
The output says:
[root@centos versions]# ./ssh_tunnels.sh -sr SSH agent is not running. Startting SSH agent. Identity added: (path to ssh key)
But when I'm trying to connect to the ssh-agent and check the key with ssh-add -L command it says:
[root@centos versions]# ssh-add -l Could not open a connection to your authentication agent.
Could somebody please help me to adjust my function so I can build it into my script and use? This is critical not to run ssh-agent via .bashrc, I need to bale to manage ssh-agent via this script (start, stop, status, etc.)
Thank you in advance