1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development SSH over PHP

Discussion in 'Software' started by Blassster, 20 Jan 2009.

  1. Blassster

    Blassster What's a Dremel?

    Joined:
    20 Dec 2003
    Posts:
    56
    Likes Received:
    0
    Hello, I was wondering if someone can help me with SSH commands to a server over a .php page.

    The project I'm doing at work requires that I connect to a server and run commands over ssh through php. I can do that, get the basic output back, but I run into a problem. When I do sudo commands, the server is waiting for me to put in the password.

    I don't know how to do this, I've tried many things. The .php page that I'm on just hangs there, waiting for the server response, but the server is waiting for me to enter the password. At what part in the .php code, after sending out the sudo command in the first place, do I also send out the password?

    I had an idea earlier but it didn't work, there's a loop catching all the data coming back, and when the loop saw the word "Password: " in the return, it thought I could send out the password to the server using fwrite and I also tried other methods. I must be doing it wrong, or using a wrong combination of commands. Overall I just used some example code on a site to connect to the server and send a basic "ls -a" command, then return the output (list of files). All I changed was the command to "sudo -l", but it's waiting for the password. Not much need for me to post my code here is what I'm saying, what I tried/added has been failing.

    This page will be accessed internally for now, and will be secured later once opened...don't worry. Heh.


    Thank you.
     
  2. FuzzyOne

    FuzzyOne

    Joined:
    19 Sep 2002
    Posts:
    1,839
    Likes Received:
    37
    you'll need to use public key based auth, I have no idea about php+key based auth, so you'll need to dig a bit further
     
  3. notmeagain

    notmeagain Minimodder

    Joined:
    29 Jan 2009
    Posts:
    561
    Likes Received:
    15

    I've recently came accross this issue at work, solved it with a bit of tinkering.

    Basically, when reading from a socket, fread will wait for the buffer to update before reading in the contents, and occasionally would hang because it hadn't recieved any information.

    I got arround this by creating a loop with a count, also because my socket was returning vast swathes of information terminated with "\n\r\n\r"


    This was a quick hack.
    Code:
    
    <?php
    echo "Attempting to open socket...<br>";
    		$sock = fsockopen("tcp://ipaddr", "port");
    		if (!$sock)
    			{
    				die("Socket Failed");
    			}
    		print ("Opened Socket<br>");
    		 #read() is a function that i wrote ;) see below
                   echo "<br>".read($sock,null)."<br>";
                   
    		echo "<br>-- Logging in --<br>";
    		write($sock, FALSE, "password", NULL);
    		echo read ( $sock , null);
                      #Logic here to detect login ok/bad
    
    ######functions##############
    
    function write( $sock, $encode, $request, $content )
    {
    # $sock = your socket ($fp)
    # $encode is specific to my socket (bool)
    # $request (string)
    # $content (string)
    
    	if ( $encode )
    	{
    		$request = $request." ".base64_encode( $content );
    	}
    	# debug -> echo $request;
           # the "\r\n" is used to simulate an "ENTER" key press, just writing to the socket does nothing without this        
    	fwrite( $sock, $request."\r\n" );
    }
    
    
    ##Read function, you can set a maximal count limit for exceptionaly large files, this also stops reading the socket if no information is recieved
    
    function read( $sock, $decode )
    {
    #$sock = your socket ($fp)
    #$decode is specific to my socket.
    
    #initiate count as 0
    $count=0;
    $res = NULL;
    $response = NULL;
    # My socket returns a double newline on command completion, yours may be singular, try to find a pattern.
    
    while ( $count < 5 && substr($res,-2)!="\n\n" )
    	{      #echo "--$res--";
    		#flush();
    		$count++;
    		$res = fread($sock,65535);
    		$response .= $res;
    	}
    		if ( $decode )
    		{
    			$response = base64_decode( $response );
    		}
    	return $response;
    }
    
    
    
    ?>
    
    It is really messy, but it got the job done.

    Hope this will help:thumb:
     

Share This Page