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

Hi Perl monks.
I have a requirement where i connect to remote server using telnet. I am using Net::Telnet
We have 2 different type of devices.
for one type of device the following is working -

$t = new Net::Telnet (Timeout => 60, Prompt => '/%|#|>|\\$ /');

for second type of device the following is working - $t = new Net::Telnet (Timeout => 60,Prompt => '/[\$%#>] $/');
how to deal with such situations where we each device is expecting different type of prompts.
how to give one command prompt that will work for both the devices?

Replies are listed 'Best First'.
Re: Double prompt in Net::Telnet
by Corion (Patriarch) on Mar 12, 2018 at 13:16 UTC

    Combining regular expressions can be done by using alternation and parentheses, see perlre. But note that once you have a combined regular expression, both matches will always be tried. The better approach is to have a list of devices and the prompts to use with them. Even better is to configure all devices to have an identical prompt.

    I suggest that you create a list of prompt strings to be matched and other strings not to be matched and then write a regular expression (and surrounding program) that will check whether your expression actually matches the prompts and nothing else.

    Combining two regular expressions %|#|>|\\$ and [\$%#>] $ is done by wrapping each regular expression in parentheses (...) and joining these with |:

    ((%|#|>|\\$)|([\$%#>] $))

    This expression can be simplified a bit but that should "work".

Re: Double prompt in Net::Telnet
by roboticus (Chancellor) on Mar 12, 2018 at 13:45 UTC

    vasanth.easyrider:

    If you can't come up with a single regular expression to match all the prompts your different devices use, you might consider adding a prompt recognizing regular expression to your device configuration:

    my %devices = ( 'foo' => { URL => '1.2.3.4', UID => 'bob', PWD => 'b0bzP@55w0rLD', promptRex => qr/[\$%#>] $/, statusCheckCmd => 'hello' }, 'bar' => { URL => '4.3.2.1', UID => 'joe', PWD => '533KR175@uc3', promptRex => qr/[A-Z]:> /, statusCheckCmd => 'world' }, ); for my $dev (keys %devices) { my $DEV = new Net::Telnet(Timeout=>10, Prompt=>$devices{$dev}{promp +tRex}); $DEV->open($devices{$dev}{URL}); $DEV->login($devices{$dev}{UID}, $devices{$dev}{PWD}); my @status = $DEV->cmd($devices{$dev}{statusCheckCmd}); }

    I don't recall ever using Net::Telnet, but it looks like you could do it this way...

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.