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

WHAT IS TCL?

4 Answers   Airtel,


How to Swap 30 & 40 in IP address 192.30.40.1 using TCL script?

8 Answers   HCL, ONGC,


How increment a character? For example, I give 'a' and I should get 'b'.

3 Answers  


{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,


How do you find the length of a string without using string length command in TCL??

6 Answers   Hexaware,






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

10 Answers  


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

0 Answers  


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

1 Answers   GE,


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,


How to run a package in tcl

2 Answers  


Write a program to increment IP address to +10 and verify it is correct ip or not ?? example my ip is 172.122.132.143 increment this ip to +10 and verify it is valid ip or not

1 Answers   Alcatel,


How TCL works

1 Answers   Cisco,


Categories