[REBOL] Re: How to get allocated ip number
From: gscottjones:mchsi at: 16-Oct-2002 5:03
Hi, Michel,
From: "Goasampis Michel"
> I need to manipulate the ip number my PC obtain from
> the provider when connect to internet.
>
> To get it, I hack something with the 'get-modes function which give this
> result on my linux pc :
> >>probe get-modes tcp:// 'interfaces
> [
> make object! [
> name: "lo"
> addr: 127.0.0.1
...
> so I do :
>
> all-interfaces: get-modes tcp:// 'interfaces
> internet-interf: all-interfaces/3
> my-ip: copy to-string internet-interf/addr
>
> but it isn't verry elegant, and does'nt work on all pc (if more or less
interfaces are found).
> Please help me, I hope there is a more efficient way like
> my-ip: copy some/voodoo/path...
...
I hesitated to answer yesterday, in case someone else had a "whiz bang"
suggestion. I do not speak with authority, but I believe that there are
several issues here. In addition to having variable numbers of tcp
interfaces, depending on the machine, the names given to an individual
interface may not be consistent across platforms. If no one "knows" the
answer to this question, then a little feedback will quickly resolve it. My
platform returns names that look like:
name: "lo0"
name: "if17474259"
The first evidently refers to the local (loopback) ip and the second must
refer to my network interface card. I do not know how this number is
generated (is it an encoding of the NIC MAC number?).
Besides the variability in the number of interfaces and the variability in
the naming of the interfaces, I thought your solution was fine because it a)
works, and b) it is readily understood. You can avoid one variable
intermediate by writing the full path out, like:
internet-interf: all-interfaces/3/addr ; yields the desired IP tuple
There are many other ways to manipulate the block containing the various IP
interfaces. The next example shows how to iterate through all interfaces
foreach interface all-interfaces [print interface/addr]
If you know that you are looking for a particular class of IP, you could
further refine the result similar to the following:
foreach interface all-interfaces [
if find interface/name "ppp" [
print interface/addr
]
]
will find interfaces that use the ppp protocol (namely, dial-up
connections).
Hope this helps. Please don't worry about the language issue. I've been
speaking English for 43 years, and still haven't figured it out. :-) So
please ask further questions, especially if I missed the point.
--Scott Jones