用expect实现telnet登陆到linux系统后,再执行一个shell脚本,待这个shell脚
参考一种类自然语言的脚本语言:
001 var {v_time} value datetime
002 screen.text at point {10} {30} string {2. telnet ipv4 (192.168.186.132:23) test , datetime: ~v_time~}
003
004 telnet.connect ip {192.168.186.132} port {23}
005 telnet.recv expect {Username:}
006 telnet.send {cisco%0a}
007 telnet.recv expect {Password}
008
009 telnet.send {cisco%0a}
010 telnet.recv expect {>}
011 telnet.send {enable%0a}
012 telnet.set onrecv {More} send { }
013 telnet.set delay {200} ms after per byte send
014 telnet.send {show version%0a}
015 telnet.recv for {5} seconds
016
017 telnet.send {show run%0a}
018 telnet.recv for {5} seconds
019 telnet.send {exit%0a}
020 telnet.close
021
022 var {v_time} value datetime
023 screen.text at point {10} {60} string {telnet tested end. datetime:~v_time~}
024
025 run.end
--------------------------------------------------------------------
简单网络管理语言(Simple Network Management Language)是一种类自然语言的脚本语言,它采用snmp、telnet、ping、dns、arp、ssh、tcp、udp、web等协议访问网络设备,内置smtp协议用于告警等邮件的发送,支持excel、mysql、sqlite等3种方式进行网管数据的存取。
最新版本: snml8.13(ladybird20200205) utf8&ipv6
项目托管地址:https://github.com/ladybirdSnml/snmlipv6utf8snmptelnetsshweb
打包下载:https://ladybirdsnml.github.io/snmlipv6utf8snmptelnetsshweb/snml_utf8ipv6.zip
2024-10-28 广告
#!/usr/bin/expect
set ipAddr "172.28.0.11"
set userName "root"
set passwd "mypasswd"
set myScript "/home/script.sh"
set timeout 10
spawn telnet $ipAddr
expect {
"login:" {
send "$userName\n"
expect "assword:"
send "$passwd\n"
}
"*$" {
send "$myScript\n"
expect "成功"
send "exit\n"
}
}
暂时没有环境,所以未加验证,试试吧。
我知道能实现,如何实现?主要是实现如何判断shell输出“成功“不会,请教!
实现例子请参考xiehuahere的代码