Is there something wrong with my coding?
Posted: Thu Jan 11, 2007 9:00 pm
feyd | Please use
Thank you for the help and very sorry for the trouble.
FiOh
^^;;
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi everyone here,
I am new to PHP but am assigned to work on an existing web-based project done using PHP. This web-based project is used to connect to networking devices via two different connection method; via Direct Telnet and via Terminal Server. When i tried to connect to a device, i have to login to the device.
I have no problem connecting to the networking devices via the Direct Telnet but i am not able to connect to the networking devices via the Terminal Server. I have also tried to 'ping' to the devices using the command prompt and all the devices are working fine and were connected.
When i tried to login to the device via Terminal Server, the web page returns me this error:
[b][color=red]Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Group\Apache2\htdocs\terminalserver_D\cisco2612\Login_Process.php on line 119[/color][/b]
Below is the coding for the page that has the error. I refer to the line of the error pointed at from the error message above but i could not understand the error. Does anyone knows what went wrong with the coding? The error on Line 119 is $tmpecho = fgets ($fp, 1024); within the while(true) loop.
[color=green][b]Login_Process.php[/b][/color]Code: Select all
<? session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SMART Multi-Vendor Network Deployment System -- CISCO [Router 2612 Series]</title>
<link href="../stylesheet/cssStyle.css" rel="stylesheet" type="text/css" />
<?
$ip = "172.16.221.210";
$username ="cisco"; // $_POST['username'];
$password ="cisco"; // $_POST['password'];
$enable_password ="class"; // $_POST['enable_password'];
$host = $ip;
$port = 23;
$fp = fsockopen ($host, $port, $errno, $errstr, 10);
if (!$fp)
{
echo $results .= "<br><font face=verdana color=red size=2>";
echo $results .= "<p align=center>";
echo $results .= "<b>ERROR: Could Not Open Socket Connection. Please try again.</b>";
echo $results .= "</font>";
exit();
}
else
{
fgets ($fp, 1024); //junk line
//****************************************************************
//clear terminal line
$p=0;
$n=0;
while($p<4)
{
fputs ($fp, "cl li 35");
fputs ($fp, "\r");
fputs ($fp, "\r");
fgets ($fp, 1024);
fgets ($fp, 1024);
fgets ($fp, 1024);
$p++;
}
while($n<4)
{
fputs ($fp, "cl li 34");
fputs ($fp, "\r");
fputs ($fp, "\r");
fgets ($fp, 1024);
fgets ($fp, 1024);
fgets ($fp, 1024);
$n++;
}
//****************************************************************
//end clear terminal line codes
$device="2r2";
$x=0;
$s=0;
fputs ($fp, $device);
while ($s<3)
{
fputs ($fp, "\r\n");
$s++;
}
while ($x<4)
{
fgets ($fp, 1024);
$x++;
}
//seccond negotiation required to access other devices via terminal server
fputs($fp,chr(0xFF).chr(0xFD).chr(0x01));
fputs($fp,chr(0xFF).chr(0xFD).chr(0x03));
fputs($fp,chr(0xFF).chr(0xFB).chr(0x18));
fputs($fp,chr(0xFF).chr(0xFB).chr(0x1F));
fputs($fp,chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x3C));
fputs($fp,chr(0xFF).chr(0xF0));
fputs($fp,chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x41).chr(0x4E).chr(0x53).chr(0x49));
fputs($fp,chr(0xFF).chr(0xF0));
//end second negotatiation
//fputs($fp,"\r"); //enter console by inputting \r
//fgets ($fp, 1024); //junk text
fputs($fp,"\r"); //get started by inputting \r
fgets ($fp, 1024); //blank
fputs($fp,$password."\r"); //enter telnet password
fgets ($fp, 1024); //user access verification:
fgets ($fp, 1024); //blank
fgets ($fp, 1024); //password:
fputs($fp,"en"."\r");
fputs($fp,$enable_password."\r"); //enter enable password
fgets ($fp, 1024); //password:
$temps=fgets ($fp, 1024); //this is the line to check for bad password and get hostname , Hello>en
fgets ($fp, 1024); //password: (for enabled)
//*******************************
//get hostname codes
$gethostname=explode(">",$temps); //seperate base on ">" so can get hostname
$hostname=$gethostname[0];
//*******************************
fputs ($fp, "terminal length 0\n");
fputs ($fp, "show run");
fputs ($fp, "\r");
$count=0;
while(true)
{
$tmpecho = fgets ($fp, 1024);
//Hide the string of terminal length 0
if(stristr($tmpecho,'terminal length 0') === FALSE )
{
$moredetails .=$tmpecho;
}
//break from the loop
if(strstr($tmpecho,"end"))
{
fputs ($fp, "\r");
//fputs ($fp, "exit"); //break connection with the router and let it time out, alternative should be found
$moredetails .=fgets ($fp, 1024);
break;
}
}
fclose($fp);
}
$filename = 'Login_Info.txt';
$store_content = $ip.",".$username.",".$password.",".$enable_password.",".$hostname;
// Lets make sure the file exists and is writable first.
if (is_writable($filename))
{
/*----------------------------------------------------------------------------
In our example we are opening $filename in append mode.
The file pointer is at the bottom of the file hence
that is where $somecontent will go when we fwrite() it.
----------------------------------------------------------------------------*/
if (!$handle = fopen($filename, 'w'))
{
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (!fwrite($handle, $store_content))
{
echo "Cannot write to file ($filename)";
exit;
}
$_SESSION['login'] = $username;
echo "<script type=\"text/javascript\"> self.location=\"General.php\"; </script>";
fclose($handle);
}
else
{
echo "The file $filename is not writable";
}
?>
</head>
<body class="loginCSS">
<center>
<table width="800px" cellpadding="0" cellspacing="0" border="0">
<tr><td><img border="0" src="../assets/images/cisco/cisco2612.gif"></td></tr>
<tr><td><?php include('menu_cisco2612.html'); ?></td></tr>
<tr><td height="20px"></td></tr>
<tr><td align="left">
<!--CENTER SECTION-->
<table width="100%" cellpadding="5" cellspacing="5" border="0">
<!--HEADERS-->
<tr align="left" valign="top"><td width="70%">
<strong>G E N E R A L S E T T I N G S</strong>
<br /><hr size="1">
</td>
<td>
<strong>U S E R A S S I S T A N C E</strong>
<br><hr size="1">
</td></tr>
<!--LEFT COLUMN: DISPLAY INFORMATION & SETTINGS-->
<tr><td align="center">
<form name="General" method="post" action="<?php echo $PHP_SELF; ?>" onSubmit="return general_check();">
<table width="75%" cellpadding="0" cellspacing="0" border="0">
<!--Display Box-->
<tr align="center"><td colspan="3">
<strong>INFORMATION:</strong><br />
<textarea cols="90" rows="15" readonly><? echo $moredetails; ?></textarea>
</td></tr>
<tr><td colspan="3" height="20px"></td></tr>
<!--SETTINGS FOR CONFIGURATION-->
<tr align="center"><td colspan="3">
<strong>SETTINGS:</strong><br />
<hr size="1">
</td></tr>
<!--set hostname-->
<tr>
<td valign="top" width="25%"><strong>Host Name</strong></td>
<td valign="top"><strong>: </strong></td>
<td valign="top"><input type="text" name="devicename" id="devicename" /></td>
</tr>
<tr><td colspan="3" height="20px"></td></tr>
<!--submit or cancel current configuration-->
<tr align="center"><td colspan="3">
<input type="submit" name="submit" id="submit" value="Save" />
<input type="reset" name="cancel" id="cancel" value="Cancel" />
</td></tr>
</table>
</form>
</td>
<!--RIGHT COLUMN: INSTRUCTIONS-->
<td align="left" valign="top">
[<strong>Hostname</strong>] :
<br />
To specify or modify the host name for your network server.
</td></tr>
</table>
</td></tr>
<tr><td height="20px"></td></tr>
<!--COPYRIGHT-->
<tr><td align="center"><?php include ('../include/copyright.html') ?></td></tr>
</table>
</center>
</body>
</html>FiOh
^^;;
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]