Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl Script to enable Cisco

by Null_Route0 (Initiate)
on Jul 23, 2012 at 15:27 UTC ( #983191=perlquestion: print w/ replies, xml ) Need Help??
Null_Route0 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all - Just getting my feet wet for the first time with some Perl scripting and I had two questions. I am working on a script to log into a Cisco and do a "show run" and dump the output to a text file. Im using the Net::SSH::Perl module which doesnt appear to support the enable command. Im not sure how I can get it into enable mode. Also need some advice on the creation of the txt file. I have the script create a file based on the host name, but it always seems to plop a ? at the end of the txt file and Im not sure why. Any advice would be much appreciated.
#!/usr/bin/perl -w use strict; use warnings; use 5.010; ###prompt for host### print "Host IP "; my $host = <>; ##Setup connection### use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new($host); $ssh->login('user', 'pw'); ###run commands### my($run) = $ssh->cmd("show run"); ##create file and write to it### my $filename = $host; open(my $fh, '>', $filename) or die "Could not open file '$filename' $ +!"; say $fh ($run); close $fh; say 'done'

Comment on Perl Script to enable Cisco
Download Code
Re: Perl Script to enable Cisco
by aitap (Deacon) on Jul 23, 2012 at 16:04 UTC

    What do you mean by "enable command"? Shell commands are supported by remote hosts, not Perl modules.

    As for the text file, you may want to use print instead of say. The "?" character say adds is actually a newline symbol, \n. Under Windows newline symbol is different (\r\n), so you may print that instead.

    Sorry if my advice was wrong.
      Thanks for the clarification with /n What I mean by enable command is this... On a Cisco there is a whole subset of commands you can run from privileged mode. To do that you type the command "enable" and then are prompted for a password. Im just not sure how I go about scripting that out.

      I tried like this, but it didnt work

      ###run commands### $ssh->cmd("enable theEnablePassword");
      Tried like this too
      $ssh->cmd("enable"); $ssh->cmd("theEnablePassword");
      Not sure if I explained that right.
        What do these commands return? I mean, what if you try to say $ssh->cmd("enable theEnablePassword");?
        Sorry if my advice was wrong.

        Since it sounds like you'll need to your script to interactively respond to the remote device, I think that you need to use something like Expect.

        I personally have never used expect, so I can't offer up more details on how to use expect. Based on my limited knowledge of expect, I believe that it can be used to issue a command, what for a specific response, and then issue another command based on the response received.

Re: Perl Script to enable Cisco
by i5513 (Monk) on Jul 23, 2012 at 18:30 UTC

    Of course, it is ok playing with ssh / telnet / expect modules, but sometimes there are programs which work like you expect. Not need of reinvent the weel :-)

    For this case (and if you are not in Windows OS), I recommend you take a look to rancid, and its ~ "/.login/" commands

      Running Fedora 16 so I'll take a look at Rancid.

      I figured this project would be a fun introduction to the wild world of Perl =)

      Its much easier to learn things when you have some sort of context to work with and I've learned a ton, but I just wish I had a little more success getting this to work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983191]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (19)
As of 2014-09-02 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (24 votes), past polls