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?
08-29-2018 09:18 PM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide