-4

我以前从未使用过 perl 脚本。我已经完成了期望脚本,有没有办法将它转换为perl?

我现在正在尝试学习 perl 脚本。任何想法都会有所帮助!

foreach addr $addr {
if {$addr eq {}} continue;
    set timeout 180
    match_max 1000000
    spawn telnet $addr
    expect {
    "*timed out*"   {puts "Host not responding"; continue}
    "'^]'."                                 {sleep .1}
    }
    expect "*login*"        {send "$user\r"}
    expect "*assword:*"     {send "$pass\r"}
    expect {
    "Login incorrect"   {puts "Bad Password or User"; exit}
    -re {[#>] ?$}           {send "page-off\r"} 
    }
    log_file $customer\-License-$date.log
    expect -re {[#>] ?$}
    send "show clock\r"
    sleep .5
    expect -re {[#>] ?$}    
    send "show docsis channel utilization\r"
    sleep .5
    expect -re {[#>] ?$}
    send "show clock\r"
    sleep .5
    expect -re {[#>] ?$}
    sleep .5
    send -- "\r\rexit\r"
send "\r"
puts "Connection closed, connecting to next chassis\r"
    sleep 1
    log_file
expect eof;
}

非常感谢!

4

1 回答 1

0
system('expect', '-f', <<'__EOS__');
foreach addr $addr {
if {$addr eq {}} continue;
    set timeout 180
    match_max 1000000
    spawn telnet $addr
    expect {
    "*timed out*"   {puts "Host not responding"; continue}
    "'^]'."                                 {sleep .1}
    }
    expect "*login*"        {send "$user\r"}
    expect "*assword:*"     {send "$pass\r"}
    expect {
    "Login incorrect"   {puts "Bad Password or User"; exit}
    -re {[#>] ?$}           {send "page-off\r"} 
    }
    log_file $customer\-License-$date.log
    expect -re {[#>] ?$}
    send "show clock\r"
    sleep .5
    expect -re {[#>] ?$}    
    send "show docsis channel utilization\r"
    sleep .5
    expect -re {[#>] ?$}
    send "show clock\r"
    sleep .5
    expect -re {[#>] ?$}
    sleep .5
    send -- "\r\rexit\r"
send "\r"
puts "Connection closed, connecting to next chassis\r"
    sleep 1
    log_file
expect eof;
}
__EOS__

或者,还有Expect模块。

于 2017-06-15T06:16:39.060 回答