ssh login using php

by Ranganathan 2014-08-30 13:00:07

Step : 1
For linking the libssh & PHP together. There's a PECL module. so let's install the following code in your system
pecl install -f ssh2
Step : 2
To access our new "ssh2.so" module which is loaded by PHP, just edit a "php.ini"
extension=ssh2.so
Example code :
<?php
if (!function_exists("ssh2_connect")) die("ssh2_connection faliure");
if(!($con = ssh2_connect("server.test.com", 22)))
{
    echo "Error: unable to establish connection
";
}
else
{

    if(!ssh2_auth_password($con, "root", "secretpassword"))
    {
        echo "Error: Authentication Faliure
";
    }
    else
    {
        echo "Status: logged in...
";
        if (!($stream = ssh2_exec($con, "ls -al" )))
        {
            echo "Error: unable to execute command
";
        }
        else
        {
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096))
            {
                $data .= $buf;
            }
            fclose($stream);
        }
    }
}
?>

 
692
like
0
dislike
0
mail
flag

You must LOGIN to add comments