Is there something wrong with my coding?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Is there something wrong with my coding?

Post by 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&nbsp;E&nbsp;N&nbsp;E&nbsp;R&nbsp;A&nbsp;L&nbsp;&nbsp;&nbsp;S&nbsp;E&nbsp;T&nbsp;T&nbsp;I&nbsp;N&nbsp;G&nbsp;S</strong>
				<br /><hr size="1">
			</td>
			<td>
				<strong>U&nbsp;S&nbsp;E&nbsp;R&nbsp;&nbsp;&nbsp;A&nbsp;S&nbsp;S&nbsp;I&nbsp;S&nbsp;T&nbsp;A&nbsp;N&nbsp;C&nbsp;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>:&nbsp;&nbsp;</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" />
						&nbsp;&nbsp;&nbsp;
						<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>
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]
Last edited by FiOh on Thu Jan 11, 2007 11:14 pm, edited 2 times in total.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Which line is it failing on precisely?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Highly suspicious: while(true) ... infinite loop condition.
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

Begby wrote:Which line is it failing on precisely?
Sorry, i just edited the post.
It is this line '$tmpecho = fgets ($fp, 1024); ' I have make it in Red and Bold.

FiOh
>.<
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

FiOh wrote:
Begby wrote:Which line is it failing on precisely?
Sorry, i just edited the post.
It is this line '$tmpecho = fgets ($fp, 1024); ' I have make it in Red and Bold.

FiOh
>.<
You've posted FAR too much code to be allowed to get away with NOT using the correct highlighting tags.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's no error handling in the code. If the server for some reason responds different than expected it happens unnoticed. You should reconsider that.
In the meanwhile try

Code: Select all

$count=0;
while(true)
{
	$read = array($fp); $write=$except=null;
	if ( false === stream_select($read, $write, $except, 1))) {
		echo "<div>- idle</div>\n"; flush();
	}
	else {
		$tmpecho = fgets($fp, 1024); //Hide the string of terminal length 0
		echo '<div>', htmlentities($tmpecho), "</div>\n"; flush();
		
		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);
see http://de3.php.net/stream_select
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

To feyd: I'm sorry about not using the tags... i will learn to use it. ^^;


To volka: I've tried pasting the codes u suggested but i still cannot login to the device... now here is the error
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 108. This line of error is

Code: Select all

$gethostname=explode(">",$temps);			//seperate base on ">" so can get hostname
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Hm, line 108 isn't covered by my code snippet.
You may try

Code: Select all

function my_fgets($fp, $maxlen) {
	$read = array($fp); $write=$except=null;
	while( false===stream_select($read, $write, $except, 1) ) {
		echo '<div>', date('H:m:i'), " - idle</div>\n"; flush();
	}
	
	$s = fgets($fp, $maxlen);
	echo '<div>', date('H:m:i'), ' Debug: ', htmlentities($s), "</div>"; flush();
	return $s;
}
(untested)
and replace all fgets by my_fgets for debugging purposes. Remember to keep backup files ;)
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

volka wrote:Hm, line 108 isn't covered by my code snippet.
You may try

Code: Select all

function my_fgets($fp, $maxlen) {
	$read = array($fp); $write=$except=null;
	while( false===stream_select($read, $write, $except, 1) ) {
		echo '<div>', date('H:m:i'), " - idle</div>\n"; flush();
	}
	
	$s = fgets($fp, $maxlen);
	echo '<div>', date('H:m:i'), ' Debug: ', htmlentities($s), "</div>"; flush();
	return $s;
}
(untested)
and replace all fgets by my_fgets for debugging purposes. Remember to keep backup files ;)
May i know where should i place the above suggested coding in the file?
*Confused*

^^;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

anywhere. Just put it at the beginning, right after the first <?php
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

volka wrote:anywhere. Just put it at the beginning, right after the first <?php
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 103

Now the same error points to line 103, which is:

Code: Select all

$temps=my_fgets ($fp, 1024);					//this is the line to check for bad password and get hostname , Hello>en.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It wasn't meant to fix the problem. You should use the debug output to find the problem.
Does the server send the data you expect? my_fgets prints a timestamp, is there an unusual gap somewhere?
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

What does it means by the 'debug output'?

I've just tried the program and this time it points to another line.
What does this line means?

Code: Select all

echo '<div>', date('H:m:i'), " - idle</div>\n"; flush();
Sorry for causing so much problem here... ^^;;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's going to point to a different line almost every time. The reason why is because of the infinite loop, as I said before. volka and probably the rest of us are more interested in what information the debug code is pushing out more then where php stops.
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

feyd wrote:It's going to point to a different line almost every time. The reason why is because of the infinite loop, as I said before. volka and probably the rest of us are more interested in what information the debug code is pushing out more then where php stops.
I see. I'm sorry that i sound stupid and caused so much trouble here. >.<
May i know where is the debug code information? Where do i see it? How to i open the debug code information?

X_x
Post Reply