#!/usr/bin/perl
#
#

use warnings;
use strict;

use lib '../include';

use Net::Telnet::Cisco;
use Net::Ping;

use Data::Dumper;


BEGIN {
	require "../include/config.pm";
	eval {
		require "../include/config.local.pm";
	};
}

sub ios_getroute {
	my ($t, $net) = @_;

	$t->cmd("show ip route".($net ? " $net" : '')) or return 0;;
	
	return 1;
}

my $ios_server = "noc-gw.net.tg07.gathering.org";
my $vlannumber = 16;

#my $ios = nms::ios_connect($ios_server, $nms::config::ios_user, $nms::config::ios_pass)
#	or die "Unable to connect to cisco";

my $ios = Net::Telnet::Cisco->new(Host => $ios_server,
                                  Errmode => 'return',
                                  Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/');
$ios->login($nms::config::ios_user, $nms::config::ios_pass);
$ios->enable;

#nms::ios_enable($ios);
#$ios->cmd();
#nms::ios_getroute($ios, "192.168.1.0");
#$ios->cmd("");

#nms::ios_close($ios);

# Disable paging
$ios->cmd("terminal length 0");

#my @routes = $ios->cmd("show ip route");

die "Already routed up 192.168.1.0/24" if (ios_getroute($ios, "192.168.1.0") == 1);



$ios->cmd("conf t");
$ios->cmd("int vlan $vlannumber");
$ios->cmd("ip add 192.168.1.254 255.255.255.0 secondary");
$ios->cmd("exit");
$ios->cmd("exit");

my $zyxeloldip = "192.168.1.1";

### Do things
## ZyxelNG connect


my $p = Net::Ping->new();
printf "Waiting for zyxel to come up...\n";
while (1) {
	last if $p->ping($zyxeloldip);
	print "pinging...\n";
	sleep 1;
}
print "Zyxel is alive..\n";
$p->close();

system("perl ./zyxelng.pl 192.168.1.1 192.168.2.1");

####

$ios->cmd("conf t");
$ios->cmd("int vlan $vlannumber");
$ios->cmd("no ip add 192.168.1.254 255.255.255.0 secondary");
$ios->cmd("exit");
$ios->cmd("exit");

$ios->close();


