0

我的代码是为从多个设备输出的一个日志文件生成的。

我们如何为每个日志文件输出分离每个设备?

这是我的代码:

#!/usr/bin/expect -f
#Slurp up the input file
set fp [open "ip.txt" r]
# To avoid empty lines, 'nonewline' flag is used
set file_data [read -nonewline $fp]
close $fp
set prompt ">"
log_file -noappend router_status.txt
foreach ip [split $file_data "\n"] {
    puts "Router $ip Interface Status"
    spawn telnet $ip
    expect "Username:"
    send "username\r"
    expect "assword:"
    send "password\r"
    expect $prompt
    # To avoid sending 'Enter' key on huge configurations
    send "show interface description\r"
    expect {
    -ex "---(more" { send -- " "; exp_continue }
    "*>" { send "exit\r" }
    }
    set timeout 3; # Reverting to default timeout
    # Sending 'exit' at global level prompt will close the connection
    expect eof
    }
4

1 回答 1

1

你可以简单地通过改变log_file

foreach ip [split $file_data "\n"] {
    puts "Router $ip Interface Status"
    spawn telnet $ip

    # Altering 'log_file' for each ip
    log_file -noappend router_${ip}_status.log

    # Your further code here...


}
于 2015-12-02T05:31:00.790 回答