SSH2 PHP stopped working

Discussion in 'Coding, Scripting & Programming' started by RA4W, May 12, 2014.

  1. RA4W

    RA4W New Member Verified Provider

    17
    0
    Mar 21, 2014
    So basically ive been using this for atleast 5 months now and its been all fine now just a few days ago it just stopped working no changes has been made and i dont think any updates would cause this? as i havent done any changes at all.

    $connection = ssh2_connect('ip', 22);

    ssh2_auth_password($connection, 'root', 'oo');

    $cmd = ssh2_exec($connection, 'useradd '.$username.' -s /bin/false; echo -e "'.$password.'\n'.$password.'" | passwd '.$username.'');

     

    So basically what happens is that it connect just nicely but it changes the root password? extremely wierd i would appreciate any help i could get as i am clueless.

     


    Code:
    <?php
    ini_set('max_execution_time', 60);
    define("_VALID_PHP", true);
    
    if (isset($_POST['create'])) {
    	$username = $_POST['user_id'];
    	$password = $_POST['password'];
      
    
    	// server 1 cmd
    	$connection = ssh2_connect('ip', 22);
    	ssh2_auth_password($connection, 'root', 'pw');
    	$cmd = ssh2_exec($connection, 'useradd '.$username.' -s /bin/false; echo -e "'.$password.'\n'.$password.'" | passwd '.$username.'');
    	
    }
    ?>
    <!DOCTYPE HTML>
    <body>
    <form method="POST">
    <div class="field">
    	<label class="left">User ID:</label>
    	<input type="text" name="username">
    </div>
    <div class="field">
    	<label class="left">Password:</label>
    	<input type="text" name="password">
    </div>
    <div class="field">
    	<input type="submit" name="create" value="Create">
    </div>
    </form>
    </body>
    </html>
    
     

    Thanks!
     
    Last edited by a moderator: May 12, 2014
  2. serverian

    serverian Well-Known Member Verified Provider

    503
    338
    May 16, 2013
    Where do you use this code? Any links?  :rolleyes:
     
    GIANT_CRAB and fisle like this.
  3. KuJoe

    KuJoe Well-Known Member Verified Provider

    1,761
    1,318
    May 17, 2013
    Might I suggest switching to phpseclib, it's so much easier to use and has built in debugging. Here's your same script using phpseclib (I used my own code for the adduser part, because I know it works):

    Code:
    <?php
    ini_set('max_execution_time', 60);
    define("_VALID_PHP", true);
    include('Net/SSH2.php');
    
    if (isset($_POST['create'])) {
    	$username = $_POST['user_id'];
    	$password = $_POST['password'];
    
    	// server 1 cmd
    	$ssh = new Net_SSH2('ip');
    	if (!$ssh->login('root', 'pw')) {
    		exit('Login Failed');
    	}
    	echo $ssh->exec('pwd');
    	echo $ssh->exec('pwd');
    	$ssh->exec('/usr/sbin/adduser '.$username.' -s /bin/false');
    	$ssh->exec('echo -e "'.$password.'\n'.$password.'" | passwd '.$username.'');
    	#echo $ssh->getLog(); //uncomment for debugging
    	
    }
    ?>
    <!DOCTYPE HTML>
    <body>
    <form method="POST">
    <div class="field">
    	<label class="left">User ID:</label>
    	<input type="text" name="username">
    </div>
    <div class="field">
    	<label class="left">Password:</label>
    	<input type="text" name="password">
    </div>
    <div class="field">
    	<input type="submit" name="create" value="Create">
    </div>
    </form>
    </body>
    </html>
    
     
    Last edited by a moderator: May 12, 2014
  4. TruvisT

    TruvisT Server Management Specialist Verified Provider

    398
    144
    May 16, 2013
     Second that! phpseclib is nice
    I see what you did there!
     
  5. RA4W

    RA4W New Member Verified Provider

    17
    0
    Mar 21, 2014
    Thanks for the help ill try it out! and let you know how it works.

    EDIT:

    So i tried it same result

    at the top it displays /root /root and you can connect with the password you put in the box :/

    Any ideas?

    Actually that made it work my bad.

    Cheers! i had forgot user_id which should have been username thanks.
     
    Last edited by a moderator: May 12, 2014