sshagent 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #### My (demuredemeanor) sub source ssh-agent script
  2. # Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
  3. # https://notabug.org/demure/dotfiles/
  4. # legacy repo http://github.com/demure/dotfiles
  5. # vim:set syntax=sh:
  6. ## Add check for key exists first
  7. ## Add check -> Y/N for ssh_env existing? might not need, if using __ssh_agent right
  8. ## Re-add root/sudo check now that logname is dead?
  9. ## Note: logname is dead https://unix.stackexchange.com/questions/347889/a-proper-replacement-for-the-logname-utility
  10. ## SSH Agent
  11. function __ssh_agent() {
  12. local SSH_ENV="${HOME}/.ssh/environment"
  13. function __start_agent {
  14. echo "Initialising new SSH agent..."
  15. /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
  16. echo succeeded
  17. chmod 600 "${SSH_ENV}"
  18. . "${SSH_ENV}" > /dev/null
  19. /usr/bin/ssh-add;
  20. }
  21. ## Source SSH settings, if applicable
  22. if [ -f "${SSH_ENV}" ]; then
  23. . "${SSH_ENV}" > /dev/null
  24. ps -ef 2>/dev/null | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
  25. pstree -up ${USER} 2>/dev/null | grep ${SSH_AGENT_PID} | grep ^ssh-agent > /dev/null || {
  26. __start_agent;
  27. }
  28. }
  29. else
  30. __start_agent;
  31. fi
  32. }