#!/usr/bin/env perl
# nslookup.pl Perl implementation of Linux 'nslookup' utility, suitable for use
#             in non-interactive mode on FreeBSD and other operating systems.
#
# Copyright (c) 2020 Graham Jenkins <grahjenk@cpan.org>. All rights reserved.
# This program is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself. Revised: 2020-05-10

use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use Net::Nslookup;
use vars qw($VERSION);
$VERSION=1.01;

# Collect options, check usage
my ($type,$secs,$host,$server)=("A",15,,);
GetOptions("query=s"=>\$type,"timeout=i"=>\$secs);
die "Usage: ".basename($0)." [-query=type] [-timeout=secs] name [server]\n".
    " e.g.: ".basename($0)." -query=A -timeout=1 myhost.net [8.8.8.8]\n"
  if( ($#ARGV<0) or ($#ARGV>1) );

# Get the response
my @result;
if ( defined($ARGV[1]) ) {
  @result=nslookup(host=>"$ARGV[0]",type=>"$type",timeout=>"$secs",
                                                             server=>"$ARGV[1]")
} else {
  @result=nslookup(host=>"$ARGV[0]",type=>"$type",timeout=>"$secs")
}
 
# Print the result
if ( @result ) {
  foreach (@result) { print "$_\n" }
  exit(0);
} else {
  print STDERR "** Failed **\n";
  exit(1)
}

__END__

=head1 NAME

nslookup - a portable implentation of the Linux 'nslookup' utility

=head1 README

A perl implentation of the Linux 'nslookup' utility, suitable for use in
non-interactive mode on FreeBSD and other operating systems.

=head1 USAGE

nslookup [-query=type] [-timeout=secs] name [server]

Default values for 'query' and 'timeout' are "A" and "15".

=head1 SCRIPT CATEGORIES

UNIX/System_administration
Networking

=head1 AUTHOR

Graham Jenkins <grahjenk@cpan.org>

=head1 COPYRIGHT

Copyright (c) 2020 Graham Jenkins. All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=cut