0

I am fairly new to php and am currently working on a website for my university. What I am trying to do is make a log in page that allows a user to establish a connection to a remote SSH server and then run linux scripts all from wihtin php. So far, I've found this code that -- I guess is supposed to allow a user to log in. I've tried running the code, but it doesn't seem to work.

Edit: When I run the code, the page simply says "function ssh2_connect doesn't exist."

<?php

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");

// log in at server1.example.com on port 22

if(!($con = ssh2_connect("server1.example.com", 22))) {
echo "fail: unable to establish connection\n"; }

else {
// try to authenticate with username and password
if(!ssh2_auth_password($con, "username", "password")) {
    echo "fail: unable to authenticate\n";
}

else {
    // allright, we're in!
    echo "okay: logged in...\n";

    // execute a command
    if (!($stream = ssh2_exec($con, "ls -al" ))) {
        echo "fail: unable to execute command\n";
    }

else {
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";
        while ($buf = fread($stream,4096)) {
            $data .= $buf;
        }
        fclose($stream);
    }
  }
 }
?>
jl1126
  • 21
  • 1
  • 6
  • And then people wonder why all of their student PII is dumped on pastebin.. – Kaylined Jul 10 '19 at 21:17
  • 1
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a [mre]. Please [edit] your question to add these details into it or we may not be able to help. – Machavity Jul 10 '19 at 21:21
  • When I run the code, the page simply says "function ssh2_connect doesn't exist." – jl1126 Jul 10 '19 at 21:40
  • 1
    Which OS you are running on, you need to install extra packages to have `ssh2_*` functions. https://www.php.net/manual/en/ssh2.installation.php – catcon Jul 10 '19 at 22:26
  • I'm running on Windows 10. – jl1126 Jul 10 '19 at 23:10
  • https://stackoverflow.com/questions/15134421/php-install-ssh2-on-windows-machine – catcon Jul 11 '19 at 01:01

0 Answers0