3 Replies Latest reply on Jul 20, 2017 9:25 AM by jkrenzien

    Script for mass change of VLAN 1 subnet

    Ismo

      I need to change native vlan subnet for tens of switches. Instead of doing it manually by login to every switch separately I would like to do it with NCM template. But naturally IP should be different to every switch, so how should I create a script to manage that?

       

      Last part of IP remains the same, just C-class changes. For example 192.168.11.30 changes to 10.10.10.30, 192.168.11.8 changes to 10.10.10.8 and so on.

       

      All switches are Cisco 2950 and 2960 models.

       

      I got an idea, that maybe I (or at least someone(!), I'm not very familiar work templates...) would be able to create a script that takes the last part of address, saves it to variable, then adds it in the end of the second variable, and then writes all back to configuration. So I could change all IP's with one template without need to be afraid of typos...

       

      Doesn't sound so complex in theory.

       

      Also gateway need to be changed same way, but that's the same for every switch, so no need to be case sensitive script for that.

        • Re: Script for mass change of VLAN 1 subnet
          jkrenzien

          While it is possible to script what you want. It may not be as easy as you think. I am going to make a few assumptions based on what you stated.

           

          First off, I am going to approach with the belief that IP you need to change is the only IP on the SW and that they are on a single LAN. In that case we need to do a bit of work with TCL. Do NOT use this script directly, I am grabbing pieces of what I have done in the past.

           

          # Set variables for easy reuse/customization/enhancments

          set NEWIP 10.10.10.

          set IPSUB 255.255.255.0

          set NEWROUTE 0.0.0.0 0.0.0.0 10.10.10.1

          set OLDROUTE 0.0.0.0 0.0.0.0

          set NATIVEVLAN vlan1

           

          # Grab the output of show run and prepare for parsings

          set IOSOUTPUT [exec "show run interface ${NATIVEVLAN}"]

          #Insert regex line to capture the last octect of the IP (not enough coffee in me to do it yet, sorry) captured vairable = DEVIP

           

          # Finish creating the new IP

          set NEWIP ${NEWIP}${DEVIP}

           

          ios_config "ip route ${NEWROUTE}" "end"

          ios_config "interface ${NATVIEVLAN}" "ip address ${NEWIP} ${IPSUB}" "end"

          ios_config "no ip route ${OLDROUTE}" "end"

           

          # at this point the new IP and route should be in effect

           

           

          This can all be put into a script you just need get into the TCL shell (in user exec mode type tclsh)

            • Re: Script for mass change of VLAN 1 subnet
              Ismo

              In what form should regex's be written in TCL? What language it is after all? I'm just a little bit familiar with JS and C#... I've never used TCL, I didn't even understood such a thing exist in swithces! So I'm really rookie in scripting. Is it a big risk to break everything by doing something wrong?

               

              I think this regex would find right octet: (?<=192.168.11.).* but it should be written to command and in variable somehow...


              I could test this on one switch (bad thing is, I lost connection to that switch because it's not routed anymore... and if I change subnet from router, then I lost all the other switches...). So I'm going to take one switch in my table with available console cable, so I can setup everything back of **** hits the fan.

               

              Anyway, if this works for one switch why it wouldn't work for all of them. So I just put that script in NCM template and run for 50 switches. And after all is done, then change the router.

                • Re: Script for mass change of VLAN 1 subnet
                  jkrenzien

                  TCL is a scripting language and the regex command is 'regexp' (linked that to the tck.tk website for it). I may have time to work out and test the rexexp today. Once you get the regex part working and you test it on one switch, you should be able to push it to all switches, just make sure you don't save your config until you verify everything worked correctly.