How to get the next ip for given ip
ex: 10.10.10.1 -> 10.10.10.2
ex: 10.10.10.255 -> 10.10.11.0

Answers were Sorted based on User's Feedback



How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10..

Answer / pranav damele

set ip "10.10.10.255"
set list [split $ip "."]
set ww [lindex $list 0]
set xx [lindex $list 1]
set yy [lindex $list 2]
set zz [lindex $list 3]

if {$zz>254} {
set zz 0
incr yy
if {$yy>255} {
set yy 0
incr xx
if {$xx>255} {
set xx 0
incr ww }
if {$ww>255} {
set ww 0
}}} else {
incr zz
}
puts "$ww.$xx.$yy.$zz"

Is This Answer Correct ?    28 Yes 4 No

How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10..

Answer / karthik

proc next_ipv4 {a} {
regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)} $a b
set b [split $b .]
if {[lindex $b 3] < 255} {
set c [lindex $b 3]
set c [expr $c +1]
set b [lreplace $b 3 3 $c]
set b [join $b .]
puts "next IP is $b"
} elseif {[lindex $b 2] < 255} {
set c [lindex $b 2]
set c [expr $c +1]
set b [lreplace $b 2 3 $c 1]
set b [join $b .]
puts "Next IP is $b"
} elseif {[lindex $b 1] < 255} {
set c [lindex $b 1]
set c [expr $c +1]
set b [lreplace $b 1 3 $c 0 1]
set b [join $b .]
puts "next IP is $b"
} elseif {[lindex $b 0] < 255} {
set c [lindex $b 0]
set c [expr $c +1]

set b [lreplace $b 0 3 $c 0 0 1]
set b [join $b .]
puts "next IP is $b"
} else {
puts "Last IPv4 address is 255.255.255.255"
}
}

Is This Answer Correct ?    3 Yes 0 No

How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10..

Answer / pavithra kumar

proc incri { no ip } {
set ip [split $ip .]
set 4_oct [ lindex $ip 3]
set 3_oct [ lindex $ip 2]
set 2_oct [ lindex $ip 1]
set 1_oct [ lindex $ip 0]


for {set i 0 } { $i < $no } {incr i } {
if {$4_oct <= 254 } {
incr 4_oct

puts [ lreplace $ip 0 3 $1_oct $2_oct $3_oct $4_oct ]
} elseif { $3_oct <=254 } {
set 4_oct 0
incr 3_oct
puts [ lreplace $ip 0 3 $1_oct $2_oct $3_oct $4_oct ]
} elseif { $2_oct <=254 } {
set 4_oct 0
set 3_oct 0
incr 2_oct
puts [ lreplace $ip 0 3 $1_oct $2_oct $3_oct $4_oct ]
} elseif { $1_oct <=254 } {
set 4_oct 0
set 3_oct 0
set 2_oct 0
incr 1_oct
puts [ lreplace $ip 0 3 $1_oct $2_oct $3_oct $4_oct ]
} else { puts " ip is full " }
}
}

Is This Answer Correct ?    2 Yes 0 No

How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10..

Answer / amarnath

#!/usr/local/bin/expect



set ip 10.20.245.250
set a [split $ip .]
puts $a
set oct1 [lindex $a 0]
set oct2 [lindex $a 1]
set oct3 [lindex $a 2]
set oct4 [lindex $a 3]

for {set i 0} {$i<=4574} {incr i } {
if {$oct4<=255} {
puts [join "$oct1 $oct2 $oct3 $oct4" "."]
incr oct4 }
if {$oct4==256} {
incr oct3
set oct4 0
puts [join "$oct1 $oct2 $oct3 $oct4" "."]
incr oct4 }
if {$oct3==256} {
incr oct2
set oct4 0
incr oct3
set oct3 0
puts [join "$oct1 $oct2 $oct3 $oct4" "."]
incr $oct4
}
}

Is This Answer Correct ?    1 Yes 0 No

How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10..

Answer / gowri

set ip "10.10.10.1"
lappend result [ regexp (\[0-9]+\) (\[0-9]+\) (\[0-9]+\) (\[0-9]+\) $ip match val1 val2 val3 val4]
puts $val1
puts $val2
puts $val3
puts $val4
if {$val1 > 255} {
set val1 0
incr val2
if {$val2 > 255 } {
set val2 0
incr val3
if {$val3 >255} {
set val3 0
incr val4
if {$val4 > 255} {
set val4 0

}}} else {
incr val5
lappend result1 "$val1.$val2.$val3.$val4"
puts $result1

}

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More Tcl Interview Questions

how to remote log in to a system A to system B ,execute commands in it and collect the log in system A from B using TCL script??  

1 Answers   Global Edge, Sandvine,


WHAT IS TCL?

4 Answers   Airtel,


how to write the startup scripts in winrunner? can any body explain with example code?

0 Answers  


write a program to the given ip is valid private address or not(192.168.1.1)?

4 Answers   HCL,


1.What are the different ways to initialize a variable. How to differentiate global and local variables, explain it through a simple tcl program. 2.Create a list of week days and print the first and last character of each day using foreach command 3.Can you write a small program to verify the given input is file or directory.Before checking, just ensure that the file/dir exists or not in the given path. If the given input is a file, findout the size and verify that the file has all read ,write and execute permission.

3 Answers   ATTO, HCL,






How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10.11.0

5 Answers   Cisco, HCL,


{Anu Anudeep Anukumar Amar Amaravathi Aruna} is their any possibility to find the letter "a"in the given list? if yes how?

3 Answers   Cisco, HCL,


Which scripting language is better among TCL Perl and Python and why? 

1 Answers   GE,


how to increment eacl element in a list ? eg: incrlist {1 2 3} =>2 3 4

9 Answers   Honeywell,


Where can find the sample tcl programs?

3 Answers  


How do you check whether a string is palindrome or not using TCL script?

10 Answers  


Test case on windows calculator?

1 Answers  


Categories