1

EDIT 1 : Removed the sentence : ", well first thing first the " have to be escaped such as \" , but" , my mistake this doesn't make sense.

EDIT 2 : Added content of $cmd

EDIT 3 : added intelligible code sample

I'm experiencing a problem when using Perl's Net::SSH::Expect module to communicate with Juniper network devices (JunOS).

There is a specific need to use double quotes in JunOS for certain commands. When I connect manually to the device, a fictive command such as : show something | match "this | that" works correctly.

When using this command through the Net::SSH::Expect module the command won't return an output at all.

Here's how the session is opened

$session = Net::SSH::Expect->new(
                                host => $device->{ip},
                                port => $port // 22,
                                ssh_option => $options // '',
                                user => $username,
                                password => $password,
                                raw_pty => 1,
                                timeout => $session_timeout)

and the commands are being sent using "$session->send($cmd);" , then retrieved with the before() function.

$session->send($cmd);
                
unless($session->waitfor($device->{promptSSH}, $cmd_timeout)) {
    $logger->warn("$device->{hostname} ($device->{ip}) : SSH - Echec (Timeout) de la commande $cmd");
    $data->{'timeout'} = 1; # status flag
} 
@{$data->{'output'}} = $session->before();
return $data;

Where $cmd would contain as per example : show interfaces diagnostics optics | match "Laser output power |Laser receiver power"

Output when we escape the double quotes

If I don't escape the double quotes, we get this strange output

Output if we don't escape the double quotes

Please note that the commands are read from a simple text file in the system, here's an extract

Extract of the file where the commands are read from

I'm totally clueless, can anyone help me please ?

Cheers

  • Please show the content of `$cmd` – Håkon Hægland Oct 30 '20 at 16:54
  • Here's the command, sorry about the confusion with the escaping : show interfaces diagnostics optics | match "Laser output power |Laser receiver power" – Luc Larochelle Oct 30 '20 at 18:57
  • *"When using this command through the Net::SSH::Expect module the command won't return an output at all."* According to the [documentation](https://metacpan.org/pod/Net::SSH::Expect) the `send()` command does not return anything – Håkon Hægland Oct 31 '20 at 18:59
  • True ... well I'll edit the question tomorrow then and add details about how the output is captured but this piece of code has been working for years. Problem seems to be with the double quotes or one of the pipes in this specific command but I don't understand why. – Luc Larochelle Nov 01 '20 at 16:01
  • Here we go, the code sample should explain how I retrieve the output after sending the command. This command should return output, but it's empty. I've printed to a file so we can have a look at what's going on but there is nothing in the file. – Luc Larochelle Nov 02 '20 at 14:26
  • 1
    Can you try the `read_all()` method instead of the `before()` method ? – Håkon Hægland Nov 02 '20 at 18:58
  • I tried, I finally get and output that'll demonstrate the behavior ... I'll edit the question and add explanations – Luc Larochelle Nov 04 '20 at 15:49
  • 1
    What happens if you do not read the commands from the file? That is: just putting the command explicitly into the `send()` like `$session->send('show interfaces diagnostics optics | match "Laser output power |Laser receiver power"');` – Håkon Hægland Nov 04 '20 at 16:43
  • it behaved normally ! but I don't understand why and how the double quotes or the pipe can cause an issue ? In the code, I assigned the command as a string between single quotes in $cmd before using send($cmd) though – Luc Larochelle Nov 04 '20 at 20:59
  • Great, maybe then update your answer with how you read the command from file and how you assigned `$cmd` ? – Håkon Hægland Nov 04 '20 at 21:01
  • 1
    I don't think I can answer the question since someone closed it. But here we go : using read_all() instead of before() solved the issue. Still, I remember switching from read_all() to before() in the past, to solve an issue with execution time, so I patched the code to allow an exception for that particular type of device, otherwise the before() func is used everywhere else. Thanks for pointing that out, your help was and is still highly appreciated. – Luc Larochelle Nov 06 '20 at 15:42

0 Answers0