vasanth.easyrider has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, i have a requirement where i need to connect to 900 routers using telnet and execute certain commands and capture the output. I am using Net::Telnet module to achieve my requirement. But i have a problem managing the router login prompt. It is impossible for me to know the login prompt of 900 routers. Is it possible for me to use regular expressions in Net::Telnet module to manage login prompts or anyother way to use regular expressions in login prompts...........please advice

Replies are listed 'Best First'.
Re: Regular expressions in shell prompts
by Corion (Patriarch) on Feb 12, 2018 at 14:11 UTC

    Yes. Net::Telnet supports regular expressions for detecting the prompt. The example in the SYNOPSIS section already shows how to use a regular expression.

    The first step I would do is issue a mandate to change all (login) prompts on all routers to a single, identical scheme so that your script becomes simpler to maintain. That way any prompt that does not fit your scheme becomes the problem of the person buying/installing it and not yours.

Re: Regular expressions in shell prompts
by karlgoethebier (Abbot) on Feb 12, 2018 at 14:16 UTC
    "...connect to 900 routers using telnet and execute certain commands and capture the output..."

    This seems to be some big monitoring task. You should consider to use Nagios or Icinga. See also Alerting On Critical State Changes.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Regular expressions in shell prompts
by shmem (Chancellor) on Feb 12, 2018 at 14:25 UTC

    The argument for prompt in the constructor of Net::Telnet in fact is a regular expression. So the task at hand here is to craft a valid regular expression for all 900 routers. If you are having problems with that, feel free to ask.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Regular expressions in shell prompts
by VinsWorldcom (Prior) on Feb 12, 2018 at 19:15 UTC

    Just a quick aside, if these are Cisco routers Net::Telnet::Cisco adds some enhancements to the regular Net::Telnet (by subclassing) to specifically address Cisco routers - including the prompts, and both login and enable prompts as well.

Re: Regular expressions in shell prompts
by Anonymous Monk on Feb 12, 2018 at 14:47 UTC
    I strongly echo the admonition to use a network monitor such as (specifically) Nagios. This subsystem will let you approach this requirement in-reverse – specify the physical device and execute a particular command on it – and it can easily do the same thing periodically. It also gives you an effective alternative to the "wad of network traffic" that querying 900 routers in a loop would do ... and an alternative for "what to do when the program dies, of course, on the 899th iteration."