Return to Snippet

Revision: 7565
at July 30, 2008 04:10 by narkisr


Initial Code
if [ -d ~/.ssh ]; then

    # Touch files we expect to exist
    if [ ! -e ~/.ssh/config ]; then touch ~/.ssh/config; fi
    if [ ! -e ~/.ssh/known_hosts ]; then touch ~/.ssh/known_hosts; fi

    # Parse ~/.ssh/known_hosts and ~/.ssh/config to find hosts
    for x in `sed -e ’s/[, ].*//’ ~/.ssh/known_hosts; awk ‘/^Host [^*?]+$/{print $2}’ ~/.ssh/config`; do

        # Don’t override commands
        type $x > /dev/null 2>&1 && continue
        alias $x=”ssh $x”

        # Remove the domainname
        y=${x/.*/}
        if [ "$y" != "$x" ]; then
            if ! type $y > /dev/null 2>&1; then
                alias $y=”ssh $x”
            fi
        fi

        # Remove prefix. Add prefixes you want removed here
        y=${y/pf1-/}
        y=${y/pf2-/}
        if [ "$y" != "$x" ]; then
            if ! type $y > /dev/null 2>&1; then
                alias $y=”ssh $x”
            fi
        fi
    done
fi

Initial URL


Initial Description
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/).

Initial Title
Auto aliasing of ssh hosts

Initial Tags


Initial Language
Bash