Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

my script logs in to console of router and clear all the user (console) lines. When it encounters the current line, it comes out of the script. Is there a way to handle this situation, more precisely, when it encounters current line, it should not clear and it should go to the next line which is used by somebody else

######################### sub clear_line { ######################### print "router name? "; my $r = <STDIN>; chomp($r); print "term? "; my $terms = <STDIN>; chomp($terms); my $values; my @test_array; my @data_set = ( JT->new( host => $r ), ); foreach my $rh (@data_set) { for (my $term = $terms; $term <=40; $term++) { $rh->cmd("request system logout terminal pts/$term"); } } }

Logs below

[shell]$ ./ft_generic_bk.pl router name? abc term? 1 Oct 20 00:21:07 [INFO ] [abc] JT::Device::connect: abc is connected vi +a /volume/labtools/bin/nicetelnet pid 16210 Oct 20 00:21:09 [INFO ] [abc] [cmd] cat /usr/share/cevo/cevo_version Oct 20 00:21:10 [INFO ] [abc] [cmd] request system logout terminal pts +/1 Oct 20 00:21:12 [INFO ] [abc] [cmd] request system logout terminal pts +/2 Oct 20 00:21:14 [INFO ] [abc] [cmd] request system logout terminal pts +/3 Oct 20 00:21:16 [INFO ] [abc] [cmd] request system logout terminal pts +/4 Oct 20 00:21:17 [INFO ] [abc] [cmd] request system logout terminal pts +/5 Oct 20 00:21:17 [INFO ] [abc] [cmd] request system logout terminal pts +/6 Oct 20 00:21:18 [INFO ] [abc] [cmd] request system logout terminal pts +/7 Oct 20 00:21:18 [INFO ] [abc] [cmd] request system logout terminal pts +/8 Oct 20 00:21:19 [INFO ] [abc] [cmd] request system logout terminal pts +/9 Oct 20 00:21:20 [INFO ] [abc] [cmd] request system logout terminal pts +/10 Oct 20 00:21:21 [INFO ] [abc] [cmd] request system logout terminal pts +/11 Oct 20 00:21:22 [INFO ] [abc] [cmd] request system logout terminal pts +/12 Oct 20 00:21:23 [INFO ] [abc] [cmd] request system logout terminal pts +/13 Oct 20 00:21:24 [INFO ] [abc] [cmd] request system logout terminal pts +/14 Oct 20 00:21:25 [INFO ] [abc] [cmd] request system logout terminal pts +/15 Oct 20 00:21:26 [INFO ] [abc] [cmd] request system logout terminal pts +/16 Oct 20 00:21:27 [INFO ] [abc] [cmd] request system logout terminal pts +/17 Oct 20 00:21:28 [INFO ] [abc] [cmd] request system logout terminal pts +/18 Oct 20 00:21:29 [WARN ] [abc] Device response :Connection closed by fo +reign host. Oct 20 00:21:29 JT.pm:2877 [ERROR] [abc] JT::Device::_send: Expect rep +orted 3:Child PID 16210 exited with status 256 JT::error_handler HARD at /volume/labtools/lib/JT/Device.pm li +ne 2867, <STDIN> line 2. JT::Device::_send('JT::JUNOS=HASH(0xf0867c4)', 'cmd', 'request + system logout terminal pts/18', 'timeout', 60, 'timeout_ok', 0, 'slo +w', 0, ...) called at /volume/labtools/lib/JT/JUNOS.pm line 2106 JT::JUNOS::cmd('CMD', 'request system logout terminal pts/18') + called at ./ft_generic_bk.pl line 1912 ft_generic_tc::clear_line() called at ./ft_generic_bk.pl line +2417 JT: die ./ft_generic_bk.pl with exit code 18 CONNECT_LOST

Line 18 is used by my script and it disconnects and i should avoid it. Is there a way to avoid my line and go to the next line ?

Replies are listed 'Best First'.
Re: Exception handling (skip one item in list)
by hippo (Archbishop) on Oct 20, 2017 at 08:17 UTC

    In your loop, skip that term.

    for (my $term = $terms; $term <=40; $term++) { next if $term == 18; $rh->cmd("request system logout terminal pts/$term"); }

      Thanks. But, line number is random. Can't predict the same line again

        Look for the docs of your JT class (whatever that is) and see how to determine your specific term from it or from the array elements it returns (your $rh).

Re: Exception handling
by Anonymous Monk on Oct 20, 2017 at 15:44 UTC
    If a runtime exception is bringing your program to a halt, the proper approach is to trap the exception so that it does not. In Perl, the exception-trapping block is rather-improbably called eval{...}. There are CPAN modules which provide the more-conventional try...except syntax.