Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to increment IPv6 address using TCL?

1,366 views
Skip to first unread message

Neha Jain

unread,
Nov 24, 2011, 7:44:08 AM11/24/11
to
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.

Georgios Petasis

unread,
Nov 24, 2011, 1:26:29 PM11/24/11
to
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...

Instead of:
set last_part 0x[lindex $parts end]
expr {$last_part + 1}

you can also do:
set last_part 0x[lindex $parts end]
incr last_part

George

Neha Jain

unread,
Nov 25, 2011, 4:41:32 AM11/25/11
to pet...@iit.demokritos.gr, jneh...@gmail.com
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...
Hi george,

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.

Waiting for your reply.

Regards,
Neha

Andreas Leitgeb

unread,
Nov 25, 2011, 6:20:48 AM11/25/11
to
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.

Andreas Leitgeb

unread,
Nov 25, 2011, 7:49:20 AM11/25/11
to
An afternote: I'm not really much involved with ipv6 yet, so my
understanding of "shortened" forms may be wrong. The code works
correct with complete strings, like the one shown, but the
interpretation of e.g. immediately subsequent colons might be
wrong. Rather *not* uncomment the "trimleft"-line...

Neha Jain

unread,
Nov 25, 2011, 12:38:23 PM11/25/11
to a...@gamma.logic.tuwien.ac.at
Dear Andreas,

Thank you very much for your reply.Currently I am using TCL 8.3.2.0
version.I already run yr prog.but its not working.Error is occuring in
the below line -

puts "the numeric value of the orig IP: [format "%llx" $numip]"

Actualy this is not a fixed format.This is IPv6 addressing
format.So,the max address will be
FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF.I just take one example.
We increase the address last block to next one and so on.But the
address not exceed FFFF.If it cross max limit i.e FFFF then 2nd last
one will be incremented & last block will be 0000.

So,please send me the details TCL prog.
Waiting for your reply.

regards,
Neha


On Nov 25, 5:49 pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
wrote:
> Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at> wrote:

Andreas Leitgeb

unread,
Nov 25, 2011, 2:00:25 PM11/25/11
to
Neha Jain <jneh...@gmail.com> wrote:
> Dear Andreas,
> Thank you very much for your reply.Currently I am using TCL 8.3.2.0
> version.

Ouch, why use such a dinosaur?
For this old version it is definitely not as convenient
to program as the code I showed for 8.5.

> puts "the numeric value of the orig IP: [format "%llx" $numip]"

Yes, this %llx thingie and some more relevant bits of my
code snippet definitely wouldn't work before tcl 8.5.

> Actualy this is not a fixed format.

What I meant with "fixed format" is, that it doesn't contain
two consecutive colons.

e.g. (I read up a bit on it, after my previous answers)
the short-format address
1:2345:6789:ab:cdef::1
would be equivalent to this "fixed format":
0001:2345:6789:00ab:cdef:0000:0000:0001

And there exist even uglier variants to get ipv4-addresses
into some kind of ipv6-wrapper format. (::ffff:127.0.0.1)

Supporting all those looks like a tedious task, that I'm not
likely to do just for fun.

If you can narrow your problem to complete forms, or at least
forms without double-colons (and non of the ipv4-bridge formats),
then there may be a chance for some algorithm.

I'll await your responses about "why stone aged Tcl 8.3.*"
and about complete forms, before writing up more code.

Neha Jain

unread,
Nov 26, 2011, 1:31:00 AM11/26/11
to a...@gamma.logic.tuwien.ac.at
On Nov 26, 12:00 am, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
wrote:
Dear Andreas,

Thnx for your reply.You are absolutely correct.I already install TCL
8.5.10 and your code is successfully execute.But this code is only
valid for fe80:0000:0000:0000:0204:61ff:fe9d:f15f.Not in general.
Suppose my IPv6 address is fe80:0000:0000:0000:0204:61ff:fe9d:ffff so
the out put will be fe80:0000:0000:0000:0204:61ff:fe9e:0000
and also if my ipv6 format is fe80::204:61ff:fe9d:ffff (or any ipv6
format,may be fe80::204:61ff) , it will give me the output
fe80::204:61ff:fe9e:0000
That's my objective.So,please send me the details code for this
format.
Waiting for your kind cooperation.
Regards,
Neha

Ashok Nadkarni

unread,
Nov 26, 2011, 3:25:32 AM11/26/11
to
Neha, check out the ip module in tcllib -
http://tcllib.sourceforge.net/doc/tcllib_ip.html

In particular, the contract and normalize commands will convert between
the various ipv6 formats. You can then use the various code snippets
provided by previous responders in this thread to achieve what you want.
You will have to put together the complete solution yourself,
particularly since the problem as you states is underspecified (what
does increment mean - Treat it as a 128-bit int? Or only the host part?
What to do on wrap around? etc.)

Andreas Leitgeb

unread,
Nov 26, 2011, 8:27:32 AM11/26/11
to
Neha Jain <jneh...@gmail.com> wrote:
> Thnx for your reply.You are absolutely correct.I already install TCL
> 8.5.10 and your code is successfully execute.But this code is only
> valid for fe80:0000:0000:0000:0204:61ff:fe9d:f15f.Not in general.

You mean, you feel unable to change the one line that contains your
example address to something that will get the original address
from somewhere?
Where would you get it from, anyway?

What's your background? Have you ever programmed in Tcl or any
other computer language? Do you intend to learn Tcl?

> Suppose my IPv6 address is fe80:0000:0000:0000:0204:61ff:fe9d:ffff so
> the out put will be fe80:0000:0000:0000:0204:61ff:fe9e:0000

I suppose you at least tried to replace the address in the code
I posted... (I'd have expected this type to work correctly)

> and also if my ipv6 format is fe80::204:61ff:fe9d:ffff (or any ipv6
> format,may be fe80::204:61ff) , it will give me the output
> fe80::204:61ff:fe9e:0000

For this you'd use the tcllib code mentioned in the other followup.
convert short-form to long (canonical) form, then apply my code
and then convert back to a shorter form...

> That's my objective.So,please send me the details code for this
> format.
> Waiting for your kind cooperation.

I think "cooperation" isn't a good term here. It's rather used with
people cooperating with authorities (e.g. for crime investigations)
or even authoritarian regimes, or based on some commercial interests.

Les Cargill

unread,
Nov 26, 2011, 4:45:57 PM11/26/11
to
--- CODE ---

namespace eval IPV6 {
set overflow 0

proc aincr { o } {
variable overflow

set o [ split $o : ]
set len [ llength $o ]
set idx $len
incr idx -1


set overflow 0

while {$idx >= 0} {
set t 0x[lindex $o $idx]

#optional check for 4 nibble elements only
if {[string length $t] != 6} { return error }

if {$t == "0xffff"} {
lset o $idx "0000"
incr idx -1
continue
}
incr t
lset o $idx [format "%04x" $t ]
break
}

# Substituted zeros will all have four (4) elements
set n [ regsub -all "0" $o "" z ]
set overflow [expr $n == [expr $len * 4 ] ]
# cleaning up...
unset z

return [join $o ":" ]
}
}

array set old {
1 fe80:0000:0000:0000:0204:61ff:fe9d:f15f
2 fe80:0000:0000:0000:0204:61ff:fe9d:ffff
3 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
4 ffff:ffff:ffff:ffff
5 ffff:ffff:fff:ffff:ffff:ffff:ffff:ffff
}

foreach a [lsort -integer [ array names old ] ] {
set b [::IPV6::aincr $old($a)]
puts "$old($a)->$b ($::IPV6::overflow)"
}


--- OUTPUT ---

C:\tcl\IPV6>tclsh85 IPV6.tcl
fe80:0000:0000:0000:0204:61ff:fe9d:f15f->fe80:0000:0000:0000:0204:61ff:fe9d:f160
(0)
fe80:0000:0000:0000:0204:61ff:fe9d:ffff->fe80:0000:0000:0000:0204:61ff:fe9e:0000
(0)
ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff->0000:0000:0000:0000:0000:0000:0000:0000
(1)
ffff:ffff:ffff:ffff->0000:0000:0000:0000 (1)
ffff:ffff:fff:ffff:ffff:ffff:ffff:ffff->error (0)

C:\tcl\IPV6>

Alexandre Ferrieux

unread,
Nov 26, 2011, 5:21:38 PM11/26/11
to
On Nov 26, 10:45 pm, Les Cargill <lcargil...@comcast.com> wrote:
>
> [code solving the problem as stated]

This worries me because:

(a) either the OP is a student leveraging the Tcl community to do his/
her homework.

(b) or there really is someone around here wanting to increment an
IPv6 address and to let the carry ripple all the way up to the highest
bits. Hope no power plant safety depends on that.

In either case, that's no good.

-Alex

Les Cargill

unread,
Nov 26, 2011, 5:58:49 PM11/26/11
to
Alexandre Ferrieux wrote:
> On Nov 26, 10:45 pm, Les Cargill<lcargil...@comcast.com> wrote:
>>
>> [code solving the problem as stated]
>
> This worries me because:
>
> (a) either the OP is a student leveraging the Tcl community to do his/
> her homework.
>

I don't particularly care. I learned to code by looking at the code of
people who were farther along the learning curve than I was.

> (b) or there really is someone around here wanting to increment an
> IPv6 address and to let the carry ripple all the way up to the highest
> bits. Hope no power plant safety depends on that.
>

I hope no meteors hit my house today, too :)

> In either case, that's no good.
>
> -Alex

Alex, clearly our Spidey Senses are out of phase...

--
Les Cargill

Erik Leunissen

unread,
Nov 27, 2011, 9:27:05 AM11/27/11
to
My suspicion was aroused likewise by the way the communication in this
thread was conducted.

Neha didn't care to speak for herself when Andreas asked her about her
background, and whether she was willing to learn Tcl at all. Of course
,we can do our own superficial social analysis, which reveals some
interesting data in this respect:

- If you track the IP-address in the "posting-host" header of Neha's
postings, using the whois database, you find that it belongs to a
company called "Bharti Airtel Ltd. - TELEMEDIA Services".

- Googling for "Bharti Airtel", you can follow the lead to this
company's web site, which indeed has a telemedia services department.

- If you google for "Neha Jain", combined with "Bharti Airtel", the very
first hit reveals what a certain Neha Jain tells about herself at LinkedIn.

If these data really belong the OP, then she isn't a student but someone
employed by a company affiliated with Bharti Airtel, a commercial IT
provider.

> In either case, that's no good.
>

I agree.

Erik.

> -Alex


--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Erik Leunissen

unread,
Nov 27, 2011, 9:27:28 AM11/27/11
to
On 26/11/11 23:58, Les Cargill wrote:
>
> I don't particularly care. I learned to code by looking at the code of
> people who were farther along the learning curve than I was.
>

That presumes that the OP has any intention to learn about coding Tcl.
What clues do you have for that presumption?

In fact, she asked for coding solutions.

She might be an employee under pressure from her commercial employer to
meet any target that has nothing to do with "learning Tcl".

By the way: did she say "thank you for the coding solutions" ?


Erik.

>> (b) or there really is someone around here wanting to increment an
>> IPv6 address and to let the carry ripple all the way up to the highest
>> bits. Hope no power plant safety depends on that.
>>
>
> I hope no meteors hit my house today, too :)
>
>> In either case, that's no good.
>>
>> -Alex
>
> Alex, clearly our Spidey Senses are out of phase...
>
> --
> Les Cargill


Message has been deleted

Neha Jain

unread,
Nov 27, 2011, 10:45:35 AM11/27/11
to
Hello every body,

Sorry for my late reply.Due to my illness I could not check my
mail.Now I am reading all this mail.1st of all than you very much for
valuable suggestion.I am not a programming back ground.I am simple
graduate.I don't have any linkdin account also.But now I will try to
learn TCL prog.using some docs.
Again thank you very much for your cooperation.
Have a nice day.
bye

Andreas Leitgeb

unread,
Nov 27, 2011, 2:33:59 PM11/27/11
to
Erik Leunissen <lo...@the.footer.invalid> wrote:
> On 26/11/11 23:58, Les Cargill wrote:
>> I don't particularly care. I learned to code by looking at the code of
>> people who were farther along the learning curve than I was.
> By the way: did she say "thank you for the coding solutions" ?

For the record: yes he/she did.

Les Cargill

unread,
Nov 27, 2011, 2:43:07 PM11/27/11
to
Erik Leunissen wrote:
> On 26/11/11 23:58, Les Cargill wrote:
>>
>> I don't particularly care. I learned to code by looking at the code of
>> people who were farther along the learning curve than I was.
>>
>
> That presumes that the OP has any intention to learn about coding Tcl.
> What clues do you have for that presumption?
>

I don't. Long ago in a galaxy far away, Usenet used to be
principally about people helping each other a bit - not in
a Utopian sense, just in a "Bob down at the car parts store"
sense.

I had ten or fifteen minutes ready at hand.

> In fact, she asked for coding solutions.
>
> She might be an employee under pressure from her commercial employer to
> meet any target that has nothing to do with "learning Tcl".
>
> By the

Oh well. This is pretty basic stuff.

> way: did she say "thank you for the coding solutions" ?
>

(S)he sure did.

>
> Erik.
>
>>> (b) or there really is someone around here wanting to increment an
>>> IPv6 address and to let the carry ripple all the way up to the highest
>>> bits. Hope no power plant safety depends on that.
>>>
>>
>> I hope no meteors hit my house today, too :)
>>
>>> In either case, that's no good.
>>>
>>> -Alex
>>
>> Alex, clearly our Spidey Senses are out of phase...
>>
>> --
>> Les Cargill
>
>

--
Les Cargill
0 new messages