Skip to content

Instantly share code, notes, and snippets.

@njuaplusplus
Forked from bradland/ssh-known-hosts-mgmt.sh
Created September 23, 2015 05:41
Show Gist options
  • Save njuaplusplus/a0254e6589fd159edef1 to your computer and use it in GitHub Desktop.
Save njuaplusplus/a0254e6589fd159edef1 to your computer and use it in GitHub Desktop.

Revisions

  1. Brad Landers revised this gist Oct 26, 2011. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions ssh-known-hosts-mgmt.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    # This is a short collection of tools that are useful for managing your
    # known_hosts file. In this case, I'm using the '-f' flag to specify the
    # global known_hosts file because I'll be adding many deploy users on this
    @@ -21,10 +20,11 @@ ssh-keygen -f /etc/ssh/ssh_known_hosts -R github.com
    rm /etc/ssh/ssh_known_hosts.old

    # Wipe all known_hosts files
    rm ~/.ssh/known_hosts
    rm ~/.ssh/known_hosts.old
    rm /etc/ssh/ssh_known_hosts
    rm /etc/ssh/ssh_known_hosts.old
    if [ -e ~/.ssh/known_hosts ]; then rm ~/.ssh/known_hosts; fi
    if [ -e ~/.ssh/known_hosts ]; then rm ~/.ssh/known_hosts; fi
    if [ -e ~/.ssh/known_hosts.old ]; then rm ~/.ssh/known_hosts.old; fi
    if [ -e /etc/ssh/ssh_known_hosts ]; then rm /etc/ssh/ssh_known_hosts; fi
    if [ -e /etc/ssh/ssh_known_hosts.old ]; then rm /etc/ssh/ssh_known_hosts.old; fi

    # Show last return code; useful for testing the grep example
    echo $?
  2. Brad Landers created this gist Oct 26, 2011.
    30 changes: 30 additions & 0 deletions ssh-known-hosts-mgmt.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@

    # This is a short collection of tools that are useful for managing your
    # known_hosts file. In this case, I'm using the '-f' flag to specify the
    # global known_hosts file because I'll be adding many deploy users on this
    # system. Simply omit the -f flag to operate on ~/.ssh/known_hosts

    # Add entry for host
    ssh-keyscan -H github.com > /etc/ssh/ssh_known_hosts

    # Scan known hosts
    ssh-keygen -f /etc/ssh/ssh_known_hosts -H -F github.com

    # Scan known hosts and grep (return code is 0 if matched; 1 if not matched)
    ssh-keygen -f /etc/ssh/ssh_known_hosts -H -F github.com | grep 'github.com'

    # Count matches for host
    ssh-keygen -f /etc/ssh/ssh_known_hosts -H -F github.com | wc -l

    # Remove entry for host
    ssh-keygen -f /etc/ssh/ssh_known_hosts -R github.com
    rm /etc/ssh/ssh_known_hosts.old

    # Wipe all known_hosts files
    rm ~/.ssh/known_hosts
    rm ~/.ssh/known_hosts.old
    rm /etc/ssh/ssh_known_hosts
    rm /etc/ssh/ssh_known_hosts.old

    # Show last return code; useful for testing the grep example
    echo $?