This is the output you get when you run it:#!/usr/bin/perl use strict; use warnings; use Net::DNS::Resolver; my $hostname = 'perlmonks.org'; my $res = Net::DNS::Resolver->new( nameservers => [qw(8.8.8.8)], ); my $query = $res->search($hostname); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print "Found an A record for $hostname: ".$rr->address; print "\n"; } }
So now taking it a step further, I want to check a whole file of names. This is the code I have, but it's not working. Can someone please help and let me know where I went wrong?Found an A record for perlmonks.org: 66.39.54.27 Found an A record for perlmonks.org: 216.92.34.251 Found an A record for perlmonks.org: 209.197.123.153
#!/usr/bin/perl use strict; use warnings; use Net::DNS::Resolver; open FILE, "<hostname_check.txt" || die $!; while (<FILE>) { my $res = Net::DNS::Resolver->new( nameservers => [qw(8.8.8.8)], ); my $query = $res->search($_); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print "Found an A record for $_: ".$rr->address; print "\n"; } } } close FILE;
In reply to Need help with script with a while loop reading file by brianjb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |