How to obtain current IP address?
- From: Bob Halpin <bhalpin@xxxxxxxxxxxxx>
- Date: Fri, 30 Sep 2005 00:06:58 -0700
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;
------------------------------------------------------- .
- Follow-Ups:
- Re: How to obtain current IP address?
- From: Bryan Oakley
- Re: How to obtain current IP address?
- Prev by Date: Re: Compiling BLT using MinGW
- Next by Date: Difference in 8.4/8.5 text widget - Bug or Feature
- Previous by thread: My own control structures are slow
- Next by thread: Re: How to obtain current IP address?
- Index(es):