需要建立1.txt一列为主机名 一列为ip
#!/bin/bash
while read IP ;do
host_pass="Eastcom@!@#"
#expect &> /dev/null <<EOF #或者expect <<EOF &> /dev/null
#set timeout 20
#spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$IP
#expect {
#"yes/no" { send "yes\n";exp_continue }
#"password" { send "$PASS\n" }
#}
#expect eof
#EOF
#ssh root@$IP /bin/bash < key2.sh
B=`echo $IP | awk {'print $1'}`
A=`echo $IP | awk {'print $2'}`
/usr/bin/expect <<-EOF
set timeout 7
spawn ssh -p22088 sms@$A
expect {
"yes/no" { send "yes\n";exp_continue}
"sms@${A}" { send "${host_pass}\n"}
}
expect "*$"
send "su\r"
expect "password:"
send "Eastcomnfv@2018\r"
send "\r"
expect "*#"
send "hostnamectl set-hostname $B\r"
send "exit\r"
expect eof
EOF
done < 2.txt
标签:bin,更改,IP,主机名,expect,root,远程,ssh
From: https://www.cnblogs.com/catastrophe/p/17314270.html