根据IP批量生成信息追加到~/.ssh/knows_hosts文件

本文是获取ip_list的ip列表,然后判断是否已存在于known_hosts中,如果不存在,则追加。

#!/bin/bash
#定义knows_hosts所在目录
SSH_DIR=~/.ssh

SCRIPT_PREFIX=./tmp
TMP_SCRIPT=$SCRIPT_PREFIX.sh

for ip in $(cat ip_list)  
do
#不相等,则IP不为空
    if [ "x$ip" != "x" ]; then
        echo -------------------------
        echo "x$ip","x"
        TMP_SCRIPT=${SCRIPT_PREFIX}.$ip.sh
        echo $TMP_SCRIPT
        # check known_hosts
        #判断IP是否已存在known_hosts
        val=`ssh-keygen -F $ip`
        #相等则IP不存在于known_hosts
        if [ "x$val" == "x" ]; then
            echo "$ip not in $SSH_DIR/known_hosts, need to add"
            val=`ssh-keyscan $ip 2>/dev/null`
            if [ "x$val" == "x" ]; then
                echo "ssh-keyscan $ip failed!"
            else
                echo $val>>$SSH_DIR/known_hosts
            fi
        fi
     fi
done

你可能感兴趣的:(根据IP批量生成信息追加到~/.ssh/knows_hosts文件)