Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Unwanted extra text appearing in array

by shadowbiz (Acolyte)
on Jun 13, 2003 at 15:57 UTC ( [id://265718]=perlquestion: print w/replies, xml ) Need Help??

shadowbiz has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a script to go through a list of devices and will find if dhcp relay is enabled on them. The command I am running I am feeding into an array and want that array dumped to a file line for line. But it is not working. Any help will be appreciated. Here is the script
sub main { my($devicename, $ip, $mask, $ts); my($line, @modem, $result_line); open(DEVICELIST, "<Devicelist") or die "Can't open Devicelist file : $ +!\n"; while ($line = <DEVICELIST>) { $devicename = $line; chomp($devicename); # login to the device $ts=loginDevice("username", "passwd", "enpasswd", $devicename) +; open(OUTFILE, ">>Device-info") || die("Cannot open Device-info: $!\n"); print OUTFILE "Device $devicename\n"; @modem = $ts->cmd("show run | inc dhcp relay"); foreach $result_line (@modem) { print OUTFILE "$result_line\n"; } print OUTFILE "\n"; close(OUTFILE); $ts->print('exit'); } exit; }
Here is a sample output of what I am getting.

Device device1
device1

Device device2
device2

I have replace the real device names for security reasons. But you can get an idea. From what I can gather somehow the device names ends up in the array??? I have varified that the script logs in fine. Any help would be appreciated.

shadow(biz)

Title edit by tye

Replies are listed 'Best First'.
Re: Unwanted extra text appearing in array
by Muoyo (Novice) on Jun 13, 2003 at 19:03 UTC
    Your problem is probably in the output from the method call to cmd(); Try piping the output from that command directly to a file and take a look at it. Make sure you're getting what you expect. (Not really very helpful) -muoyo
Re: Unwanted extra text appearing in array
by fglock (Vicar) on Jun 13, 2003 at 17:24 UTC

    From the Net::Telnet docs:

        @lines = $t->cmd("who");

    which, I think, means you'd better end your subroutine with:

    return $telnetsession->cmd('term len 0'); }

    update: now that I reread it: no, this won't help.

Re: Unwanted extra text appearing in array
by chip (Curate) on Jun 13, 2003 at 18:10 UTC
    OK, the additional info helps me see what you're doing. But you're basically scripting an interaction with a machine we know nothing about. I have no idea what system "show run | inc dhcp relay" is a valid command on, much less what its output should look like or how to parse it.

    In other words, you don't have a Perl question, you have a question about the system you're connecting to. And we don't know anything about that.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Looks like a Cisco router to me =)

      Update: Confirmed here way before me.

      --
      Allolex

Re: Arrays
by chip (Curate) on Jun 13, 2003 at 16:18 UTC
    Without the source code for &loginDevice we have no way to help you.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Here is the source for &loginDevice
      sub loginDevice { my ($name, $password, $enable, $devicename) = @_; my ($telnetsession); $telnetsession = new Net::Telnet( Timeout => 10, Host => $devicenam +e ); $telnetsession->waitfor('/Username: /'); $telnetsession->print("$name"); $telnetsession->waitfor('/Password: /'); $telnetsession->print("$password"); $telnetsession->waitfor("/>/"); $telnetsession->print("enable"); $telnetsession->waitfor('/Password: /'); $telnetsession->print("$enable"); $telnetsession->prompt('/#|>/'); $telnetsession->cmd('term len 0'); return $telnetsession; }
        There are a couple things for you to look into:
      • Net::Telnet::Cisco since it looks like a Cisco machine you are trying to login to.
      • Also the $ts->login() method in Net::Telnet
      • Also look into the dumplog method in Net::Telnet It will show you what you are sending and recieving to the device.

        Updated after pondering the real question

        Oh yeah, the reason the device name is in the array is because it is part of the output you get before the prompt is matched. If your device is Cisco7000 your prompt is Cisco7000#, your code looks for the # in the prompt. So when you do a command you will get
        Cisco7000# sho run <CR> <-- This is where the data in the array starts output...blahblahblah Cisco7000# ^ this is where the data in the array stops
        So you see, you get everything between the <CR> and the last # symbol, INCLUDING the hostname part of the new prompt.

        The easiest solution is to ignore the last element of the array. The best solution is to incorperate the name of the device into the prompt string you are looking for, but you will need to watch for case sensitivity, since you pass the name of the device to the subroutine anyway, you can just change the prompt line to $telnetsession->prompt('/$devicename[#|>]/');. Note: this regex untested, you probably need to tweak it

        "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://265718]
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found