For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > Finding the current IP of my laptop









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Finding the current IP of my laptop
Emmanuel Lesouef

2004-04-22, 5:32 pm

Hello all,

I am trying to build a little script (my first one...) that launches several
commands depending on the current IP address of my laptop.

I would like to use a perl module instead of grep'ing the result of
ifconfig.

Do you have so guidelines to give me ?

Thanks in advance.

--
Emmanuel Lesouef
Luke Bakken

2004-04-22, 5:32 pm

> I would like to use a perl module instead of grep'ing the result of
> ifconfig.
>=20
> Do you have so guidelines to give me ?
>=20
> Thanks in advance.


Getting the IP is pretty platform dependent. If you're on Windows you
can use Win32::IPHelper, but since you mentioned ifconfig I'm assuming
you're not. I'd check out search.cpan.org, and failing that, maybe look
at what's available in your /proc filesystem if your OS has one.
Rajesh Dorairajan

2004-04-22, 7:40 pm

use IO::Socket;

$ip = inet_ntoa((gethostbyname($HOSTNAME))[4]);

Rajesh

> -----Original Message-----
> From: Emmanuel Lesouef [mailto:e.lesouef@taika.fr]
> Sent: Thursday, April 22, 2004 1:02 PM
> To: beginners@perl.org
> Subject: Finding the current IP of my laptop
>
>
> Hello all,
>
> I am trying to build a little script (my first one...) that
> launches several
> commands depending on the current IP address of my laptop.
>
> I would like to use a perl module instead of grep'ing the result of
> ifconfig.
>
> Do you have so guidelines to give me ?
>
> Thanks in advance.
>
> --
> Emmanuel Lesouef
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Rajesh Dorairajan

2004-04-22, 7:40 pm

Sorry. Missed out getting hostname.

use IO::Socket;
use Sys::Hostname;

$HOSTNAME = hostname();
$ip = inet_ntoa((gethostbyname($HOSTNAME))[4]);

Rajesh

> -----Original Message-----
> From: Rajesh Dorairajan
> Sent: Thursday, April 22, 2004 2:18 PM
> To: 'Emmanuel Lesouef'; beginners@perl.org
> Subject: RE: Finding the current IP of my laptop
>
>
> use IO::Socket;
>
> $ip = inet_ntoa((gethostbyname($HOSTNAME))[4]);
>
> Rajesh
>
>


Smoot Carl-Mitchell

2004-04-22, 9:33 pm

On Thu, 22 Apr 2004 14:20:43 -0700
"Rajesh Dorairajan" <rajesh.dorairajan@tumbleweed.com> wrote:

> Sorry. Missed out getting hostname.
>
> use IO::Socket;
> use Sys::Hostname;
>
> $HOSTNAME = hostname();
> $ip = inet_ntoa((gethostbyname($HOSTNAME))[4]);


This is okay provided your laptop has a valid host to IP address
translation available. On Unix typically either an entry in /etc/hosts
or a DNS A record entry.

Take a look at Net::Interface on CPAN. It appears to let you look at
interface information in a system independent way.

--
Smoot Carl-Mitchell
Systems/Network Architect
email: smoot@tic.com
cell: +1 602 421 9005
home: +1 480 922 7313
Wc -Sx- Jones

2004-04-22, 10:31 pm

Rajesh Dorairajan wrote:
> Sorry. Missed out getting hostname.
>
> use IO::Socket;
> use Sys::Hostname;
>
> $HOSTNAME = hostname();
> $ip = inet_ntoa((gethostbyname($HOSTNAME))[4]);
>
> Rajesh


# Either way this only works if /etc/hosts is populated:

use IO::Socket;
use Sys::Hostname;

$HOSTNAME = hostname("postfix.org");
$ip = inet_ntoa((gethostbyname($HOSTNAME))[4]); print "\n$ip\n";

# prints 192.168.1.69 regardless...

__END__

use IO::Socket;

"\n$ip\n";
$ip = Socket::inet_ntoa((gethostbyname(localho
st))[4]); print "\n$ip\n";
$ip = Socket::inet_ntoa((gethostbyname(chasecr
))[4]); print "\n$ip\n";
$ip = Socket::inet_ntoa((gethostbyname(linksys
))[4]); print "\n$ip\n";
$ip = Socket::inet_ntoa((gethostbyname(loghost
))[4]); print "\n$ip\n";
$ip = Socket::inet_ntoa((gethostbyname(imac))[4]); print "\n$ip\n";

__END__

This only prints out what is
known in /etc/hosts -

127.0.0.1 localhost
192.168.1.69 insecurity.org chasecr loghost
192.168.1.1 linksys fw
192.168.1.10 airlan house
192.168.1.50 imac angela

Otherwise it gives:
Usage: Socket::inet_ntoa(ip_address_sv) at IP line x.

error and exits immediately...

Cheers :)
--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
http://jaxpm.insecurity.org/
http://cis4dl.insecurity.org/
Emmanuel Lesouef

2004-04-23, 4:30 am

<Message original de Smoot Carl-Mitchell en date du jeudi 22 avril 2004 à 17:07>

> On Thu, 22 Apr 2004 14:20:43 -0700
> "Rajesh Dorairajan" <rajesh.dorairajan@tumbleweed.com> wrote:
>

First of all, thank you all for your answers.
[color=darkred]
>
> This is okay provided your laptop has a valid host to IP address
> translation available. On Unix typically either an entry in /etc/hosts
> or a DNS A record entry.


And that is exactly the problem :)
In /etc/hosts :
127.0.0.1 localhost lothlorien

Which, is, not the "really" good answer. In fact, I am trying to find a way
to have the eth0 IP address.

>
> Take a look at Net::Interface on CPAN. It appears to let you look at
> interface information in a system independent way.
>


I just had a look at this module. It is promising. Maybe it is the answer.

For those interested :

http://search.cpan.org/~srz/Net-Int...04/Interface.pm

Thank you all.

> --
> Smoot Carl-Mitchell
> Systems/Network Architect
> email: smoot@tic.com
> cell: +1 602 421 9005
> home: +1 480 922 7313
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


--
Emmanuel Lesouef
Société Taika - e.lesouef@taika.fr
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com