#!@PERL5@

require Net::Telnet;

use Getopt::Std;
use English;

getopts('h:p:');

#print "options given: p$opt_p h$opt_h\n";
$host=$opt_h;
$port=$opt_p;
$prompt='(=\d+= )?whois R.I.P. config> ';
$command=join(" ",@ARGV);

my $retval = 0;

sub readtoprompt #returns text read, without prompt
{
   my $ses = shift;
   my $cod;
   my $retstr;

   ($prematch, $match) = $ses->waitfor("/$prompt\$/");

    if( $match =~ /=(\d+)=/  ) {
 	$cod = $1;
    }

    return ($prematch, $cod);
}

if( $command ) {
    # this needs to be longer than the maximum query timeout in
    # case the command is "set queries 0", which waits until all 
    # queries have completed then returns
    $ses=new Net::Telnet(Timeout => (60 * 20)); 
 
    # increase the buffer length to 10 megs
    $ses->max_buffer_length(10*1024*1024);

    $ses->open(Host=>$host,Port=>$port);
    readtoprompt($ses);

    $ses->print($command);

    ($ans,$retval) = readtoprompt($ses);

    $ses->print("quit");
    $ses->close();

    print $ans;
} 
else {
    print STDERR "command missing.\n";
    $retval = -1;
}


exit($retval);


