【问题标题】:Expect script telnet timeout issue预期脚本 telnet 超时问题
【发布时间】:2016-02-03 02:19:13
【问题描述】:

以下脚本 telnet 到从 txt 文件调用的各种设备,然后运行命令,记录输出,然后退出。但是,当使用不正确的 IP 地址时,脚本会显示输入的用户名和密码,这在日志文件中可见,这并不理想。知道如何插入超时来防止这种情况发生吗?

#telnet.exp
######################################################

#!/usr/bin/expect -f

# Set variables
set hostname [lindex $argv 0]
set username [lindex $argv 2]
set password [lindex $argv 1]

# Log the output
log_file -a ~/configuration-telnet.log

# Which device we are working on and at what time
send_user "\n"
send_user ">>>>>  Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"

spawn telnet $hostname
expect "Username:"
send "$username\n"

expect "Password:"
send "$password\n"
expect "#"

send "term len 0\r"
send "show running-config\r"
expect "end\r"
send "\r"
send "exit\r"

########################################################
#telnet.sh
########################################################
#!/bin/bash
echo -n "Username:"
read -s -e username
echo -ne '\n'
echo -n "Password:"
read -s -e password
echo -ne '\n'
# Feed the expect script a device list & the collected passwords
for device in `cat telnet-device-list.txt`; do
./telnet.exp $device $password $username;
done

【问题讨论】:

    标签: telnet expect


    【解决方案1】:

    为了产生timeout 事件,您可以将其添加为模式,这是一个匹配超时的内置命令。

    基本想法可以是这样的,

    expect {
        pattern {some_action_here} 
        timeout {puts "timeout_here"}
    }
    

    为了使其适用于所有类型的命令,可以将其概括为expect_after 命令。

    expect_after timeout {puts "timeout happened in the script"; exit 0}
    

    正如我提到的before,我建议您在每个send 命令之后使用expect

    所以,你的脚本可以修改如下,

    #!/usr/bin/expect -f
    
    # Set variables
    set hostname [lindex $argv 0]
    set username [lindex $argv 2]
    set password [lindex $argv 1]
    
    # Log the output
    log_file -a ~/configuration-telnet.log
    
    # Which device we are working on and at what time
    send_user "\n"
    send_user ">>>>>  Working on $hostname @ [exec date] <<<<<\n"
    send_user "\n"
    
    expect_after timeout {puts "Timeout happened; So, exiting....";exit 0}
    
    spawn telnet $hostname
    expect "Username:" 
    send "$username\r"
    expect "Password:"
    send "$password\r"
    expect "#" 
    send "term len 0\r"
    expect "#" 
    send "show running-config\r"
    expect "end"
    send "\r"
    expect "#" 
    send "exit\r"; 
    # After sending 'exit' telnet session will be closed
    # So, waiting for 'eof'
    expect eof
    

    如果你故意要编写单独的超时动作,那么你可以重写它,

    expect {
        timeout { puts "username timeout"; exit 0}
        "Username:"
    } 
    send "$username\r"
    expect {
        timeout { puts "password timeout"; exit 0}
        "Password:"
    }
    send "$password\r"
    expect {
        timeout { puts "prompt timeout"; exit 0}
        "#" 
    }
    

    注意:

    timeout 默认值为 10 秒。它甚至可以更改为

    set timeout 60; # Timeout value of 1 min
    

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 1970-01-01
      • 2012-01-30
      • 2019-12-25
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 2016-03-27
      相关资源
      最近更新 更多