#!/usr/bin/perl
# 
# Zhone_zplex_24s_B_program.pl --set2default --data=10.10.10.234
# Zhone_zplex_24s_B_program.pl --program --data=10.10.10.234
#
# This script is intended for the Zhone Zplex 10-24S B with 24 FXS ports
#
# The B version is the one with a shiny metal back panel
# The A version is the one with a painted grey 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 B version is 'admin' and Password is 'zhone' 
#       Login for A version is 'user' and Password is 'password'
#         THERE IS A DIFFERENT SCRIPT FOR THE 'A' VERSION THAT YOU SHOULD USE
#
# zSH> new ip-interface-record 1/1/1/0/ip
# addr: --------------> {0.0.0.0}: 172.16.48.200
# netmask: -----------> {0.0.0.0}: 255.255.255.0
# Save new record? [s]ave, [c]hange or [q]uit: s
# 
# 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_B_program.pl - Programs or resets a Zhone Zplex Channelbank version B\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_B_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
		$t->print("admin\r\n");			# may not need full \r\n
		$t->waitfor('/password/');	# waitfor login
		$t->print("zhone\r\n");			# may not need full \r\n
		$t->waitfor('/zSH>/');		# waitfor prompt

		if ($set2default)
			{
			$t->print("set2default\n");
			$t->waitfor('/:/');		# waitfor prompt
			$t->print("yes\n");
			$t->waitfor('/erased/');		# waitfor prompt
			sleep(10);
				$ok = $t->close;
			print "DONE\n";
			exit;
			}
		if ($program)
			{
			if (!$Q) {print "setting T1 for esf/b8zs and activating\n";}
			$t->print("update ds1 1/1/1/0/ds1\n");
			$t->waitfor('/{esf}:|{d4}:/');		# waitfor prompt
			$t->print("esf\n");
			$t->waitfor('/{b8zs}:|{ami}:/');		# waitfor prompt
			$t->print("b8zs\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/{noteligible}:|{eligible}:/');
			$t->print("eligible\n");
			$t->waitfor('/uit:/');			# waitfor prompt
			$t->print("s\n");
			$t->waitfor('/zSH>/');			# waitfor prompt

			$t->print("update if-translate 1/1/1/0/ds1\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/{down}:|{up}:/');		# waitfor prompt
			$t->print("up\n");
			$t->waitfor('/uit:/');			# waitfor prompt
			$t->print("s\n");
			$t->waitfor('/zSH>/');			# waitfor prompt

			$t->print("update line-group 1\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/}:/');		$t->print("\n");
			$t->waitfor('/{down}:|{up}:/');		# waitfor prompt
			$t->print("up\n");
			$t->waitfor('/uit:/');			# waitfor prompt
			$t->print("s\n");
			$t->waitfor('/zSH>/');			# waitfor prompt

			$t->print("get if-translate 1/1/1/0/ds1\n");
			$t->waitfor('/zSH>/');			# waitfor prompt

			$port = 1;
			while ($port < 25)
				{
				if (!$Q) {print "Activating voicefxs port $port\n";}
				$t->print("update if-translate 1/1/$port/0/voicefxs\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/{down}:|{up}:/');		# waitfor prompt
				$t->print("up\n");
				$t->waitfor('/uit:/');			# waitfor prompt
				$t->print("s\n");
				$t->waitfor('/zSH>/');			# waitfor prompt
				$port++;
				}

			$port = 1;
			$ifindex = 3;
			while ($port < 25)
				{
				if (!$Q) {print "Linking voicefxs port $port to T1 ifindex $ifindex\n";}
				$t->print("new tdm-xcon 1/$port\n\n");
				$t->waitfor('/{none}:|{voice}:/');		# waitfor prompt
				$t->print("voice\n\n");
				$t->waitfor('/}:/');					# waitfor prompt
				$t->print("$ifindex\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/}:/');		$t->print("\n\n");
				$t->waitfor('/uit:/');			# waitfor prompt
				$t->print("s\n\n");
				$t->waitfor('/zSH>/');			# waitfor prompt
				$port++;
				$ifindex++;
				}

			}

		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;















