1

我已经用 Exepct 语言完成了以下示例脚本来备份我的网络设备配置(cisco 设备)

#!/usr/bin/expect -f

# ---------------- configuration ---------------- #

set device 192.168.244.20   
set user go00080         
set pass password        
set tftp 192.168.244.243              
set timeout 60

# -------------- core -------------- #
spawn telnet $device
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
send "copy running-config tftp://192.168.244.243\r"
expect "Address or name of remote host"
send "\r"
expect "Destination filename "
send "\r"
expect "secs"
send "exit\r"
close
exit 0

现在,我希望我的脚本从 csv 或更简单的 txt 文件中加载 $device;我的意思是,假设我的 txt 文件以下列方式包含所有 cisco ip 地址:

192.168.244.20
192.158.244.21
...
192.168.244.245

我希望该脚本一旦加载 txt 文件,每次加载一个 IP 地址,将该 IP 地址分配给 $device 变量,然后执行命令“copy running-config ...”我该怎么办?任何人都可以帮助我

4

1 回答 1

4
# -------------- core -------------- #
set file_handle [open text.txt r]
while {[gets $file_handle device] != -1} {
    spawn telnet $device
    #... same as above
    send "exit\r"
    expect eof
}
close $file_handle

有关特定命令的帮助,请参阅http://tcl.tk/man/tcl8.5/TclCmd/contents.htm

于 2012-06-07T11:28:27.113 回答