Neha Jain <
jneh...@gmail.com> wrote:
> On Nov 24, 11:26 pm, Georgios Petasis <
peta...@iit.demokritos.gr>
> wrote:
>> Στις 24/11/2011 14:44, ο/η Neha Jain έγραψε:
>> > hello everybody.i am working in TCL scripting
>> > language(mainly in IPv6 addressing format).But i am very new with this
>> > language.It will be better for me if you give me some solutions.
>> > My problem-
>> > I have fixed IPv6 address.Which is-
>> > fe80:0000:0000:0000:0204:61ff:fe9d:f15f
>> > Using TCL prog.language I want to increase by 1.So for this example my
>> > next value will be - fe80:0000:0000:0000:0204:61ff:fe9d:f160 and so
>> > on,just like a loop.
>> > It will be better if you written the TCL code for this prog.
>> proc next_ip {ip} {
>> set parts [split $ip :]
>> set last_part 0x[lindex $parts end]
>> set next [format %x [expr {$last_part + 1}]]
>> lset parts end $next
>> }
>> next_ip fe80:0000:0000:0000:0204:61ff:fe9d:f15f
>> fe80 0000 0000 0000 0204 61ff fe9d f160
>> But you have to fix the case the last part reaches FFFF...
> I just done some basic things.
> 1.My ipv6 address is - fe80:0000:0000:0000:0204:61ff:fe9d:f15f
> Using my TCL script i just separate the address.The
prog.is given
> below
> set next_ip "fe80:0000:0000:0000:0204:61ff:fe9d:f15f"
> set test [split $next_ip : ]
> puts "$test"
> 2.Incrementing the hex address.Not full string.
> set ip_value "FF19"
> scan $ip_value "%x" final_value
> incr final_value
> set new_hex_value [format "%X" $final_value]
> puts "$new_hex_value"
> But I can't increment the total string.
> Please help me the full tcl code for this address.
set old_ip "fe80:0000:0000:0000:0204:61ff:fe9d:f15f"
set numip 0; set block 65536
foreach f [split $old_ip ":"] {
set nf [scan "0$f" "%x"]; # "0$f": be safe for empty fields
set numip [expr { $numip*$block + $nf }]
};# add up the fields to a bignum.
puts "the numeric value of the orig IP: [format "%llx" $numip]"
incr numip ;# guess what :-)
set next_ip {}
while {$numip != 0} {
set f [expr {$numip % $block}]
set numip [expr {$numip / $block}]
set sf [format "%04x" $f]
#set sf [string trimleft $sf 0];# strip all leading zeros
set next_ip [linsert $next_ip 0 $sf]
}
set next_ip [join $next_ip ":"]
This will also support zero-trimmed fields on input, and
if you uncomment the "trimleft" line, then also output
a trimmed version of the incremented ip.
PS: Hurray for Tcl's bigint-math.