Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Finding the IP of a local box

by a2K (Initiate)
on Sep 14, 2006 at 05:24 UTC ( [id://572868]=perlquestion: print w/replies, xml ) Need Help??

a2K has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to use a PERL script to get the IP addy assigned to my box Via DHCP. I have a Nix box set up as a gateway/router right before my broadband connection. I would like to be able to SSH into this box at any time, from anywhere, but my ISP gives me a dynamic IP assigned through DHCP. I am trying to find a way to create a PERL script that will find the assigned IP of this box, throw it in a text file, and upload it to an FTP server. I just dont know how to find out the IP using PERL. I am thinking of parsing the assigned IP out of the DHCPcd logs or some thing similar. Is there an easier way to do this? If so, what is it? If there isn't, how would i go about making PERL check these logs when ever there is activity, to find out if there was a change in the IP? Thanx for any input on this problem in advance guys. By the way, i am a totally new to PERL, but an working on learning it as part of this project. Thanks --a2K

Replies are listed 'Best First'.
Re: Finding the IP of a local box
by ikegami (Patriarch) on Sep 14, 2006 at 05:42 UTC

    The Perl FAQ suggests the following:

    use Socket qw( inet_ntoa ); use Net::Domain qw( hostfqdn ); my $my_host = hostfqdn(); my $my_ip = inet_ntoa scalar gethostbyname $host;

    You should also read up the FAQ on the name of the language.

    Updated. I misread it the FAQ the first time around.

Re: Finding the IP of a local box
by Skeeve (Parson) on Sep 14, 2006 at 06:22 UTC
    Just in case you don't want to invent a wheel twice... There is a service called dyndns.org. Maybe it is useful for you. Using dyndns you can acces you machine by a fixed name.

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Finding the IP of a local box
by borisz (Canon) on Sep 14, 2006 at 09:31 UTC
    use IO::Interface::Simple; my $if1 = IO::Interface::Simple->new('eth0_or_your_if_name'); print $if1->address, "\n";
    Boris
Re: Finding the IP of a local box
by robartes (Priest) on Sep 14, 2006 at 05:57 UTC

    On the command line, you would use ifconfig on the appropriate interface to get the IP address. Punching ifconfig into the CPAN search box results in Net::Ifconfig::Wrapper which provides a perl wrapper to ifconfig. I have never used this module myself, so I cannot comment on the quality and stability of it, I'm afraid.

    Codewise, you would examine the hash returned by IFconfig('list','','','') to get the IP address of your internet facing network interface.

    CU
    Robartes-

Re: Finding the IP of a local box
by sgifford (Prior) on Sep 14, 2006 at 17:40 UTC
Re: Finding the IP of a local box
by Milamber (Beadle) on Sep 14, 2006 at 20:56 UTC
    I agree with Skeeve's DynDNS comment, especially seeing as DynDNS is supported directly by most routers. The pointers to the Wrapper module in CPAN is pretty interesting though. Something I think I'll play around with this weekend.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://572868]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found