Tuesday, 27 October 2009

Unix myip

Always been missing a quick way (read: UNIX CLI util) of revealing the IP currently show to the global internet, which using NAT could be quite cucumbersome to find out.

The trick below will do the trick :-)


$ cat bin/myip
#!/bin/sh
if [ -n "`whereis curl`" ]; then
IP=`curl -s http://rickvanderzwet.nl/playground/ip.cgi`
else
IP=`fetch -qo - http://rickvanderzwet.nl/playground/ip.cgi`
fi

if [ -n "$IP" ]; then
HOST=`dig +short -x ${IP}`
fi
echo "$IP ($HOST)"



$ cat /var/www/playground/ip.cgi
#!/usr/bin/perl -w

print "Content-length: " . length($ENV{'REMOTE_ADDR'}) . "\n";
print "Content-type: text/html\n\n";
print $ENV{'REMOTE_ADDR'};

No comments:

Post a Comment