![Expect while 循环中的多个 telnet 连接](https://linux22.com/image/107226/Expect%20while%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%A4%9A%E4%B8%AA%20telnet%20%E8%BF%9E%E6%8E%A5.png)
我需要获取文件中列出的某些设备的序列号,如下所示:
device_id,ip_address
BIOTERIO, 148.000.00.189
N7K-LAN(JAF1651ANDL), 148.000.0.192
LAB_PESADO, 148.000.000.130
Arquitectura_Salones, 148.000.000.61
CIVIL_253, 148.000.000.253
Arquitectura, 148.000.000.253
ING_CIVIL, 148.000.000.251
ING_CIVIL_DIR, 148.000.0.188
Ingenieria_Posgrado, 148.000.000.253
Biblio_Barragan, 148.000.000.61
Electronica_Edif_3, 148.000.000.253
我不知道如何进行循环,但我需要它通过 telnet 连接到每个设备,使用特定的用户名和密码,然后运行此 cisco 命令“ show version
”并将序列号保存在同一个文件中(如果可能) 。
知道我该怎么做吗?
答案1
#!/usr/bin/env expect
# open first argument to program...
set fd [open [lindex $argv 0]]
# for each line using that file handle...
while {[gets $fd line] >= 0} {
# split on , and select the last one...
set lastcolumn [lindex [split $line ","] end]
# whoops there's some whitespace so clear that...
set ip [string trim $lastcolumn " "]
puts ">$ip<"
}