Skip to content

Instantly share code, notes, and snippets.

@kfox
Created February 8, 2013 15:45
Show Gist options
  • Select an option

  • Save kfox/4739828 to your computer and use it in GitHub Desktop.

Select an option

Save kfox/4739828 to your computer and use it in GitHub Desktop.

Revisions

  1. kfox created this gist Feb 8, 2013.
    19 changes: 19 additions & 0 deletions forget.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # forget.sh: remove matching lines from ~/.ssh/known_hosts
    # usage: forget <hostname|IP|substring> [...]

    function forget {
    known_hosts=~/.ssh/known_hosts
    for host in $*
    do
    host=$(echo "${host}" | sed -e 's/^ssh:\/\///' -e 's/^.*@//')
    line=$(awk '{ print $1 }' ${known_hosts} | grep -n "${host}" | sed 's/:.*$//g' | xargs)
    while [ -n "${line}" ]
    do
    l=$(echo $line | awk '{ print $1 }')
    echo -n "==> removing from ${known_hosts}: "
    awk '{ if (NR==n) print $1 }' n=$l ${known_hosts}
    sed -i '.old' "${l}d" ${known_hosts}
    line=$(awk '{ print $1 }' ${known_hosts} | grep -n "${host}" | sed 's/:.*$//g' | xargs)
    done
    done
    }