1 vi rm.sh
#!/bin/expect -f
set DATE [exec date +%Y%m%d%k]
set ipaddress [lindex $argv 0]
set port [lindex $argv 1]
set logname [lindex $argv 2]
#set password xukeqiang
set date [exec date -d "-2 day" "+%Y%m%d"]
set fd [open /home/admin/passwd.sh r]
set password [read $fd]
close $fd
spawn ssh $ipaddress -p $port -l monitor
#set timeout 300
expect "[email protected]${ipaddress}‘s password:"
#set timeout 300
send "$password\r"
expect -re "\](\$|#)"
#send "rm -rf /tmp/${logname}${date}.tar\r"
send "sh /tmp/rm.sh\r"
expect -re "\](\$|#)"
send "exit\r"
expect eof
2 vi ip.sh##此文件不能有多余的空行
192.168.1.101 22 nagiosa_
192.168.1.102 22 nagiosb_
192.168.1.103 22 nagiosc_
3 vi piliang.sh
#/bin/bash
filename=/home/admin/ip.sh
while read line
do
echo $line
/bin/expect -f /home/admin/rm.sh $line
done<$filename
4 将C:\cygwin64\bin追加到windows系统环境变量Path中,然后编辑批处理文件piliang.bat如下:
sh c:\cygwin64\home\admin\piliang.sh
pause