Web Analytics Made Easy -
StatCounter help with php please!! - CodingForum

Announcement

Collapse
No announcement yet.

help with php please!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with php please!!

    Hi! I'm currently doing on a pj which need to make amendment to the source code written by others. It is some sort of web-based configuration of Router/Switch using php programming. The source code which was done previously had problem showing full information of router/switch.

    Using HypterTerminal
    E.g: show run configuration information.
    It has a limited maximum no. of lines to display the informations, (30 lines). Once it reached the line tat say "more", you will have to press either spacebar to display the remaining information or enter to display line by line.

    Web-Based Configuration
    After loggin into the router config, u will be direct to it general page which display the show run config information. After the "more", we found the first 3 lines missing. Provided below is the source code tat program tis part. I have stated the comments tat I don't understand. Do hope there's ppl willin to help me solve this problem.
    This is the program:


    PHP Code:
    <? session_start(); ?>

    <?
        
    //session created for security purpose and to protect outsider directly entry the webpage
        
    if ($_SESSION['login'] == "")
        {
            echo 
    "<script type=\"text/javascript\"> alert(\"You must LOG IN to access this page.\"); </script>";
            echo 
    "<script type=\"text/javascript\"> self.location=\"Login.php\"; </script>";
            exit();
        }

    ?>

    <?

        $filename 
    "Login_Info.txt";
        
    $handle fopen($filename"r");
        
    $contents fread($handlefilesize($filename));
        
    fclose($handle);
        
    $login_info explode(","$contents);

        
    $host $login_info[0];
        
    $port 23;
        
    //$newHostname = $_POST['devicename'];

        //Start of configuration when hostnames input field is not empty.
        
    if($_POST['devicename'] != "")
        {
            
    $fp fsockopen($host,$port,$errno,$errstr);    //Open socket & for communication

            
    if(!fp)
            {
                
    $results "Error: Could not open socket connection";
            }
            else
            {
                
    //Writing to file
                
    fputs ($fp$login_info[2]."\n");
                
    fputs ($fp"en\n");
                
    fputs ($fp$login_info[3]."\n");
                
    fputs ($fp"config t\n");

                
    //Read and return output line by line
                
    fgets ($fp1024);                    //ÿûÿûÿýÿý
                
    fgets ($fp1024);                    //Blank

                
    $moredetails .= fgets ($fp1024);    //User System Verification
                
    $moredetails .= fgets ($fp1024);    //Blank
                
    $moredetails .= fgets ($fp1024);    //telnet pwd
                
    $moredetails .= fgets ($fp1024);    //enable
                
    $moredetails .= fgets ($fp1024);    //enable pwd
                
    $moredetails .= fgets ($fp1024);    //config t
                
    fgets ($fp1024);                    //ÿûÿûÿýÿý

                //configure hostname
                
    fputs ($fp"hostname ".$_POST['devicename']."\n");        //config hostname command
                
    $moredetails .= fgets($fp1024);                //hostname newhostname

                //exit from config. mode
                
    fputs ($fp,"exit\n");
                
    fputs ($fp"\r");

                
    $moredetails .= fgets ($fp1024);
                
    $moredetails .= fgets ($fp1024);
                
    $moredetails .= "\n-----------------------------------------------\n\n";
            }
            
    fclose($fp);

            
    //store latest info into login_info.txt
            
    $filename "Login_Info.txt";
            
    $store_content $login_info[0].",".$login_info[1].",".$login_info[2].",".$login_info[3].",".$_POST['devicename'];

            if(
    is_writeable($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;
                }
                
    fclose($handle);
            }
            else
            {
                echo 
    "The file $filename is not writable";
            }
        }

        
    //Display info upon entering the page.
        
    $fp fsockopen($host,$port,$errno,$errstr);

        if(!
    $fp)
        {
            
    $results "Error: could not open socket connection";
        }
        else
        {
            
    fputs ($fp$login_info[2]."\n");        //telnet password
            
    fputs ($fp"en\n");                    //enable
            
    fputs ($fp$login_info[3]."\n");        //secret password
            
    fputs ($fp"terminal length 0\n");        //show complete list of info without pausing
            
    fputs ($fp"show run\n");                //show run config. command
            
    fputs ($fp"\r");                        //enter key

            
    fgets ($fp1024);                        //ÿûÿûÿýÿý

            
    while(true)
            {
                
    $tmpecho fgets ($fp1024);

                
    //Hide the string of terminal length 0
                
    if(stristr($tmpecho,'terminal length 0') === FALSE )
                {
                    
    $moredetails .= $tmpecho;
                }

                
    //break from the loop
                
    if(substr($tmpecho,0,3) =='end')
                {
                    
    fputs ($fp"\r");
                    
    $moredetails .= fgets ($fp1024);
                    
    $moredetails .= fgets ($fp1024);
                    break;
                }
            }
        }
        
    fclose($fp);
    ?>

    <!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 [Aironet 1200 Series]</title>
    <link href="../stylesheet/cssStyle.css" rel="stylesheet" type="text/css" />
    <script src="../javascript/basicCheck.js"></script>
    </head>

    <body class="loginCSS">
    <center>
    <table width="800px" cellpadding="0" cellspacing="0" border="0">
        <tr><td><img border="0" src="../assets/images/cisco/cisco1200.gif"></td></tr>
        <tr><td><?php include('menu_cisco1200.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="80" 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>
    Last edited by Spookster; Oct 5, 2006, 10:36 AM. Reason: Added [php] [/php] tags

  • #2
    More descriptive subject

    In the future, please use a more descriptive subject when posting a question. See posting guidelines. Also please use the appropriate UBB tags when posting PHP code. Makes it easier to read.
    Spookster
    CodingForum Supreme Overlord
    All Hail Spookster

    Comment


    • #3
      wow,
      This is going to be a tough one to troubleshoot unless you can
      actually run it for testing. With that, I'm guessing it's a buffer
      problem. When it gets to "more", there are some remaining lines
      in a buffer that get cut-off, or when the next lines continue, they
      are getting dropped (something like that).

      Instead of 1024, try 2048 or 4096 in various places ...
      I'm just stabbing in the dark here.

      But I think your problem is with buffers.

      Comment

      Working...
      X