codezero codezero - 10 days ago 5
PHP Question

php code for sshpass and ssh2

I'm new in PHP coding
I'm writing new simple script but when i put this code i get blank page
can some one tell me what's wrong with this code ?

<?php
if($_POST) {
$host = $_POST['host'];
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("127.0.0.1", "22")))
{
echo "fail: unable to establish connection";
}
else
{
if(!ssh2_auth_password($con, "root", "password"))
{
echo "fail: unable to authenticate ";
}
else
{

$stream = ssh2_exec($con, "".$host."");
stream_set_blocking($stream, true);
$item = "";
while ($input = fread($stream,4096)) {
$item .= $input;
}
echo $item;
}
}

?>


sorry for my bad EN

Answer

You might have better luck with phpseclib, a pure PHP SSH2 implementation. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

If you want to let the command run for a certain amount of time before getting the output you can do $ssh->setTimeout(1). So you could do ping 127.0.0.1 on Linux, which won't stop, but still phpseclib would stop after one minute.