首页 > 其他 > 详细

# 批量修改ssh端口

时间:2020-11-27 17:27:41      阅读:2      评论:0      收藏:0      [点我收藏+]
#!/bin/bash
DIR=$(cd "$(dirname "$0")"; pwd)
FILE="$1"
IP_pool=$(awk ‘{print $1}‘ $DIR/$FILE)
cat /dev/null > $DIR/result.log
sshcopy(){
        /usr/bin/expect <<-EOF
        set timeout 15
        spawn ssh-copy-id root@$1
        expect "(yes/no)" { send "yes\r";exp_continue }
        expect "password:" { send "$2\r" }
        expect eof
        EOF
}

cport(){
ssh -Tq root@$1 << EOF
sed -i -e ‘/Port 22/d‘ -e ‘1a Port 40000‘ /etc/ssh/sshd_config
service sshd restart
exit
EOF
}

test(){
    echo "" | telnet $1 40000 | tee /tmp/$1.tmp
    if grep "Connected" /tmp/$1.tmp &>/dev/null; then
        echo "$1 setup successful" >> $DIR/result.log
    else
        echo "$1 setup failed" >> $DIR/result.log
    fi
}

for i in ${IP_pool}; do
{
    pwd=$(cat $DIR/$FILE | awk -v p=$i ‘$1==p{print $NF}‘)
    sshcopy $i $pwd
    cport $i
    test $i
}&wait
done
rm -rf /tmp/*.tmp

# 批量修改ssh端口

原文:https://www.cnblogs.com/ly447742/p/14048914.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号