【发布时间】:2018-08-21 20:49:46
【问题描述】:
我期望 (tcl) 脚本用于自动任务正常工作 - 通过 telnet/ssh 配置网络设备。大多数情况下要执行 1,2 或 3 个命令行,但现在我有 100 多个命令行要通过期望发送。我怎样才能以聪明和好的脚本方式实现这一点:) 因为我可以将超过 100 条的所有命令行加入到一个带有“\n”的变量“commandAll”中,然后一个接一个地“发送”它们,但我认为这很丑:) 有没有不堆叠的方法它们一起在代码或外部文件中可读?
#!/usr/bin/expect -f
set timeout 20
set ip_address "[lrange $argv 0 0]"
set hostname "[lrange $argv 1 1]"
set is_ok ""
# Commands
set command1 "configure snmp info 1"
set command2 "configure ntp info 2"
set command3 "configure cdp info 3"
#... more then 100 dif commands like this !
#... more then 100 dif commands like this !
#... more then 100 dif commands like this !
spawn telnet $ip_address
# login & Password & Get enable prompt
#-- snnipped--#
# Commands execution
# command1
expect "$enableprompt" { send "$command1\r# endCmd1\r" ; set is_ok "command1" }
if {$is_ok != "command1"} {
send_user "\n@@@ 9 Exit before executing command1\n" ; exit
}
# command2
expect "#endCmd1" { send "$command2\r# endCmd2\r" ; set is_ok "command2" }
if {$is_ok != "command2"} {
send_user "\n@@@ 9 Exit before executing command2\n" ; exit
}
# command3
expect "#endCmd2" { send "$command3\r\r\r# endCmd3\r" ; set is_ok "command3" }
if {$is_ok != "command3"} {
send_user "\n@@@ 9 Exit before executing command3\n" ; exit
}
附言我正在使用一种方法来获得成功,因为 cmd 行已成功执行,但我不确定这是完美的方法:D
【问题讨论】: