SSH2 PHP stopped working

RA4W

New Member
Verified Provider
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:

KuJoe

Well-Known Member
Verified Provider
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:

TruvisT

Server Management Specialist
Verified Provider
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):


<?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>
 Second that! phpseclib is nice
Where do you use this code? Any links?  :rolleyes:
I see what you did there!
 

RA4W

New Member
Verified Provider
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:
Top