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
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
相关文章推荐
- Linux下通过EXPECT脚本自动执行ssh远程连接
- 使用expect实现ssh自动输入密码,从而自动登陆Linux
- Linux下通过EXPECT脚本自动执行ssh远程连接
- 使用linux expect进行ssh和telnet自动化登录等操作
- [置顶] Linux下用expect实现ssh自动登录并执行脚本
- 在 Linux 命令行脚本中执行 sudo 等命令时自动输入密码(使用expect)
- 使用linux expect进行ssh和telnet自动化登录等操作
- 使用public/private key让putty(ssh)自动登录(以及linux上使用密钥做ssh自动登陆)
- linux expect详解(ssh自动登录)
- 使用expect脚本实现Linux中自动输入密码
- 使用 CustomScript 扩展程序自动执行 Linux 虚拟机自定义任务
- 使用expect实现ssh自动交互
- 使用expect和ssh远程执行命令的脚本
- Cisco 使用SSH 登陆Cisco设备
- 使用 CustomScript 扩展程序自动执行 Linux 虚拟机自定义任务
- Linux下的类似于SecureCRT的ssh,telnet自动登录软件
- Linux下使用expect和另外一台机器通过SSH进行持续性交互
- java使用ssh连接Linux并执行命令
- Linux下使用SSH非交互式远程执行命令脚本
- 使用 CustomScript 扩展程序自动执行 Linux 虚拟机自定义任务