Thanks a lot. am using the 'gethostbyname() method. works fine for me, too.
-Abhi -----Original Message----- From: $Bill Luebkert [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 05, 2002 1:07 PM To: Abhimanyu_Bhola Cc: active Subject: Re: IP of local machine??? Abhimanyu_Bhola wrote: > How can I determine IP of the local machine(can be Linux or Windows) ? Lots of ways. Try one or more of these (set if (0) to if (1) to try one): use strict; use Socket; use Sys::Hostname; use Data::Dumper; $Data::Dumper::Indent=1; if (0) { # FAQ method - doesn't work for me my $host = hostname (); printf "hostname=$host; "; my $addr = inet_ntoa (scalar gethostbyname ($host || 'localhost')); print "hostaddr=$host\n"; } if (0) { # ipconfig method - too slow my @addr = get_host_address (); foreach (@addr) { print "ipconfig: addr=$_\n"; } } if (1) { # gethostbyname method - works fine for me my $host = (gethostbyname ("localhost"))[0]; printf "hostname=$host\n"; my @addr = gethostbyname ($host); splice @addr, 0, 4; print "hostaddrs:\n"; foreach (@addr) { printf "\t%s\n", &inet_ntoa ($_); } } # or in addition - get fully qualified name if (1) { use Net::Domain qw(hostname hostfqdn hostdomain); print STDOUT "fully qualified name = @{[hostfqdn]}\n"; } sub get_host_address { # return the first active interface my $addrs = &get_active_interfaces(); print Data::Dumper->Dump([\$addrs], [qw(addrs)]) if 0; #return $addrs->[0]->{'IP_addr'}; my @addrs; for (my $ii = 0; $ii < @{$addrs}; $ii++) { push @addrs, $addrs->[$ii]->{'IP_addr'}; } return @addrs; } # get_active_interfaces sub get_active_interfaces { my $ipconfig = 'c:\windows\ipconfig'; open IPC, "$ipconfig |" or die "Error piping $ipconfig: $!\n"; my @adapters; my $adapter; while (<IPC>) { chomp; next if /^\s*$/; s/^\s+//; s/\s+$//; if (/^(\d+)\s+eth/i) { # found adapter line $adapter = $1; # get number $adapters[$adapter]->{name} = $_; # save whole line } else { next if not defined $adapter; # garbage before 1st adapter if (/IP Address[\s\.]+:\s*([\d.]+)/i) { $adapters[$adapter]->{IP_addr} = $1; # IP address } elsif (/Subnet Mask[\s\.]+:\s*([\d.]+)/i) { $adapters[$adapter]->{Mask} = $1; # Subnet Mask } elsif (/Default Gateway[\s\.]+:\s*([\d.]+)/i) { $adapters[$adapter]->{Gateway} = $1; # Def. Gateway } } } close IPC; return \@adapters; } __END__ -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.tripod.com/ (Free site for Perl) -/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/ ************************************************************************** This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated. ************************************************************************** _______________________________________________ ActivePerl mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/listinfo/activeperl
