Reply

Handling logging multi-line cli output in perl script

Techie
Posts: 16
2967     0

I'm interested in methods to handle multi-line return ouput in variables and logging it to the NetMRI custom log.

For example:

            my $output2 = $easy2->send_command("sh run int po117");
            $easy->log_message('info', "Output of $subDevName: $output2");

I'm running the command against a device that was called with new NetMRI_Easy instance within the script. I'm trying to write the output back to the primary custom log, so that it can be viewed in the Job Details viewer without opening the files specific for each device.

Generally this works fine for me, but if I try to capture output that is multi-line, I get some odd behavior. The text "LOG-:" is entered into the console of the device the script was originally executed on. This causes and error on that CLI and the script execution fails. Any ideas on how to better handle this?

*** Unknown command ***

 sniktc-as127-3560x-NET#terminal length 24 
 sniktc-as127-3560x-NET#terminal no editing 
 sniktc-as127-3560x-NET#LOG-:  
 % Unknown command or computer name, or unable to find computer address 
 sniktc-as127-3560x-NET# 

*** Error / NetMRI cli-connections/cli-error Error: 

*** ERROR: Unknown command 
*** http://10.69.15.172/api/2.8/cli_connections/log_message.json at /mnt/host/lib/NetMRI_Easy.pm line 54...
  
  ***
 sniktc-as127-3560x-NET#terminal length 24 
 sniktc-as127-3560x-NET#terminal no editing 
 sniktc-as127-3560x-NET#LOG-:  
 % Unknown command or computer name, or unable to find computer address 
 sniktc-as127-3560x-NET#

 

Re: Handling logging multi-line cli output in perl script

Adviser
Posts: 53
2968     0

Hi Sean,

Looks like you have stumbled upon a bug. The log_message method does not appear to play nicely when there are embedded line feeds and/or carriage returns in the message. I will make sure this defect gets entered into our bug tracking, however, in the interim, the recommended workaround is to do something like the following:

my $output = $easy->send_command("sh ver");

foreach my $line (split(/\r|\n/, $output)) {
    $easy->log_message("info", "$line");
}

Thanks,

- Chris

 

Thanks for the reponse Chris

Techie
Posts: 16
2968     0

Thanks for the reponse Chris and entering it into tracking. Note from my other thread, that when using the _log_message_no_login instead of log_message method this resolved the issue as well. I suppose since there is no official connection session for the job, it doesn't trigger the bug? 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You