package Extreme;
my $timeout = 30;

my $printoutsub = sub{print shift @_};

sub login {
	my $class = shift @_;
	my $router = shift @_;
	if (!$$router{ip} || !$$router{login} || !$$router{password} || !$$router{prompt}) {
		die "Unsufficient information about router. Required: ip, login, password, prompt";
	}
	$$router{prompt} = "(\\* )?".$$router{prompt}.":[0-9]* [#>]";
	#	print "Prompt: $$router{prompt}\n";

	use Net::Telnet;

	my $t = new Net::Telnet (
		Prompt => "/".$$router{'prompt'},
		Timeout => $timeout
	);
	$t->errmode("return");
	$t->open($$router{'ip'});
	$t->waitfor("/login: /");
	$t->print($$router{'login'});
	$t->waitfor("/word: /");
	$t->print($$router{'password'});
	$t->waitfor("/".$$router{'prompt'}."/");
	$t->cmd('disable clipaging session');
	$t->waitfor("/".$$router{'prompt'}."/");
	return [$t, $$router{'prompt'}];
}

sub exec_cmd {
	my $outsub = shift @_;
	my $t = shift @_;
	my $prompt = shift @_;
	my $cmd = shift @_;

	$t->cmd("$cmd");
	my $now = time();
	my $echoed = 0;
	my $supress = 1;
	while (time() - $now < $timeout) {
		my $block = $t->get(
			Timeout => $now - time() + $timeout
		);
		my $lines = ($block =~ y/\n/\n/);
		while (($lines > 0) && ($echoed < $supress)) {
			$block =~ s/[^\n]*\n//;
			$echoed ++;
			$lines --;
		}
			
		if ($block =~ $prompt) {
			$block =~ s/$prompt//;
			&$outsub($block);
			$t->close();
			return;
		}
		if ($echoed >= $supress) { &$outsub($block) };
	}
	print "\nCommand timed out\n";
	$t->close();
}

sub show_direct_routes {
	my $class = shift @_;
	my $session = shift @_;
	my $output;
	my $myoutsub = sub{ $output .= shift @_ };
	exec_cmd($myoutsub, $$session[0], $$session[1], "show iproute origin direct");
	@outlines = split(/\n/, $output);
	my @routes;
	foreach my $entry (@outlines) {
		if ($entry =~ /^\*d/) {
			my $route;
			($route) = ($entry =~ m/.*[ \t](([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2})[ \t]*/);
			push @routes, $route;
		}
	}
	return \@routes;
}

sub ping {
	my $class = shift @_;
	my $session = shift @_;
	my $ipaddr = shift @_;

	exec_cmd($printoutsub, $$session[0], $$session[1], "ping $ipaddr");

}

sub traceroute {
	my $class = shift @_;
	my $session = shift @_;
	my $ipaddr = shift @_;

	exec_cmd($printoutsub, $$session[0], $$session[1], "traceroute $ipaddr");
}

sub bgp_summary {
	my $class = shift @_;
	my $session = shift @_;
	my $ipaddr = shift @_;

	exec_cmd($printoutsub, $$session[0], $$session[1], "show bgp neighbor");
}

sub bgp {
	my $class = shift @_;
	my $session = shift @_;
	my $ipaddr = shift @_;

	if ($ipaddr !~ /\//) {
		print "You must specify the exact mask (in slash notation) for \"show ip bgp\" command on this router.\nYou can check the network mask by running \"show ip route\" for this address first.";
	} else {
		exec_cmd($printoutsub, $$session[0], $$session[1], "show bgp routes detail network $ipaddr");
	}
}

sub route {
	my $class = shift @_;
	my $session = shift @_;
	my $ipaddr = shift @_;

	exec_cmd($printoutsub, $$session[0], $$session[1], "rtlookup $ipaddr");
}
1;
