2736
Views
5
Helpful
2
Replies
TCL how to increment an IP
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2018 03:02 PM
Hi,
I have create a tcl script to put ip in variable.
Exemple
Let say puts ": $ip " displays 10.10.0.0
How can I split this IP to increment only the 3rd octect by 1 to make it 10.10.1.0
What is best way to do this?
Labels:
- Labels:
-
Network Management
2 Replies 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2018 09:18 PM
Hi
$ip is your variable containing the IP address.
I would use the following to split the IP address.
regexp {(\d+)\.(\d+)\.(\d+)\.(\d+)} $ip match a b c d
Then to add +1 you will need to convert the value as integer. After that, you can use incr function to add +1 and join them back.
Here a reference well explained on IP address manipulation.
https://stackoverflow.com/questions/25712265/script-to-generate-n-number-of-valid-ip-addresses
Thanks
Francesco
PS: Please don't forget to rate and select as validated answer if this answered your question
$ip is your variable containing the IP address.
I would use the following to split the IP address.
regexp {(\d+)\.(\d+)\.(\d+)\.(\d+)} $ip match a b c d
Then to add +1 you will need to convert the value as integer. After that, you can use incr function to add +1 and join them back.
Here a reference well explained on IP address manipulation.
https://stackoverflow.com/questions/25712265/script-to-generate-n-number-of-valid-ip-addresses
Thanks
Francesco
PS: Please don't forget to rate and select as validated answer if this answered your question
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2019 04:37 PM
proc incr_ip {IP {val 1}} { upvar 1 $IP ip set octs [split $ip "."]; if {[llength $octs] != 4} { error " Invalid IP address provided: $ip" } set pow_list [list 3 2 1 0]; set loop_list [list 1 2 3 4]; #CONVERT TO IP ADDRESS TO INTEGER set ip_int [expr [join [lmap oct $octs pow $pow_list l $loop_list { if {$oct > 255 || $oct < 0} { error " Invalid IP address provided: $ip\n Error at octet $l: $oct\n Value outside of range (0-255)." } expr {$oct*int(pow(256,$pow))}; }] " + "]]; #ADD OR SUBTRACT VALUE incr ip_int $val; #CONVERT BACK TO IP ADDRESS FORMAT set ip [join [lmap pow $pow_list { set Roct [expr {$ip_int/int(pow(256,$pow))}]; set ip_int [expr {$ip_int - $Roct * int(pow(256,$pow))}]; subst $Roct }] "."]; } set IPaddress 192.257.0.1 incr_ip IPaddress
