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
Re: Finding the IP of a local box
by ikegami (Patriarch) on Sep 14, 2006 at 05:42 UTC
|
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.
| [reply] [d/l] |
Re: Finding the IP of a local box
by Skeeve (Parson) on Sep 14, 2006 at 06:22 UTC
|
| [reply] [d/l] [select] |
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";
| [reply] [d/l] |
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.
| [reply] [d/l] |
Re: Finding the IP of a local box
by sgifford (Prior) on Sep 14, 2006 at 17:40 UTC
|
| [reply] |
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. | [reply] |
|