免交互登陆其他服务器

#!/usr/bin/bash
>ip.txt

for i in {2..254}
do
{
ip=192.168.157.$i
ping -c1 -i0.01 -W1 $ip &> /dev/null
        if [ $? -eq 0 ];then
                echo "$ip" >> ip.txt

        /usr/bin/expect <<-EOF
        set timeout 10
        spawn ssh-copy-id $ip
        expect {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "1\r" };
        }
        expect eof
        EOF


        fi
:wq!

上面的是192.168.157.137服务器执行的脚本,通过免交互把密钥对拷贝到可以ping通的IP地址

#!/usr/bin/expect
spawn  ssh [email protected]

expect {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "1\r" };
}
interact
:wq!

上面的脚本是免交互、免密登陆192.168.157.137服务器

猜你喜欢

转载自blog.csdn.net/Qingyunya/article/details/124051777