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

Hi, I am trying to adjust a Cisco IOS config and need to add some extra lines of commands and they need to be added to a specific area in the IOS config

The x's are the IP address on the interface statement and i need to find interfaces that already have the ip helper-address statement defined and then append two more ip helper-address statements after the one that is already defined. The other issue is there are a number of interfaces in the IOS config that will need these extra ip helper-address applied. The common factor is any interfaces that have the ip helper-address defined add the extra two, that is what I require.

Extra IOS commands to be added:

ip helper-address x.x.x.x
ip helper-address x.x.x.x

IOS config data file example:

!
interface GigabitEthernet1/0/1
 description *** VLAN 1 **
 no switchport
 ip address x.x.x.x x.x.x.x
 ip helper-address x.x.x.x
 no ip redirects
 no ip proxy-arp
 service-policy input U_SET_DSCP
 ip ospf cost 10
 srr-queue bandwidth share 1 3 97 1
 priority-queue out 
 standby ip x.x.x.x
 standby timers 1 3
 standby priority 95
 standby preempt
!

Any help would be much appreciated.

J

  • Comment on Appending data to a file in a specific location

Replies are listed 'Best First'.
Re: Appending data to a file in a specific location
by toolic (Bishop) on May 01, 2009 at 16:53 UTC
    Loop through the lines of your input file, search for your magic string with a regular expression, then inject the extra lines. Something like this should get you started:
    use strict; use warnings; while (<DATA>) { if (/ip helper-address/) { print; print " ip helper-address x.x.x.x\n"; print " ip helper-address x.x.x.x\n"; } else { print; } } __DATA__ ! interface GigabitEthernet1/0/1 description *** VLAN 1 ** no switchport ip address x.x.x.x x.x.x.x ip helper-address x.x.x.x no ip redirects no ip proxy-arp service-policy input U_SET_DSCP ip ospf cost 10 srr-queue bandwidth share 1 3 97 1 priority-queue out standby ip x.x.x.x standby timers 1 3 standby priority 95 standby preempt !

      Hi,

      Thanks for the reply, I had used this code before, but I should have explained that you might get multiple ip helper-address statements one below the other and if you have multiple, then you will be applying the print statement more than once which is not the what I need. Is there a way to just apply the two new ip helper-address statments below more than one that has been already defined

      Example:

      !
      interface GigabitEthernet1/0/1
       description *** VLAN 1 **
       no switchport
       ip address x.x.x.x x.x.x.x
       ip helper-address x.x.x.x
       ip helper-address x.x.x.x
       no ip redirects
       no ip proxy-arp
       service-policy input U_SET_DSCP
       ip ospf cost 10
       srr-queue bandwidth share 1 3 97 1
       priority-queue out 
       standby ip x.x.x.x
       standby timers 1 3
       standby priority 95
       standby preempt
      !
      

      Thanks in advance

      James

Re: Appending data to a file in a specific location
by spectre9 (Beadle) on May 01, 2009 at 18:52 UTC

    One of the earliest uses I made of Perl was on Cisco IOS. Initially, to parse RADIUS log output, but later on, I used Expect.pm and telnet to automatically fetch router configurations, modify them, and submit them back to the router (actually, just made the IOS changes in-situ). Anyway, thought I would point out this module as it may be relevant to your task.

    "Strictly speaking, there are no enlightened people, there is only enlightened activity." -- Shunryu Suzuki
Re: Appending data to a file in a specific location
by gemoroy (Beadle) on May 01, 2009 at 17:18 UTC
    So you need defineв ifaces and there's ip's?
    ... while(/\*\*\*\() \*\*/g) { my $iface = $1; if(/\G ip adress (\d+\.\d+\.\d+\.\d+/){print "iface $iface is definde +with $1"} } ...
    I could be wrong.
Re: Appending data to a file in a specific location
by pileofrogs (Priest) on May 03, 2009 at 19:34 UTC

    Heh.. I was just about to say the same thing: Expect is good.

    Can you clarify your question? Are you asking how to talk to your Cisco device or how to edit a text file? Editing the file is easy. Talking to the Cisco device is less easy.

    --Pileofrogs

      Hi,

      Thanks for the reply, the end goal is to apply the changes to the actual Cisco devices. But at the moment I have a list of devices that have the ip helper-address defined and I only want to touch these configs and not any others and I need to adjust quiet a few. So the first pahse is to add the extra 2 ip helper-address to the configs and then apply these to the devices. But I do not want to apply the changes after the config adjustement as we have a product to do this but the product is not flexiiable enough to make these types of changes as to match any interface that has the ip helper address defined, so I though PERL would be the right tool for that job.

      Hope that explains things

      Kind Regards

      James

        Sorry, I still don't understand: Do you have text files on your desktop that need editing, or do you need to edit the files on the Cisco device?

        I had a system once where I needed to automatically make a change on a Cisco device during a failure state. I had a linux box attached to the Cisco device via serial cable. I used minicom to talk to the Cisco device. I wrote a perl script with expect that fired up minicom and talked to the Cisco. If it would be helpful, I could probably fish it out and you could take a look at it.

        -Pileofrogs