CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
array with one element from the Telnet.pm module

 



swoop
New User

Oct 30, 2009, 9:49 AM

Post #1 of 3 (1825 views)
array with one element from the Telnet.pm module Can't Post

I came up with a workaround to my problem with Cisco.pm. I found if I call the Telnet.pm waitfor routine I can extract the full content of the routers 'show log'. However, the array (@output) has one element which is the whole content of the 'show log'. As another workaround I put the array contents to a file, then read the file back into the script and populate another array. Then I can parse the array elements. Is there another way of doing this? I was wondering if instead of placing the array contents to a file and re-reading them back into the script if I can some how parse the array with one element (show log contents)? Below is a snippet of the code:

sub scan_log {

my $cmd = "show log";

my @output;

my $previous = "";

my $prompt = '/.*[#>]$/i';



$session->print($cmd);

@output = $session->Net::Telnet::waitfor(-match => $prompt); <<<<<<<<<<<



open (OUT, "> $DIR/show_log.log");

print OUT @output;

close (OUT);

splice(@output,0);



open (FH, "$DIR/show_log.log");

@output = <FH>;

close (FH);

unlink "$DIR/show_log.log";



foreach my $line (@output) {

chomp($line);

if ($line =~ /Trace/) {

print "TRACEBACK MSG: $previous\n";

print "TRACEBACK: $line\n";

} else {

$previous = $line;

}

}

}



sub node_login {

my $buffer_mb = 1024 * 1024;



print "Logging into node: $node ...\n";



# Login to node

$session = Net::Telnet::Cisco->new(Host => $node,

Timeout => $node_timeout,

Prompt => $prompt);



$session->send_wakeup;

$session->login("$username", "$password");

$session->enable($enable_password);

$session->max_buffer_length(5 * $buffer_mb);

$session->cmd($zero_screen);

}


toolic
User

Oct 30, 2009, 10:02 AM

Post #2 of 3 (1823 views)
Re: [swoop] array with one element from the Telnet.pm module [In reply to] Can't Post

If the 1st element of the @output array has all the contents you need and it contains newline characters, then you can split it on newline into a new array, then just search through the new array. In that case, there is no need to write out to a file and read that file back in.



Code
my @lines = split /\n/, $output[0]; 
for my $line (@lines) {
# do stuff here...
}


or, more tersely...


Code
for my $line (split /\n/, $output[0]) { 
# do stuff here...
}



swoop
New User

Oct 30, 2009, 10:16 AM

Post #3 of 3 (1822 views)
Re: [toolic] array with one element from the Telnet.pm module [In reply to] Can't Post

toolic - That worked perfectly. Thank you very much!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives