We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x
Private

Capture output of switch commands using PERL

SysAdmin
SysAdmin asked
on
Medium Priority
116 Views
Last Modified: 2020-05-14
Please bare with me, I have never used Perl in my life and I'm attempting to modify an existing script from previous worker. Any help is much appreciated.

I have a perl script that loops through Cisco Switches / Routers and can run commands. I want to log all output and "show" commands to a log file. For example:

# create log files
open STDOUT, '>', "test.txt";
open STDERR, '>', "test-errors.txt";

# Enable Mode
@out = $session->enable($password); 

$session->cmd("show run int Gi1/0/1\n");
$session->cmd("show run int vlan 1\n");
$session->cmd("show int status | inc something-here\n");

Open in new window


This is just an example but I'm having trouble outputting it all to a text file. I've attempted to put something like this:

print $session->cmd("show run int Gi1/0/1\n");
print $session->cmd("show run int vlan 1\n");
print $session->cmd("show int status | inc something-here\n");

Open in new window


But doesn't work as expected.

Any help is much appreciated thank you
Comment
Watch Question

Dave CrossPerl programmer, author and trainer
CERTIFIED EXPERT

Commented:
Hi,

We need a little more information in order to help you. What is the $session variable? Can you show us the line of code where it is created?

Author

Commented:
Thanks, Dave. Here is some more info:

my $session = Net::Telnet::Cisco->new(Host => $ip_address, Timeout => $sec, Errmode => 'return');
my @out = $session->login($login, $password);

Open in new window

Dave CrossPerl programmer, author and trainer
CERTIFIED EXPERT

Commented:
Ok, that's helpful thanks. When you said "doesn't work as expected" doesn't work as expected in your original question, what did you mean? What unexpected behaviour did you see? Was there no output? Was there an error message? Did your computer burst into flames?

Looking at the documentation for that module, it seems that the output from the commands is returned by the "cmd()" method, so your code should work as far as I can see. You could copy the documentation more explicitly with code like this:

my @output = $session->cmd('show run int Gi1/0/1');
print @output;

Open in new window


But, honestly, I don't see how that will make much difference.

Author

Commented:
Yes, I have tried both ways:

print $session->cmd("show run int Gi1/0/1\n");

Open in new window


and

my @output = $session->cmd('show run int Gi1/0/1');
print @output; 

Open in new window


Both work.......when I start to add more commands like this:

print $session->cmd("show run int vlan 1\n");

print $session->cmd("show run int vlan 2\n");

print $session->cmd("show ip eigrp neighbors \n");

print $session->cmd("show run int vlan 3\n");

Open in new window


Is when I start seeing inconsistencies. For example, when I run the above it will log the first two commands on the first run but not the last two commands. If i run it immediately again, it shows the first 3 commands but not the last one...run it a third time and now it just shows the first two commands.

If I specify it like the document states

my @output = $session->cmd('show run int Gi1/0/1');
print @output;

Open in new window


That one command works.

I start adding more commands (by the way i found I had to make the variables different, i thought i could use same one but i guess not?)

my @output = $session->cmd('show run int Gi1/0/1');
print @output;

my @output2 = $session->cmd('show run int Gi1/0/2');
print @output2;

my @output3 = $session->cmd('show run int Gi1/0/3');
print @output3;

my @output4 = $session->cmd('show run int Gi1/0/4');
print @output4;

Open in new window


I see that same issue where it doesn't show output of all the commands maybe just first two will show. . .

Very odd i thought.
Dave CrossPerl programmer, author and trainer
CERTIFIED EXPERT

Commented:
Yes, that's odd. And, I'm afraid, beyond my (admittedly tiny) knowledge of Net::Telnet:Cisco.

I think I have one suggestion left. Perhaps the network is somehow getting confused by too many commands piled on top of each other. Maybe try inserting a call to
sleep()

Open in new window

between the commands.

Author

Commented:
Hmmm..So I added sleep commands like sleep(5) and still same outcome.

Not sure what is going on but I appreciate the help.
vogenAnalyst Programmer
CERTIFIED EXPERT

Commented:
I think you can pass timeout in cmd paramater.

Author

Commented:
Vogen, thanks for reply. Where do you suggest passing timeout command?
vogenAnalyst Programmer
CERTIFIED EXPERT

Commented:
In the module. session object, like how you have $session->cmd([..,..,]) It is mentioned in documentation.

Author

Commented:
I am specififying the timeout here, is this wrong place?

my $session = Net::Telnet::Cisco->new(Host       => $ip_address,
                                                                                  Timeout    => $sec,
                                                                                  Errmode    => 'return',
                                                                                  Input_log  => "input.txt",);

Open in new window


I've tried using Input_log and Output_log, it creates the files, but no data. Any thought on that?

I am doing this earlier in script, not sure if it matters:

open STDOUT, '>', "Logs/test.txt";
open STDERR, '>', "Logs/test-error.txt";

Open in new window

Customer Support Engineer III
CERTIFIED EXPERT
Commented:
I ran a quick test on a local test switch, and added some extra output so I could see when it sent different commands. I used basic 'print "sending command 1", etc. to see what happened.

I found that it sent multiple commands so quickly it appeared almost to be at the same time, without waiting for response.  It sent so quickly the switch only responded to one.  The output I got varied, depending on which two (or three) commands it sent this way.

When you tried sleep, it may not have been long enough perhaps, but timing can depend on the device and results returned.

Instead, try something like the changes below.  In my test, it forced the script to slow down and wait for a prompt from the switch before sending the next command.  It takes a little longer for it to complete, but once the 'waitfor' lines were added, I got good results every time.

print $session->cmd("show run int GigabitEthernet1/0/1\n");
$session->waitfor($session->prompt);
print $session->cmd("show run int GigabitEthernet1/0/2\n");
$session->waitfor($session->prompt);
print $session->cmd("show run int vlan 1\n");
$session->waitfor($session->prompt);
print $session->cmd("show run int vlan 2\n");
$session->waitfor($session->prompt);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Andy, awesome find! Thank you very much for your response. I'm going to put this to the test and see what happens on my end.  I really appreciate it. 

Author

Commented:
Andy, that worked.

Thank you for the great solution! 
Andy RansomCustomer Support Engineer III
CERTIFIED EXPERT

Commented:
Awesome, I'm glad it worked for you.  I think I'll keep my test script around, as I may very well end up doing something with it myself later.

FYI
If you need to do more with Net::Telnet::Cisco  later -- it is based on the original Net::Telnet, and uses some of the same functionality.

I ended up looking at documentation for both to eventually figure out which option helped ($session->prompt in this case). Dave Cross included a link to documentation in an earlier comment.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.