There are a couple of quick and dirty ways to do this. Here's one solution:

#!perl
my $ifconfig = `ifconfig`; # the ifconfig command gives the current network information
$ifconfig =~ /inet (\d+\.\d+\.\d+\.\d+)/; # extract the ip address with a regular expression
my $ip = $1;


# write it to a text file
open (OUTPUT,">/path/to/file.txt");
print OUTPUT $ip;
close (OUTPUT);

From here you could handle special cases such as no ip address or ipv6.

The better question however is why you would want to do this when there are free systems like dyndns (dyndns.org) that let you have your own "yourname.dyndns.org" domain that points to your dynamic IP. Dyndns has mac-compatible client software as well (http://www.dyndns.org/services/dyndns/clients.html) and is free.

Zach

On Jan 5, 2004, at 1:51 PM, Stephan Hochhaus wrote:

Hello out there,

I know this has been asked before, and I have taken a look at at least one thousand websites, but I couldn't come up with a working solution as of yet.
I want to upload a textfile to my website, that contains the IP of my Mac at home (which is assigned dynamically since it connects via DSL using PPPoE). I tried various ways to get the IP, I even tried the tricky solution that can be found around the net, to parse a website that basically lists the %ENV variables. Unfortunately to no success.


Is there a way to read out my WAN IP address in Mac OS X (maybe extract it from a logfile) and write it to a file?
I am sorry if this is too basic a question, I just started my long journey to become a Perl monger.


Enjoy the new year,

Stephan




Reply via email to