Search code examples
javascriptphphtmllinuxlogin-page

How to login to linux server using PHP code?


I want to make a GUI for automation testing tool, currently that was in CLI and we need to convert it ig GUI, now the problem i am facing is that I have selected HTML,CSS,JS for front end and PHP for backend, so can any 1 of u suggest me how to login to the server, which is having some IP? or simply how to login to linux server using PHP code?


Solution

  • You can open a connection via SSH with the "Secure Shell2" PHP module.

    https://php.net/manual/en/book.ssh2.php

    // open a SSH connection (hostname = IP or network name of the remote computer)

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

    // authenticate by login / password

    ssh2_auth_password($connection, 'username', 'password');

    and then execute a command :

    // execute a shell command on the remote computer (eg. 'php -v' to know the PHP version)

    $stream = ssh2_exec($connection, 'php -v');

    // read the result from the remote computer to your local computer

    stream_set_blocking($stream, true);

    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

    // print the result in your local computer

    echo stream_get_contents($stream_out);