Telnet.pm and Cisco login 
Author Message
 Telnet.pm and Cisco login

Hi all!

I want to write a function to login on a Cisco
with the Net::Telnet module and than capture the
output of some commands.
But I have some problems.

1. The login of my Cisco looks a little bit different.
The following line show the login process:

Username: person
password: xxx

routername> en
password: xxx

routername#

2. The second problem is, that the login process could
look like this and I didn't know it before:

password: xxx

routername> en
password: xxx

routername#

What must I do to handle automatically these two different
login sequenceses?

3. For the Telnet module I had to define a prompt. Normaly
the prompt off a Cisco looks like this:

routername#

So I should define the prompt:  /routername./
But when I do the "sh run" command on a Cisco, I get following:

bla bla
....
hostname routername
....

So I think the capturing of the output of "sh run" would stop
at the line above, with my defined prompt.
How should I define my prompt?

So is there anybody out there who can help me to solve these questions?

ciao
Thorsten

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

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



Wed, 11 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login
Confronted with this problem plus trying to handle things gracefully,
I retreated to using Expect.  tcl is not as nice as Perl since there
is no provision for modules but that timeout capability is really
handy for error handling. In general the most wonderful part of the
Expect language is the flexibility of the expect statement.
String handling in Perl is superior and clearer, math/arithmetic
is easier in Perl the list goes on except for that expect block
and timeout .

I plan to release my expect scripts shortly. Then integrate them all.
You are welcom to contact me in a week's time.

Dana Hudes
Globix


Quote:

>Hi all!

>I want to write a function to login on a Cisco
>with the Net::Telnet module and than capture the
>output of some commands.
>But I have some problems.

>1. The login of my Cisco looks a little bit different.
>The following line show the login process:

>Username: person
>password: xxx

>routername> en
>password: xxx

>routername#

>2. The second problem is, that the login process could
>look like this and I didn't know it before:

>password: xxx

>routername> en
>password: xxx

>routername#

>What must I do to handle automatically these two different
>login sequenceses?

>3. For the Telnet module I had to define a prompt. Normaly
>the prompt off a Cisco looks like this:

>routername#

>So I should define the prompt:      /routername./
>But when I do the "sh run" command on a Cisco, I get following:

>bla bla
>....
>hostname routername
>....

>So I think the capturing of the output of "sh run" would stop
>at the line above, with my defined prompt.
>How should I define my prompt?

>So is there anybody out there who can help me to solve these questions?

>ciao
>Thorsten

>--
>---------------------------------------------------------------

>--------------------------------------------------------------



Fri, 13 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login

Quote:

> 1. The login of my Cisco looks a little bit different.
> The following line show the login process:

> Username: person
> password: xxx

> routername> en
> password: xxx

> routername#

> 2. The second problem is, that the login process could
> look like this and I didn't know it before:

> password: xxx

> routername> en
> password: xxx

> routername#

> What must I do to handle automatically these two different
> login sequenceses?

This should work:

    ## Connect to router.
    use Net::Telnet ();
    $t = new Net::Telnet (-timeout => 10,
                          -prompt  => '/routername[>#] $/');
    $t->open($host);

    ## Login.
    ($prematch, $match) = $t->waitfor(-match => '/username: $/i',
                                      -match => '/password: $/i');
    if ($match =~ /username: $/i) {
        $t->print($user);
        $t->waitfor('/password: $/i');
    }
    $t->print($passwd);
    $t->waitfor($t->prompt);

    ## Become superuser.
    $t->print("en");
    $t->waitfor('/password: $/i');
    $t->print($super_passwd);
    $t->waitfor($t->prompt);

Quote:
> 3. For the Telnet module I had to define a prompt. Normaly
> the prompt off a Cisco looks like this:

> routername#

> So I should define the prompt:     /routername./
> But when I do the "sh run" command on a Cisco, I get following:

> bla bla
> ....
> hostname routername
> ....

Use $ to define your prompt and try to be as specific as possible.
Here $ will anchor the pattern with the end of the input buffer -
i.e. the last thing read.  If you're still worried about matching,
then issue the cisco command to change the prompt to something
unusual.

--
Jay Rogers



Fri, 13 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login


Quote:
> Confronted with this problem plus trying to handle things gracefully,
> I retreated to using Expect.  tcl is not as nice as Perl since there
> is no provision for modules but that timeout capability is really
> handy for error handling. In general the most wonderful part of the
> Expect language is the flexibility of the expect statement.
> String handling in Perl is superior and clearer, math/arithmetic
> is easier in Perl the list goes on except for that expect block
> and timeout .

> I plan to release my expect scripts shortly. Then integrate them all.
> You are welcom to contact me in a week's time.

> Dana Hudes
> Globix

Hi Dana!

Txs for your suggestion. But I also wrote my routines with expect.
I'm managing a network with over 2000 Routers, so time is a
very important value.
And the things get worked with expect, but always very slowly.
So I changed my routines to rsh/rcp that's very fine and fast.
But some person didn't like to install rsh/rcp on routers, so
I started to programm it with telnet.

ciao
Thorsten
--
---------------------------------------------------------------

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



Fri, 13 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login


Quote:

>     ## Connect to router.
>     use Net::Telnet ();
>     $t = new Net::Telnet (-timeout => 10,
>                           -prompt  => '/routername[>#] $/');
>     $t->open($host);

>     ## Login.
>     ($prematch, $match) = $t->waitfor(-match => '/username: $/i',
>                                       -match => '/password: $/i');
>     if ($match =~ /username: $/i) {
>         $t->print($user);
>         $t->waitfor('/password: $/i');
>     }
>     $t->print($passwd);
>     $t->waitfor($t->prompt);

>     ## Become superuser.
>     $t->print("en");
>     $t->waitfor('/password: $/i');
>     $t->print($super_passwd);
>     $t->waitfor($t->prompt);

Hi Jay!

Txs for your tip!
I will try it soon, give then some feedback here again.

ciao
Thorsten
--
---------------------------------------------------------------

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



Fri, 13 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login

Quote:

> Confronted with this problem plus trying to handle things gracefully,
> I retreated to using Expect.  tcl is not as nice as Perl since there
> is no provision for modules but that timeout capability is really
> handy for error handling. In general the most wonderful part of the
> Expect language is the flexibility of the expect statement.
> String handling in Perl is superior and clearer, math/arithmetic
> is easier in Perl the list goes on except for that expect block
> and timeout .      

        In my most humble of opinions Tcl bites big butt. Try
Expect.pm if you can't get Net::Telnet to do what you want. Expect.pm
is available from your local CPAN mirror.

#!/path/to/perl

use Expect;

$telnet = Expect->spawn('telnet ciscohost');

$timeout = 30;
$prompt = 'ciscohost>';

$telnet->expect($timeout,'word:');

print $telnet "$password\n";

$telnet->expect($timeout,$prompt);

print $telnet "sh diag\n";

$telnet->expect($timeout,$prompt);

print $telnet->exp_before();
....

        I am, of course, a little biased.

        Austin



Sun, 15 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login
Having done my fair of Expect/Tcl programming, I must say I prefer to
program in Perl rather than Tcl.  To that end, the older Comm.pl has
served me well over the years, and the Expect.pm is what I'd use now.
Don't get me wrong, Expect/Tcl has gotten the job done as well.

As for using Net::Telnet, I have never used it, but from looking at "perldoc
Telnet" it seems to have everything you would need to talk to cisco router.
It also seems to me to be preferrable due to its lower use of resources
in that it does not use a child process nor a pseudo tty.

Matthew H. Gerlach

Quote:

>Confronted with this problem plus trying to handle things gracefully,
>I retreated to using Expect.  tcl is not as nice as Perl since there
>is no provision for modules but that timeout capability is really
>handy for error handling. In general the most wonderful part of the
>Expect language is the flexibility of the expect statement.
>String handling in Perl is superior and clearer, math/arithmetic
>is easier in Perl the list goes on except for that expect block
>and timeout .

>I plan to release my expect scripts shortly. Then integrate them all.
>You are welcom to contact me in a week's time.

>Dana Hudes
>Globix



>>Hi all!

>>I want to write a function to login on a Cisco
>>with the Net::Telnet module and than capture the
>>output of some commands.
>>But I have some problems.

>>1. The login of my Cisco looks a little bit different.
>>The following line show the login process:

>>Username: person
>>password: xxx

>>routername> en
>>password: xxx

>>routername#

>>2. The second problem is, that the login process could
>>look like this and I didn't know it before:

>>password: xxx

>>routername> en
>>password: xxx

>>routername#

>>What must I do to handle automatically these two different
>>login sequenceses?

>>3. For the Telnet module I had to define a prompt. Normaly
>>the prompt off a Cisco looks like this:

>>routername#

>>So I should define the prompt:  /routername./
>>But when I do the "sh run" command on a Cisco, I get following:

>>bla bla
>>....
>>hostname routername
>>....

>>So I think the capturing of the output of "sh run" would stop
>>at the line above, with my defined prompt.
>>How should I define my prompt?

>>So is there anybody out there who can help me to solve these questions?

>>ciao
>>Thorsten

>>--
>>---------------------------------------------------------------

>>--------------------------------------------------------------



Mon, 16 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login
Thorsten Dombach schrieb:

Quote:


> >     ## Connect to router.
> >     use Net::Telnet ();
> >     $t = new Net::Telnet (-timeout => 10,
> >                           -prompt  => '/routername[>#] $/');
> >     $t->open($host);

> >     ## Login.
> >     ($prematch, $match) = $t->waitfor(-match => '/username: $/i',
> >                                       -match => '/password: $/i');
> >     if ($match =~ /username: $/i) {
> >         $t->print($user);
> >         $t->waitfor('/password: $/i');
> >     }
> >     $t->print($passwd);
> >     $t->waitfor($t->prompt);

> >     ## Become superuser.
> >     $t->print("en");
> >     $t->waitfor('/password: $/i');
> >     $t->print($super_passwd);
> >     $t->waitfor($t->prompt);

> Hi Jay!

> Txs for your tip!
> I will try it soon, give then some feedback here again.

Hi Thorsten!

I have solved the problem to use Net::Telnet for Cisco-Routers.
But!!!!
I have a problem to detect a turn-off router!

I got some code from Jay which is able to detect a busy router, but for
a swiched-off router it fails!!!

In case of BUSY:
MIR.salzburg.co.at busy: timed-out waiting for password prompt ...
MIR.salzburg.co.at

In case of TURN-OFF:
scout1.salzburg.co.at offline: problem connecting to
"scout1.salzburg.co.at", port 23: Unknown Error: 0x0000274c
 ... scout1.salzburg.co.at

In this case my perl-program is killed! I can not handle this Error with
the errmode-handler!
Do you or anybody else know how to solve this???

Thanks a lot!

Klaus

Here is my code:
###################################################################

    'luna2',
    'MIR',
    'SPUTNIK',
    'pionier',
    'scout1'
    );


{
 $host = $router . ".salzburg.co.at";
 $prompt = "/" . $router . ">/";

 $tmp = &mylogin($host, $prompt);
 print $tmp . " ... $host\n";

Quote:
}

sub mylogin {

    my $s = new Net::Telnet (Timeout => 1, Prompt => $prompt)
        or return;

    $s->errmode(sub
                {
                    if ($s->timed_out) {
                        warn "$host busy: ", $s->errmsg, "\n";
                    }
                    elsif ($s->eof) {
                        warn "$host offline: ", $s->errmsg, "\n";
                    }
                    else {
                        warn $s->errmsg, "\n";
                    }
                });

    $s->open($host)
        or return;

    $s->login('username', '')
        or return;

    $s->errmode('die');
    $s;

Quote:
}

--
                                 ___     ___
                                /   \   /   \
Klaus Mairinger                |   /.\ /.\   |

________________________________ooO_\   /_Ooo_____
                                     \ /
                                      *


Mon, 23 Apr 2001 03:00:00 GMT  
 Telnet.pm and Cisco login


...

Quote:
> I have solved the problem to use Net::Telnet for Cisco-Routers.
> But!!!!
> I have a problem to detect a turn-off router!

> I got some code from Jay which is able to detect a busy router, but for
> a swiched-off router it fails!!!

I think you have no chance find out the difference between a busy
and a turned off router. Because it looks always different. This
depends on the used routing protocoll.
I saw some network, where you get an "icmp unreachable" messsage.
When you use EIGRP, you get a "host not reachable" message (and that
very fast).
So on which string do you want to trigger?

ciao
Thorsten

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

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



Tue, 24 Apr 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Telnet:Cisco Module login problem

2. Net:Telnet:Cisco.pm question

3. Problem with Telnet:Cisco pm on a 6509 switch

4. Net::Telnet::Cisco enable requires username

5. Telnet to cisco

6. Net::Telnet::Cisco - problems with different termservers

7. Net::Telnet::Cisco and unrecognized commands

8. Using the Telnet module with Cisco routers

9. Net::Telnet + Cisco AS5200

10. Cisco router telnet program

11. Net::Telnet, which shell for CISCO Routers?

12. Telnet to Cisco AS5200

 

 
Powered by phpBB® Forum Software