脚本如下
crontab_add.sh
explujing=/home/crontab_add.exp
username=root
password=123456
password=123456
iplist="
192.168.3.101
192.168.3.101
192.168.3.102
"
port=22
for ip in $iplist
do
$explujing $ip $username $password $port
"
port=22
for ip in $iplist
do
$explujing $ip $username $password $port
done
#-------------------------------------
#-------------------------------------
crontab_add.exp
#!/usr/bin/expect -f
set date [exec date -d day +'%Y%m%d']
set date [exec date -d day +'%Y%m%d']
set desthost [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set port [lindex $argv 3]
set username [lindex $argv 1]
set password [lindex $argv 2]
set port [lindex $argv 3]
spawn ssh -D $port $username@$desthost
#expect "*assword:*"
#send "$password "
expect {
"*(yes/no)?"
{
send "yes "
expect "*assword:" {send "$password "}
}
"*assword:"
{
send "$password "
}
}
expect "*#"
send "cd /var/spool/cron/ "
expect "*#"
send "echo "0 6 * * * cd /home/;./probecheck.sh &" >> /var/spool/cron/root "
expect "*#"
send -- "echo "0 8 * * * find /home/probechecklog/ -name "*192.167.1*" -mtime +1 -exec rm -f {} \; >>/dev/null &" >>/var/spool/cron/root "
send -- "echo "0 8 * * * find /home/probechecklog/ -name "*192.167.1*" -mtime +1 -exec rm -f {} \; >>/dev/null &" >>/var/spool/cron/root "
expect "*#"
send "service crond restart "
expect "*.service"
send "systemctl restart crond "
send "service crond restart "
expect "*.service"
send "systemctl restart crond "
expect "*#"
send "exit "
interact
send "exit "
interact