package Juniper;

sub login {
	my $class = shift @_;
	my $router = shift @_;
	if (!$$router{ip} || !$$router{login} || !$$router{keyfile}) {
		die "Unsufficient information about router. Required: ip, login, keyfile";
	}
	my $ssh = "ssh -q -l $$router{login} -i $$router{keyfile} $$router{ip}";

	return $ssh;
}

sub show_direct_routes {
	my $class = shift @_;
	my $ssh = shift @_;
	@output = `$ssh "show route protocol direct terse"`;

	my @routes;
	foreach my $entry (@output) {
		if ($entry =~ /[^0-9]([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}/) {
			my $route;
			($route) = ($entry =~ m/.*[^0-9](([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}).*/);
			push @routes, $route;
		}
	}
	return \@routes;
}

sub ping {
	my $class = shift @_;
	my $ssh = shift @_;
	my $ipaddr = shift @_;
	system "$ssh \"ping rapid $ipaddr\"";
}

sub traceroute {
	my $class = shift @_;
	my $ssh = shift @_;
	my $ipaddr = shift @_;
	system "$ssh \"traceroute $ipaddr\"";
}

sub route {
	my $class = shift @_;
	my $ssh = shift @_;
	my $ipaddr = shift @_;
	system "$ssh \"show route $ipaddr\"";
}

sub bgp {
	my $class = shift @_;
	my $ssh = shift @_;
	my $ipaddr = shift @_;
	my $options = shift @_ || "detail";
	system "$ssh \"show route protocol bgp $ipaddr $options\"";
}

sub bgp_summary {
	my $class = shift @_;
	my $ssh = shift @_;
	my $ipaddr = shift @_;
	system "$ssh \"show bgp summary\"";
}
1;
