#!/usr/bin/perl
# 
# Zhone_zplex_24s_A_program.pl --set2default --data=10.10.10.234
# Zhone_zplex_24s_A_program.pl --program --data=10.10.10.234
#
# This script is intended for the Zhone Zplex 10-24S A with 24 FXS ports
#
# The A version is the one with a painted grey back panel
# The B version is the one with a shiny metal back panel
# 
# This script requires that the Zplex 10 has been configured for ethernet and
# given an IP address. It is only possible to set the network settings through a
# serial connection on the zplex10. Also if you 'set2default' you will need to go 
# back and configure the ethernet again. Here are basic instructions on how to
# configure your zplex10 for the internet:
#
# NOTE: Login for A version is 'user' and Password is 'password'
#       Login for B version is 'admin' and Password is 'zhone' 
#         THERE IS A DIFFERENT SCRIPT FOR THE 'B' VERSION THAT YOU SHOULD USE
#
# user> SET CPUCFG:MASK:1:255.255.255.0
# user> SET CPUCFG:IP:1:10.10.10.234
# 
# Copyright 2006 Matt Florell <vicidial@gmail.com> License GPLv2
#

	$MT[0] = '';

$timerZ = time();
$timerLAST =0;
$secX = time();
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$year = ($year + 1900);
	$mon++;
	if ($mon < 10) {$mon = "0$mon";}
	if ($mday < 10) {$mday = "0$mday";}
	if ($min < 10) {$min = "0$min";}
	if ($sec < 10) {$sec = "0$sec";}
	$filedate = "$year$mon$mday$US$secX";
	$headerdate = "$mday/$mon/$year";
	$querydate = "$year-$mon-$mday";
	$querytime = "$hour:$min:$sec";
	$timestamp = "$mon/$mday/$year $hour:$min:$sec";

### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
	$del = '!!!';
	$i=0;
	while ($#ARGV >= $i)
	{
	$ARGS = "$ARGS $ARGV[$i]";
	$i++;
	}

	if ($ARGS =~ /-help/)
	{
	print "SCRIPT: Zhone_zplex_24s_A_program.pl - Programs or resets a Zhone Zplex Channelbank version A\nAllowed run time options:\n [--qUiEt] = quiet\n [--tEsT] = test\n [--help] = help (this screen)\n [--set2default] = This will remove all configurations and reset the device\n [--program] = This will set device for 24 channel Loopstart B8ZS-ESF T1 channelbank interface\n\n EXAMPLE:\n Zhone_zplex_24s_A_program.pl --program --data=10.10.10.234 (this would fully program the channelbank device at the IP address 10.10.10.234)\n\n";

	exit;
	}
	else
	{
		if ($ARGS =~ /--set2default /)
		{
		$set2default=1;
		}
		if ($ARGS =~ /--program /)
		{
		$program=1;
		}

		if ($ARGS =~ /--qUiEt /)
		{
		$Q=1;
		}
		if ($ARGS =~ /--tEsT /)
		{
		$T=1;
		if(!$Q) {print "\n ----- TESTING MODE -----\n\n";}
		}

		if ($ARGS =~ /--data=/i)
		{
		#	print "\n|$ARGS|\n\n";
		@data_in = split(/--data=/,$ARGS);
			$IP = $data_in[1];
		}
		else
		{
		$IP = "10.10.8.204";
		}

	}
}

else
{
print "no command line options set, defaults used\n\n\n\n\n\n";

		$IP = "10.10.8.204";
}
### end parsing run-time options ###

	if (!$IP) {$IP=9;}


$suf = '.txt';
$outfile  = "./OUT_zplex.log";
$telnetlogfile  = "./TELNET_zplex.log";

### open the error out file for writing ###
open(out, ">>./$outfile")
		|| die "Can't open $outfile: $!\n";


	if ($set2default) {$ACTION = 'RESET UNIT TO FACTORY DEFAULT';}
	if ($program) {$ACTION = 'PROGRAM UNIT AS 24 CHANNEL T1 CHANNEL BANK';}

if (!$Q)
	{
	print "going to $ACTION zplex at $IP\n";
	}

print out "$querydate $querytime ----- going to $ACTION zplex at $IP\n";
close out;


use Net::Telnet;
		@outside_dial = @MT;

		$t = new Net::Telnet (Port => 23,
							  Timeout => 20,
							  Prompt => '/.*[\$%#>] $/',
							  Output_record_separator => '',);
		$fh = $t->dump_log("$telnetlogfile");
		$t->open("$IP");
		$t->waitfor('/login/');		# waitfor login
			sleep(1);
		$t->print("user\n");
		$t->waitfor('/assword/');	# waitfor password or Password
			sleep(1);
		$t->print("password\n");
		$t->waitfor('/user>/');		# waitfor prompt
			sleep(1);

		if ($set2default)
			{
			$t->print("ZIP\n");
		#	$t->waitfor('/:/');		# waitfor prompt
				sleep(5);
			$t->print("YES\n");
		#	$t->waitfor('/erased/');		# waitfor prompt
			sleep(20);
				$ok = $t->close;
			print "DONE\n";
			exit;
			}
		if ($program)
			{
			# GET PMCUR:ALLCUR:1	# see current settings

			#	SET T1CFG:STATE:1:ACTV
			#	SET T1CFG:FRAME:1:ESF
			#	SET T1CFG:CODE:1:B8ZS
			#	SET T1CFG:LINELEN:1:00
			#	SET T1CFG:CLKSRC:1:WAN1

			if (!$Q) {print "setting T1 for esf/b8zs, external clock, <132 ft cable and activating\n";}
			$t->print("SET T1CFG:STATE:1:ACTV\n");	# turn on WAN1 T1 port
			$t->waitfor('/user>/');					# waitfor prompt
				sleep(1);
			$t->print("SET T1CFG:FRAME:1:ESF\n");	# set frame to ESF
			$t->waitfor('/user>/');					# waitfor prompt
				sleep(1);
			$t->print("SET T1CFG:CODE:1:B8ZS\n");	# set line coding to B8ZS
			$t->waitfor('/user>/');					# waitfor prompt
				sleep(1);
			$t->print("SET T1CFG:LINELEN:1:00\n");	# set line length to <132 feet
			$t->waitfor('/user>/');					# waitfor prompt
				sleep(1);
			$t->print("SET T1CFG:CLKSRC:1:WAN1\n");	# set the clock source to WAN1
			$t->waitfor('/user>/');					# waitfor prompt
				sleep(1);


			#	SET FXSCFG:STATE:1:ACTV
			#	SET FXSCFG:MODE:1:FXS
			#	SET FXSCFG:TYPE:1:LS

			#	SET FXSCFG:STATE:2:ACTV
			#	SET FXSCFG:MODE:2:FXS
			#	SET FXSCFG:TYPE:2:LS

			$port = 1;
			while ($port < 25)
				{
				if (!$Q) {print "Activating voicefxs port $port\n";}
				$t->print("SET FXSCFG:STATE:$port:ACTV\n");	# Activate the FXS port
				$t->waitfor('/user>/');						# waitfor prompt
					sleep(1);
				$t->print("SET FXSCFG:MODE:$port:FXS\n");	# set the port to standard FXS
				$t->waitfor('/user>/');						# waitfor prompt
					sleep(1);
			### Port and timeslot not needed, defaults OK
			#	$t->print("SET FXSCFG:WAN:$port:1\n");		# tie the port to WAN1 T1 port
			#	$t->waitfor('/user>/');						# waitfor prompt
			#		sleep(1);
			#	$t->print("SET FXSCFG:TS:$port:$port\n");	# set the timeslot for the port
			#	$t->waitfor('/user>/');						# waitfor prompt
			#		sleep(1);
				$t->print("SET FXSCFG:TYPE:$port:LS\n");	# set the port to standard Loop Start
				$t->waitfor('/user>/');						# waitfor prompt
					sleep(1);

				$port++;
				}

			}

		sleep(2);
		$t->print("logout\n");
		sleep(2);
			$ok = $t->close;
		print "DONE\n";

if (!$Q)
	{
	print "DONE\n";
	}

### calculate time to run script ###
$secY = time();
$secZ = ($secY - $secX);
$secZm = ($secZ /60);
if(!$Q) {print "script execution time in seconds: $secZ     minutes: $secZm\n";}


exit;

