删除~/.ssh/known_hosts相应ip小工具

1、背景

因为工作原因,用的mac和Linux比较多,这两个系统都自带终端。好处就是ssh可以直接执行连接,但是开发环境是几个虚拟机,经常的重装,导致我的hostkey经常需要到~/.ssh/known_hosts文件下删除相应的ip,比如下面:

截屏2020-05-06 下午10.26.55.png

当known_hosts文件有十几二十几行的时候vim打开查找已经是比较麻烦的事情了。身为程序员当然要写个脚本来代替自己去删除了。

2、脚本内容

#!/bin/bash

echo "--------删除~/.ssh/known_hosts相应ip小工具--------"
KNOWN_PATH=~/.ssh/known_hosts
read -p "请输入你想删除的IP:
" ip
function get_line() {
    sed -n -e '/'$1'/=' $KNOWN_PATH
}
function get_context() {
    sed -n $1'p' $KNOWN_PATH
}
function del_all() {
    if [ "$(uname)" == "Darwin" ];then        # Mac OS X 操作系统
        sed -i "" '/'$1'/d' $KNOWN_PATH
    else                                    # 非Mac OS X默认为Linux
        sed -i '/'$1'/d' $KNOWN_PATH
    fi
}
function del_one() {
    if [ "$(uname)" == "Darwin" ];then        # Mac OS X 操作系统
        sed -i "" $1'd' $KNOWN_PATH
    else                                    # 非Mac OS X默认为Linux
        sed -i $1'd' $KNOWN_PATH
    fi
}
lines=$(get_line $ip)
if [ ! "$lines" ];then
    echo "没有找到该IP!"
    exit
fi
for i in $lines
do
    echo '查找到第 '$i' 行存在该IP,内容如下:'
    echo $(get_context $i)
done
read -p '请输入要删除的ip的行号 或者 输入all来删除所有匹配到的行:
' del_line
if [ ! "$del_line" ];then
    echo "输入不能为空!"
    exit
elif [ "$del_line" == "all" ];then
    del_all $ip
    if [ $? -eq 0 ]; then
        echo "success"
    else
        echo "fail"
        exit
    fi
else
    del_one $del_line
    if [ $? -eq 0 ]; then
        echo "success"
    else
        echo "fail"
        exit
    fi
fi
echo '0_0删除成功'

运行展示:

截屏2020-05-06 下午10.30.15.png

git@github.com$ ssh -T -vvv git@github.com OpenSSH_9.9p1, OpenSSL 3.2.4 11 Feb 2025 debug1: Reading configuration data /c/Users/SW0122/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/c/Users/SW0122/.ssh/known_hosts' debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/c/Users/SW0122/.ssh/known_hosts2' debug2: resolving "github.com" port 22 debug3: resolve_host: lookup github.com:22 debug3: channel_clear_timeouts: clearing debug3: ssh_connect_direct: entering debug1: Connecting to github.com [20.205.243.166] port 22. debug3: set_sock_tos: set socket 4 IP_TOS 0x48 debug1: Connection established. debug1: identity file /c/Users/SW0122/.ssh/id_rsa type 0 debug1: identity file /c/Users/SW0122/.ssh/id_rsa-cert type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ecdsa type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ecdsa-cert type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ecdsa_sk type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ed25519 type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ed25519-cert type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ed25519_sk type -1 debug1: identity file /c/Users/SW0122/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /c/Users/SW0122/.ssh/id_xmss type -1 debug1: identity file /c/Users/SW0122/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_9.9 debug1: Remote protocol version 2.0, remote software version 6fdec156f debug1: compat_banner: no match: 6fdec156f debug2: fd 4 setting O_NONBLOCK debug1: Authenticating to github.com:22 as 'git' debug3: record_hostkey: found key type ED25519 in file /c/Users/SW0122/.ssh/known_hosts:1 debug3: load_hostkeys_file: loaded 1 keys from github.com debug1: load_hostkeys: fopen /c/Users/SW0122/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /e: Permission denied (publickey).
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值