Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. # add to .bashrc
  2.  
  3. # Set up ssh-agent
  4. SSH_ENV="$HOME/.ssh/environment"
  5.  
  6. function start_ssh_agent {
  7. echo "Initializing new SSH agent..."
  8. touch $SSH_ENV
  9. chmod 600 "${SSH_ENV}"
  10. /usr/bin/ssh-agent | sed 's/^echo/#echo/' >> "${SSH_ENV}"
  11. . "${SSH_ENV}" > /dev/null
  12. # the next line can be removed if you don't want to be prompted for a passphrase start
  13. /usr/bin/ssh-add
  14. }
  15.  
  16. function kill_ssh_agent {
  17. bash_count=$(pgrep -c bash)
  18. if [ $bash_count -gt 2 ]; then
  19. # don't kill agent when there's still a bash shell running;
  20. # because this function will be executed inside a bash shell,
  21. # pgrep will find *2* bash processes when exiting the last real bash
  22. true
  23. else
  24. # do kill agent since this is the last bash
  25. eval $(ssh-agent -k)
  26. rm "${SSH_ENV}"
  27. fi
  28. }
  29.  
  30. # Source SSH settings, if applicable
  31. if [ -f "${SSH_ENV}" ]; then
  32. . "${SSH_ENV}" > /dev/null
  33. kill -0 $SSH_AGENT_PID 2>/dev/null || {
  34. start_ssh_agent
  35. }
  36. else
  37. start_ssh_agent
  38. fi
  39.  
  40. trap kill_ssh_agent EXIT
Add Comment
Please, Sign In to add comment