我需要更新几个网关的配置...我有两个文件:
文件 1 ( ip.txt
) :保存网关的 IP 地址 ---> 该文件每月更新一次。
文件 2 ( cmd.txt
):保存在网关上进行配置更改的命令 ---> 这也会不时更新。
到目前为止我有两个脚本......
脚本 1 即script1.sh
:基本上 script1 正在读取网关 IP 并传递它以期望脚本登录到网关。
#!/bin/sh
for device in `cat /home/ip.txt`;do
./step_3 $device;
done
第二个脚本
#!/bin/expect -f
set IP [lindex $argv 0]
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user@$IP
expect "password: "
send "difficult123\r"
expect "GBU_0:"
send "su -\r"
expect "Password: "
send "verydifficult123\r"
expect "GBI_0:"
send "/17.1/bin/cli.exe\r"
expect "USERNAME : "
send "GOOR\r"
expect "PASSWORD : "
send "DIFFICULT123\r"
expect "] "
****************** POINT 1 - Only One Change command shown -- I have multiple commands here to insert-----
send "CHG-MEM:SEV=CRITICAL;\r"
expect "(Y/N) :"
send "Y\r"
expect "]"
******************** POINT 2
send "exit\r"
expect "0:~> "
send "exit\r"
expect "logout"
send "exit\r"
expect "closed"
expect eof
如果我对expect命令b/w Point 1和2进行硬编码,那么一切都会像魅力一样工作(我有多个命令)...我如何从文件中调用expect命令b/w Point 1和2...即,我做不想对里面的命令进行硬编码,而是在一个单独的文件中,这样用户就可以更新命令文件而无需触摸脚本并运行它......将 IP 传递给期望脚本效果很好......我不确定是否可以也从文件传递期望命令。
答案1
TCL 可以轻松包含并运行其他文件中的命令;这source(n)
例如命令允许一个人说
#!/usr/bin/env expect
package require Tcl 8.5
set IP [lindex $argv 0]
set include_file [lindex $argv 1]
# ... begin commands before here
catch {source $include_file} result options
if {[dict get $options -code] != 0} {
puts stderr "could not source $include_file: $result"
exit 1
}
# end commands after here ...
将其另存为runner
一个即可拥有一个包含文件
$ cat runthese
puts a
puts b
puts c
$ expect runner 127.0.0.1 runthese
a
b
c
$
当然,runthese
要包含的文件或任何文件可以根据需要改为具有send
和。expect