On 07/11/13 23:31, Frank Vino wrote:
> Could you please let me know the perl script how to find DNS IP
> address of system.?

Testing Shawn Wilson's code:

2013-07-12 21:13:45 dpchrist@desktop ~/sandbox/perl
$ cat ip-address.pl
#!/usr/bin/perl
use strict;
use warnings;
my $file = "/etc/resolv.conf";
open(my $fh, "<", $file) or die "Can not open $file: " . $!;
my @dns;
while (<$fh>) {
    next unless /nameserver +([0-9\.]+)/;
    push @dns, $1;
}
print "@dns\n";

2013-07-12 21:14:13 dpchrist@desktop ~/sandbox/perl
$ perl ip-address.pl
192.168.1.1

The code works with strict and warnings, but won't find your Internet IP address if you have a NAT firewall between your host and the Internet.


Testing James Alton's code:

2013-07-12 21:18:34 dpchrist@desktop ~/sandbox/perl
$ cat ip-address2.pl
#!/usr/bin/perl
use strict;
use warnings;
use feature say;
use Net::DNS;
foreach (Net::DNS::Resolver->new->nameservers) { say; }

2013-07-12 21:18:41 dpchrist@desktop ~/sandbox/perl
$ perl ip-address2.pl
Subroutine BEGIN redefined at ip-address2.pl line 5.
Bareword "say" not allowed while "strict subs" in use at ip-address2.pl line 4.
BEGIN not safe after errors--compilation aborted at ip-address2.pl line 5.

The code fails with strict enabled.


2013-07-12 21:18:50 dpchrist@desktop ~/sandbox/perl
$ cat ip-address2.pl
#!/usr/bin/perl
#use strict;
use warnings;
use feature say;
use Net::DNS;
foreach (Net::DNS::Resolver->new->nameservers) { say; }

2013-07-12 21:20:15 dpchrist@desktop ~/sandbox/perl
$ perl ip-address2.pl
Unquoted string "say" may clash with future reserved word at ip-address2.pl line 4.
192.168.1.1

The code works with strict disabled, but issues a warning and won't find your Internet IP address if you have a NAT firewall between your host and the Internet.


If your host is behind a NAT firewall and you want to find your firewall's Internet IP address, I'd suggest:

1. Put a Perl CGI script on an Internet host that prints $ENV{REMOTE_ADDR}, or find a site that offers such functionality:

    http://whatismyipaddress.com/

2. Write Perl script that fetches the above and parses the result. For the first task, you might want to consider LWP:

    http://search.cpan.org/~gaas/libwww-perl-6.05/lib/LWP.pm


HTH,

David

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to