auto_scp.sh
#!/bin/bash
dir=/home/ability/maintain
localFile=/home/Ityunwei/agent/bin/install_os7.sh
remoteDir=/home/Ityunwei/agent/bin
while read line
do
host=`echo $line| awk '{print $1}'`
password=`echo $line | awk '{print $2}'`
expect <<EOF
set timeout -1
spawn scp -r ${localFile} ${User}@${host}:${remoteDir}
expect {
"yes/no" {send "yes\r"; exp_continue}
"password:" {send "${password}\r"}
}
expect 100%
expect eof
EOF
if [ $? -eq 0 ]; then
echo "${host}成功"
else
echo "${host}失败"
fi
sleep 20s
expect expect_scpw.sh $host $password &
sleep 20s
done < $dir/IThost
expect_scpw.sh
#!/usr/local/bin/expect
set HOST [lindex $argv 0]
set PASSWD [lindex $argv 1]
spawn ssh root@$HOST -o StrictHostKeyChecking=no
expect "(yes/no)?" { send "yes\n" }
expect "*password:" { send "$PASSWD\n" }
expect "*password:" { send "$PASSWD\n" }
expect "*#" { send "hostname\n" }
expect "*#" { send "cd /home/Ityunwei/agent/bin/\n" }
expect "*#" { send "chown -R Ityunwei:Ityunwei install_os7.sh \n" }
expect "*#" { send "./install_os7.sh\n" }
expect "*#" { send "tailf -n1 /etc/rc.local\n" }
expect eof