How to obtain current IP address?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

How to obtain current IP address?

From: Bob Halpin <bhalpin@attglobal.net>
Date: Fri Sep 30 2005 - 09:06:58 CEST

Here's the situation:

A machine is connected to the net via dynamic IP address.

Periodically, I want that machine to check it's current IP#, and if it
has changed, to notify me (how it does that is not important.) (Yet)

So, how do you find out your current IP# via Tcl?

The following is my first kick at the can. Using example code from Brent
Welch's "Practical Programming in Tcl and Tk", it:

- uses the http package to fetch www.whatismyip.com

- parses the page to extract the IP#

When the laughter dies down I would be very interested in any more
elegant approaches anyone would like to suggest.

Thank's in advance.

Bob Halpin

--------------- Source -----------------
(Please excuse any ugly line-wrapping)

package require http;

proc extract_ip {Src} {
        foreach Line [split $Src \n] {
                if {[string first "<TITLE>Your IP" $Line]<0} {continue;}
                if {[regexp {([0-9]*)(\.)([0-9]*)(\.)([0-9]*)(\.)([0-9]*)} $Line match
Ip1 dot1 Ip2 dot2 Ip3 dot3 Ip4]} {
                        return "$Ip1.$Ip2.$Ip3.$Ip4";
                }
        }
        return "";
}
proc httpCallback {token} {
        upvar #0 $token state
        set Result [extract_ip $state(body)];
        http::cleanup $token;
        set ::Ip $Result;
}
set ::Ip "";
http::geturl "http://www.whatismyip.com" -command httpCallback;
vwait ::Ip;
puts "IP is: $::Ip";
return $::Ip;

-------------------------------------------------------
Received on Sat Oct 15 03:53:47 2005