Hi All,
I am trying to use functions in my script and I am not able to achieve what I want.
The script is to based on PhpTelnet found on this link;
http://www.geckotribe.com/php-telnet/
In my script, when someone selects a device using a drop down, the script should login into the switch and run few commands on it.
Here is the HTML code of the form page:
<html>
<head>
<title>Default Confiruation Tool</title>
</head>
<body bgcolor="#EBFEEF">
<FORM METHOD=POST ACTION="dcmain.php">
<center>
<table border="1" width="45%" id="table1" bordercolorlight="#000000" bordercolordark="#000000">
<tr>
<td width="30%" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB">
Select Device</td>
<td style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB" width="62%">
<select size="1" name="selectdevice">
<option selected value="select">Select</option>
<option value="SV2-21">SV2-21 : ASA 5540</option>
</select></td></tr>
</tr>
<tr>
<td width="30%" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB">
TFTP Server IP</td>
<td style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB" width="62%">
<Input size=40 maxlength=50 NAME="tftpserver"></td>
</tr>
<tr>
<td width="30%" height="26" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB">
Path & Image Name (/.../..)</td>
<td height="26" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB" width="62%">
<Input size=40 maxlength=50 NAME="pathimagename"></td>
</tr>
<tr>
<td width="30%" height="26" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB">
Public Interface VLAN</td>
<td height="26" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB" width="62%">
<Input size=10 maxlength=50 NAME="pubvlan"></td>
</tr>
<tr>
<td width="30%" height="26" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB">
Private Interface VLAN</td>
<td height="26" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB" width="62%">
<Input size=10 maxlength=50 NAME="privvlan"></td>
</tr>
<tr>
<td colspan="2" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB"><Center>
<INPUT style="font-family: Times New Roman; font-size: 8pt; color: #ffffff; border-style: solid; border-color: #ffffff; background-color: #000000" TYPE=submit value="Start Process">
</td>
</tr>
<tr>
<td colspan="2" style="border-style: solid; border-width: 1px" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#DEFDDB">
<p style="margin-top: 0; margin-bottom: 0">NOTE: </p>
<p style="margin-top: 0; margin-bottom: 0">1) TFTP Server and Path & Image name are only required when you want
to <b>erase the flash</b> and load an image you want.</p>
<p style="margin-top: 0; margin-bottom: 0">2) Leave Public and Private
VLAN blank for defaults.</td>
</tr>
</table>
</center>
</body>
</html>
Here is the dcmain.php:
<?php
require_once "PHPTelnet.php";
$user=$PHP_AUTH_USER;
$pass=$PHP_AUTH_PW;
$tftpasa=('copy tftp://'. $tftpserver . $pathimagename .' flash:');
function defaultvlan()
{
$telnet = new PHPTelnet();
$shprivport=('sh port '. $privport);
$setpubvlan=('set vlan ' . $pubvlan . ' ' . $pubport);
$setprivvlan=('set vlan ' . $privvlan . ' ' . $privport);
print "----$selectdevice : STARTING BASIC PROCESS----<br/>";
print "----$selectdevice : Changing switchports to default----<br/>";
//----Changing switchports to default----
$result = $telnet->Connect('10.66.79.251','','');
switch ($result) {
case 0:
$telnet->DoCommand('', $result);
echo $result;
$telnet->DoCommand('en', $result);
echo $result;
$telnet->DoCommand('', $result);
echo $result;
$telnet->DoCommand('', $result);
echo $result;
//$telnet->DoCommand('sh port ' . $pubport, $result);
//echo $result;
//$telnet->DoCommand('', $result);
//echo $result;
$telnet->DoCommand($shprivport, $result);
echo $result;
//$telnet->DoCommand('', $result);
//echo $result;
//$telnet->DoCommand($setpubvlan, $result);
//echo $result;
//$telnet->DoCommand('', $result);
//echo $result;
//$telnet->DoCommand($setprivvlan, $result);
//echo $result;
//$telnet->DoCommand('', $result);
//echo $result;
//sleep(20);
print "----$selectdevice : DONE Changing switchports to default----<br/>";
?>
<BR>
<?
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
break;
case 1:
echo '[PHP Telnet] Connect failed: Unable to open network connection';
break;
case 2:
echo '[PHP Telnet] Connect failed: Unknown host';
break;
case 3:
echo '[PHP Telnet] Connect failed: Login failed';
break;
case 4:
echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';
break;
//----DONE Changing switchports to default----
}
}
?>
<?php
//----SV-2-21 : Start----
if($selectdevice == "SV2-21"){
//----SV-2-21 : Basic Start----
if (empty($tftpserver) && empty($pubvlan) && empty($privvlan))
{
$pubport = "5/13";
$privport = "5/14";
$pubvlan = "205";
$privvlan = "214";
defaultvlan();
}
}
?>
Something about dcmain.php:
When a user selects a device in this case its only SV2-21, I would like it to run different functions if the tftpserver & pubvlan & privvlan value is empty. I would run other functions on the input I get on the pubvlan or tftpserver.
When I execute the script, I get the following output:
---- : STARTING BASIC PROCESS----
---- : Changing switchports to default----
SV-3-16
SV-3-16 (enable)
SV-3-16 (enable) sh port
Port Name Status Vlan Level Duplex Speed Type
OUTPUT TRUNCATED
The problem is that the script always executes the command "sh port" and is not taking the $privport. If everything is working fine, "sh port 3/15" command should execute on the switch.
The idea of using functions in this case is to avoid several lines of coding for each device (I have around 40 devices)
Where am I going wrong? Please suggest.
Thanks in advance,
Guru.