expect 的安装及简单应用

#!/usr/bin/expect  
for i in `cat file`; do
    spawn scp $i root@192.168.8.xxx:/data/backup/mysql/rds_archives
    expect "*password:"
    send "xxxx\r"
done

案例:

#!/bin/bash
while read line ; do
    host=`echo $line  | awk '{print $1}' `
    pass=`echo $line  | awk '{print $2}' `
    expect -c "  
        set timeout 3;  
        spawn scp -P2019 tcp_status.sh services_status.sh userparameter_services.conf ${host}:/home/oper ;  
        expect {                
            \"*assword\" { send \"$pass\r\" }  
            \"yes/no\" { send \"yes\r\"; exp_continue }  
         } ;  
        send \"exit\r\"
        expect eof 
    "
    expect -c "
        set timeout 1;  
        spawn ssh -p2019  oper@${host};  
        expect {                
            \"*assword\" { send \"$pass\r\" }  
            \"yes/no\" { send \"yes\r\"; exp_continue }  
         } ;  
        expect \"*]*\"   {send \" cd /home/oper/;sudo mv services_status.sh  tcp_status.sh  /etc/zabbix/;sudo mv userparameter_services.conf /etc/zabbix/zabbix_agentd.d/ ; sudo chown zabbix. -R /etc/za
bbix \r\" }  ;        expect \"*]*\"   {send \" sudo systemctl restart zabbix-agent.service \r\" }  ;
        send \"exit\r\"
        expect eof 
    "
done < tmp.txt

推荐阅读更多精彩内容