What’s my IP address?

Useful question, this is how I answer it on my windows systems.

Its a TCL/TK script that invokes the windows XP shell command ping.

As noted, I use an information graphic to decorate the box. This is not part of activestate’s tcl/tk distribution, and thus needs to be copied to $tklibrary/images. (See also Scripting on Windows, on this wiki.)

I am not happy with the triming of the ip address, which is displayed into the address variable using square brackets. Since these have a specific syntactical meaning, I can’t find the syntax to remove them from the string.

wm title . {Query site ip?}
wm iconname . {Query site ip?}
wm geometry . 225x65+500+100
# This is not part of the package, it needs to be copied here 
image create photo infopic -file [file join $tk_library images info.gif]

set pingreply {} 
# windows XP 
set pingreply [exec ping -n 1 davelevy.info] 
set address [ lindex $pingreply 2 ] 
# only tested on one ip address, do I need to subtract two from length 
# to remove the first and last. What is the syntax to remove leading and 
# trailing []. 

set daddress [string range $address 1 [expr [ string length $address ] - 2]]
set messagetext " public tcp/ip no : $daddress" 
set buttontext " Done "

frame .top 
frame .bottom

label .top.icon -image infopic 
frame .top.f -width 50 label .top.f.mess -text $messagetext

pack .top.f.mess -side top -fill x 
pack .top.icon .top.f -side left
button .bottom.dismiss -text $buttontext -command "exit" 
pack .bottom.dismiss pack .top .bottom

proc exit {} { destroy . }

The script can be downloaded whatsmyip.zip.

Chris advises that

set daddress [string trim $address {[]} ]

removes the brackets from the address and is obviously much neater and is character based. It implements a remove the unwanted character rule, not remove the first and last characters.

Related Posts

One thought on “What’s my IP address?

Leave a Reply

Your email address will not be published. Required fields are marked *