您的位置:首页 > 运维架构 > Linux

Linux 下使用expect自动telnet/ssh cisco设备执行ping测试

2014-01-16 22:16 525 查看
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




yum -y install expect

please refer /article/4212789.html 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.


本文出自 “Ilovecat(个人笔记)” 博客,请务必保留此出处http://hj192837.blog.51cto.com/655995/1352370
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: