#!perl -w
use strict;

use Net::Telnet;
sub wf; sub tx; sub gl;

my $c = Net::Telnet->new(
  Host => 'imax.reid.loc',
  Port => 4000,
  Timeout => 5,
  Input_record_separator => "\cM\cJ",
  Output_record_separator => "\cJ",
);
$c->input_log(*STDOUT);

wf "Please enter existing";
tx "Foo";
wf "Password:";
tx "foo";
wf "Logged in";
tx "attach";
for (1..10) {gl()}

$c->close;

sub wf {
  print "waiting for $_[0]\n";
  $c->waitfor("/$_[0]/");
}

sub gl {
  my $t = $c->getline();
  $t =~ s/\c[\[.*?[a-z]//sgi;
  local $/ = "\cM\cJ";
  chomp $t;
  #print "<<< $t\n";
  $t;
}

sub tx {
  print "\n>>> ", @_, "\n";
  $c->print(@_);
}