-4

I never use perl script before. I have already done expect script, is there any way to convert it to perl?

I am trying to learn perl script right now. Any idea could help!

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;
}

Thanks a lot!

pynexj
  • 19,215
  • 5
  • 38
  • 56

1 Answers1

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__

Alternatively, there's the Expect module.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • thank you so much! But I have already converted to Perl script. I'd say Perl script is not hard. Anyways, thanks for those information. – Terrence Guo Jun 21 '17 at 18:12