Auto aliasing of ssh hosts


/ Published in: Bash
Save to your folder(s)

A bash script that auto register ssh aliases of known hosts (as seen at http://blogs.ubuntu-nl.org/dennis/2008/07/29/traincoding-ssh-aliases/).


Copy this code and paste it in your HTML
  1. if [ -d ~/.ssh ]; then
  2.  
  3. # Touch files we expect to exist
  4. if [ ! -e ~/.ssh/config ]; then touch ~/.ssh/config; fi
  5. if [ ! -e ~/.ssh/known_hosts ]; then touch ~/.ssh/known_hosts; fi
  6.  
  7. # Parse ~/.ssh/known_hosts and ~/.ssh/config to find hosts
  8. for x in `sed -e ’s/[, ].*//’ ~/.ssh/known_hosts; awk ‘/^Host [^*?]+$/{print $2}’ ~/.ssh/config`; do
  9.  
  10. # Don’t override commands
  11. type $x > /dev/null 2>&1 && continue
  12. alias $x=”ssh $x”
  13.  
  14. # Remove the domainname
  15. y=${x/.*/}
  16. if [ "$y" != "$x" ]; then
  17. if ! type $y > /dev/null 2>&1; then
  18. alias $y=”ssh $x”
  19. fi
  20. fi
  21.  
  22. # Remove prefix. Add prefixes you want removed here
  23. y=${y/pf1-/}
  24. y=${y/pf2-/}
  25. if [ "$y" != "$x" ]; then
  26. if ! type $y > /dev/null 2>&1; then
  27. alias $y=”ssh $x”
  28. fi
  29. fi
  30. done
  31. fi

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.