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: Beginner:
copy from cisco router

 



zjedzmnie
Novice

Oct 1, 2008, 12:46 AM

Post #1 of 8 (906 views)
copy from cisco router Can't Post

Hi

I'm new to Perl so please don't shout

I need help in my little project.

I want to log in remote host (cisco router) and then copy some information from logs.

I want to send "sho log | i ISDN-6-DISCONNECT" because only that interest me. This is a example:

Oct 1 06:19:34 CEsT: %ISDN-6-DISCONNECT: Interface Serial1/0:0 disconnected from 999999999 a-aaaa-999, call lasted 512 seconds

And I'm trying to copy only "999999999" and "a-aaaa-999"

Any suggestions. Thanks for help. And sorry for my english.

to log in and to send massages I use:

e->expect($timeout, [qr/password:/ => sub {my $in=shift;$in->send("zjedzmnie\r")}],[timeout => sub {$fuc=1}]);

$e->expect($timeout, [qr/^(\w|\d|-)+?#/, sub {my $in=shift;$in->send("show log | i ISDN-6-DISCONNECT\r")}], [timeout => sub {$fuc=1}]);

$fuc #if something go wrong


(This post was edited by zjedzmnie on Oct 1, 2008, 1:17 AM)


zjedzmnie
Novice

Oct 1, 2008, 6:20 AM

Post #2 of 8 (898 views)
Re: [zjedzmnie] copy from cisco router [In reply to] Can't Post

That's all I do and a have problem with for(my $i=0; $........
It doesn't work, I don't know what to do.

Log example from cisco router:
Oct 1 13:15:53 CEsT: %ISDN-6-DISCONNECT: Interface Serial1/0:0 disconnected from 999999999 h-hhhh-999, call lasted 42 seconds


#!/usr/bin/perl -w

use strict;
use Expect;

my $sendmail="/usr/sbin/sendmail";

my $timeout=60;

my $alltext="Content-Type: text/plain;
charset=\"iso-8859-2\"
Subject: Nr tel ISDN naszych biur HSB

";

my $e = new Expect;
$e->log_user(1);

my $fucked=0;

$e->spawn("/usr/bin/ssh -l ******** ************");

sleep 1;

$e->expect($timeout,
[qr/password:/ => sub {my $in=shift;$in->send("**************\r")}],
[timeout => sub {$fucked=1}]);

if ($fucked==1){
$alltext.="login error\n";

}else{
$e->expect($timeout,'-re','^(\w|\d|-)+?#');
$e->send("terminal length 0\r");
$e->expect($timeout,'-re','^(\w|\d|-)+?#');
$e->send("show log | i ISDN-6-DISCONNECT\r");
$e->expect($timeout,'-re','^(\w|\d|-)+?#');

my $tablica=$e->before();

$tablica=~s/\r//mg;
$tablica=~s/^\s+//mg;

my @tmp=split("\n",$tablica);


for(my $i=0; $i<=$#tmp; $i++){

if ($tmp[$i]=~/^(.*?)(\d{9})\ (h\-[a-z]{4}-\d{3})(.*?)\n$/){

$tmp[$i]=~s/^(.*?)(\d{9})\ (h\-[a-z]{4}-\d{3})(.*?)\n$/$2\-$3/;

print "$tmp[$i]\n";

}
}


}


$e->send("logout\r");


open(SENDMAIL, "| $sendmail -oi -t -f '****************\@*************'" ) or die "Can't open sendmail: $!\n";
print SENDMAIL "To: <****************\@************>\n";
print SENDMAIL $alltext;
close(SENDMAIL) or warn "Sendmail didn't close nicely: $!\n";


KevinR
Veteran


Oct 1, 2008, 10:50 AM

Post #3 of 8 (894 views)
Re: [zjedzmnie] copy from cisco router [In reply to] Can't Post

remove the \n from these two regexps and retry your code:


Code
 if ($tmp[$i]=~/^(.*?)(\d{9})\ (h\-[a-z]{4}-\d{3})(.*?)\n$/){ 

$tmp[$i]=~s/^(.*?)(\d{9})\ (h\-[a-z]{4}-\d{3})(.*?)\n$/$2\-$3/;


There should be no \n in the strings because of the split("\n") earlier in the script, so the regexps will fail to match because it will not find a \n.
-------------------------------------------------


zjedzmnie
Novice

Oct 1, 2008, 10:48 PM

Post #4 of 8 (881 views)
Re: [KevinR] copy from cisco router [In reply to] Can't Post

thanks, now it works


zjedzmnie
Novice

Oct 1, 2008, 11:40 PM

Post #5 of 8 (876 views)
Re: [zjedzmnie] copy from cisco router [In reply to] Can't Post

ok, next problem, I have to sort my @tmp

now i look that
example:
111111111 s-qwqw-172
333333333 s-erer-189
444444444 s-yuuy-125
111111122 s-yuio-190
but I have also
111111321 s-qwqwq-173

And I want to sort by last 3 numbers

444444444 s-yuuy-125
111111111 s-qwqw-172
111111321 s-qwqwq-173
333333333 s-erer-189
111111122 s-yuio-190


KevinR
Veteran


Oct 2, 2008, 12:45 AM

Post #6 of 8 (873 views)
Re: [zjedzmnie] copy from cisco router [In reply to] Can't Post

Show me what you have tried so far to sort the data.
-------------------------------------------------


zjedzmnie
Novice

Oct 2, 2008, 1:53 AM

Post #7 of 8 (868 views)
Re: [KevinR] copy from cisco router [In reply to] Can't Post

#!/usr/bin/perl -w

use strict;
use Expect;

my $sendmail="/usr/sbin/sendmail";

my @tmp;

my $timeout=60;

my $alltext="Content-Type: text/plain;
charset=\"iso-8859-2\"
Subject: Nr tel ISDN naszych biur HSB

";


my $e = new Expect;
$e->log_user(1);

my $fucked=0;

$e->spawn("/usr/bin/ssh -l ************* *************");

sleep 1;

$e->expect($timeout,
[qr/password:/ => sub {my $in=shift;$in->send("************8\r")}],
[timeout => sub {$fucked=1}]);

if ($fucked==1)
{
$alltext.="DMZ: login error\n";
}
else
{
$e->expect($timeout,'-re','^(\w|\d|-)+?#');
$e->send("terminal length 0\r");
$e->expect($timeout,'-re','^(\w|\d|-)+?#');
$e->send("show log | i ISDN-6-DISCONNECT\r");
$e->expect($timeout,'-re','^(\w|\d|-)+?#');

my $logi=$e->before();

$logi=~s/\r//mg;
$logi=~s/^\s+//mg;

@tmp=split("\n",$logi);

sleep 1;



for(my $i=1; $i<=$#tmp; $i++)
{
if ($tmp[$i]=~/^(.*?)(\d{9})\s(h-\w{4,5}-)(\d{3})(.*?)$/)
{
$tmp[$i]=~s/^(.*?)(\d{9})\s(h-\w{4,5}-)(\d{3})(.*?)$/$2\t$3$4\n/;

#print "$tmp[$i]\n";
}
else
{
$tmp[$i]=~s/^(.*?)$/\n/;
}
}
}

my @tosort;

for(my $j=0; $j<=$#tmp;$j++)
{
if ($tmp[$j]=~/^(\d{9})\t(h-\w{4,5}-)(\d{3})$/)
{
$tosort[$j]=$tmp[$j];
}
}


my @sorted = sort tableSorter @tosort;

sub tableSorter ($$)
{
my($row1, $row2) = @_;

my $column1comparison = $row1->[2] cmp $row2->[2];

if($column1comparison != 0)
{
return $column1comparison;
}

my $column2comparison = $row1->[1] cmp $row2->[1];

if($column2comparison != 0)
{
return $column2comparison;
}

return $row1->[0] cmp $row2->[0];
}

$alltext.="@sorted\n";

$e->send("logout\r");

open(SENDMAIL, "| $sendmail -oi -t -f '***********@************'" ) or die "Can't open sendmail: $!\n";
print SENDMAIL "To: <**************\@*************>\n";
print SENDMAIL $alltext;
close(SENDMAIL) or warn "Sendmail didn't close nicely: $!\n";





And I have problem in line (my $column1comparison = $row1->[2] cmp $row2->[2];)

Can't use string ("999999999 h-aaaa-100
") as an ARRAY ref while "strict refs" in use at ./sho_log_tel_sort_save.pl line 99.


KevinR
Veteran


Oct 2, 2008, 2:56 AM

Post #8 of 8 (864 views)
Re: [zjedzmnie] copy from cisco router [In reply to] Can't Post


Code
@tosort = ('111111111 s-qwqw-172', 
'333333333 s-erer-189',
'444444444 s-yuuy-125',
'111111122 s-yuio-190',
'111111321 s-qwqwq-173'
);


my @sorted = map{$_->[0]}
sort{$a->[1] <=> $b->[1]}
map{/-(\d+)$/; [$_,$1]} @tosort;

print "$_\n" for @sorted;

-------------------------------------------------

 
 


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

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