cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5121
Views
0
Helpful
6
Replies

TCL Script to read out and change interface vlan IP address

packettracer86
Level 1
Level 1

Hello guys,

can someone help me out with a TCL script for some 2960-CG Switches?

I need to change the IP Address of an interface vlan in the third octet for about …. uhm … many switches and they are not at my location so I don’t have console access :/

 

Could you write me a script which reads out the IP address of an interface vlan XXX

and sets the new address with a just in the third octet different address?

 

So this should look like following:

 

OLD:

int vlan 123

Ip address 10.20.30.40 255.255.252.0

 

NEW:

Int vlan 123

Ip address 10.20.50.40 255.255.252.0

 

 

Thank you very much!

Regards,

packettracer86

6 Replies 6

Dan Frey
Cisco Employee
Cisco Employee

I'm not sure what the ask is here.  You want the script to read in the ip address from a vlan, will it always be vlan123?  If there are multiple vlan interfaces which vlan(s) need the IP address to be changed?   Will the third octet always change from 30 to 50, or will it always be +20 from the existing 3rd octet?

Once the above is known is it your intention to manually log into every switch and paste a script into the tcl shell for the changes to take place?  How do you intend to deploy the script?

 

Here are the answers to the questions:

1. The VLAN ist always interface vlan123.

2. There are no other INTERFACE vlans -> it is the management vlan for 2960 miniswitch

3. Yes the third octet will always change from 30 to 50

4. Now to the question how to deploy the script! First i thought about roll out the script in combination with a kron job (kron on the switch to start the script on a specific time..) over our management system BUT at the last night i had a nightmare.

My nightmare was the ip default-gateway it is allready set to the "new" ip range 10.20.48.1 255.255.252.0.

So the only chance to fix this messed up configs is to initiate/start another script on the next L3-Device which has an IP address in the same subnet OR to manually change the ip address on every (about 150) switches ://

Do you see any chance to run some kind of script on the L3 device which connects via telnet/ssh (based on a ip adress list i have) to the miniswitches and load in a script which changes the ip address??

(Mark to myself: Next time check configs generated via script twice.. )

 


 

I'm thinking of a policy that would do this:

  • script will run on the upsteam router and execute "show cdp neighbors detail | inc IP address: 10" so it can get the ip address of your 2960.   If you have multiple adjencies with 10 address the last address will be used.
  • The ip address that is obtained in the step above is presumably the ip address of the 2960.  Telnet to that address.
  • On the 2960 give vlan 123 the new ip address.
  • 2960 already has the updated default gateway.
  • The next hop ip of the default gateway on the 2960 would be used to update the ethernet ip address of the upstream L3 device.  Will need to identify the router ethernet port in the cdp table (unless it is always the same port on all your routers)?

The EEM script would have to be loaded onto the flash drive of all your routers.

Is the CDP dependency OK in your environment?

 

 

Hey Daniel,

 

that sounds exactly what i THOUGHT i would need. I just checked the "master" config of the miniswitches and  ... well there is an access list on the vty line which will make the whole plan .... useless!

I am really sorry for wasting your time!! :/

It looks like the only way to change the ip address is to manually connect to every switch via the consoleport.

 

If you are still interestet in helping me, i have a last idea just to make it a "bit" easier to change the IP.

 

I connect to the con port and login by hand. Than i copy/paste in a script which will get started with the last line i paste (i hope you understand what i mean! i paste the script and the command to start that one).

And this script just reads out the ip address of interface vlan 123 and changes the ip in the third octect from x.x.30.x to x.x.50.x and makes a wr.

 

Thanks and regards!

 

This should do what you want.   Paste this in when you are in exec mode:

tclsh
set mask "255.255.252.0"
set result [exec sh ip int brief | inc Vlan123]
regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)} $result match ip
regexp {([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)} $ip match first second third forth
regsub  ($third) $third "50" third
set newip $first.$second.$third.$forth
ios_config "int vlan123" "ip address $newip $mask"
set result [exec sh ip int brief | inc Vlan123]
wr

 

Dan

Hello Daniel,

thank you for your response!

I will try out this tcl script in the next days and write back how it worked!

 

Regards