R1#tclsh
R1(tcl)#foreach address { ip_address1 ip_address2 ip-address3 } { ping $address re 5 si 1500 }
R1(tcl)#tclquit


For login one router, then from there to ping other devices. We can view result from web

wKiom1LaFbmhkoZRAACfBSkBQag987.jpg


yum -y install expect

please refer http://hj192837.blog.51cto.com/655995/1352783 to send mail via outside ISP


vi /tmp/telnet.sh

#!/usr/bin/expect -f
#telnet auto login

set timeout -1
set host 192.168.1.20
set user cisco
set password cisco
spawn telnet $host
expect "*name:"
send "$user\r"
expect "*word:"
send "$password\r"
foreach address { 192.168.1.21 192.168.1.22 } {
expect "*#"
send "ping $address\r"
}
expect "*#"
send "exit\n"
#sleep is apply on above send command
sleep 2
expect eof


vi /tmp/pingtest.sh

ping -c 5 192.168.1.20 &> /dev/null
result=$?
if [ $result -eq 0 ]
then
 /tmp/telnet.sh > /tmp/telnet-pingtest.txt
 echo "ping test result" | mail -s "ping test result" -a /tmp/telnet-pingtest.txt 133xxxxxxxx@189.cn
else
 echo "can not reach R1" | mail -s "can not reach R1" 133xxxxxxxx@189.cn
fi


chmod +x /tmp/telnet.sh /tmp/pingtest.sh



首先完成ssh cisco@192.168.1.20

vi /tmp/ssh.sh

#!/usr/bin/expect -f
#ssh auto login

set timeout -1

set host 192.168.1.20
set user cisco
set password cisco
spawn ssh $host -l $user
expect "*password:"
send "$password\r"
foreach address { 192.168.1.21 192.168.1.22 } {
expect "*#"
send "ping $address\r"
}
expect "*#"
send "exit\n"
#sleep is apply on above send command
sleep 2
expect eof


vi /tmp/pingtest.sh

ping -c 5 192.168.1.20 &> /dev/null
result=$?
if [ $result -eq 0 ]
then
 /tmp/ssh.sh > /tmp/ssh-pingtest.txt
 echo "ping test result" | mail -s "ping test result" -a /tmp/ssh-pingtest.txt 133xxxxxxxx@189.cn
else
 echo "can not reach R1" | mail -s "can not reach R1" 133xxxxxxxx@189.cn
fi


chmod +x /tmp/ssh.sh /tmp/pingtest.sh


You can add to crontab to send your mail periodically.