2017-06-15 65 views
-4

我從來不使用perl腳本。我已經完成了期望的腳本,有什麼辦法將它轉換爲Perl?如何將expect腳本轉換爲perl scprit?

我正在嘗試學習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; 
} 

非常感謝!

+0

Stackoverflow不是代碼編寫/翻譯服務。 – Brandin

回答

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模塊。

+0

非常感謝你!但我已經轉換爲Perl腳本。我會說Perl腳本並不難。無論如何,感謝那些信息。 –