# 批量修改ssh端口

#!/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
posted @ 2020-11-30 14:56  那就这样吧~  阅读(261)  评论(0)    收藏  举报