CEED CEED - 4 months ago 17
PHP Question

PHP execute command after exectuing node in PHP

I want to execute a second command after executing the first but its not working for me... This is the code i have

<?php
include('Net/SSH2.php');
$owed = $_POST["value"];
$server = "127.0.0.1";
$username = "xxx";
$password = "xxx";
$command1 = "cd /var/www/ww && node bot.js";

$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $password)) {
exit('Login Failed');
}
echo $ssh->exec($command1);


This is all fine and its working till this step, BUT.. the problem is after executing the NODE is asking for VALUE to proceed the task, im getting the value on the first index of the script but i dont know how to execute it after the first command.

picture

// edit

I need to pass $_POST["value"] as a second command.

Answer

As documentation says you can use write in this case

5.1.4. read() / write() vs. exec()

write() sends input - such as a command - to the server. Normally, operating systems don't process commands until the Enter key is hit, so to mimic this, you'll need to send a "\n", a "\r\n" or a "\r" depending on the operating system the server you're connecting to is using. Note that although the input will most likely be a command it doesn't have to be.

What you send will be parsed by whatever is expecting user-entered text. So if you type in "passwd\n" the input - until "\n" is entered - will be parsed by passwd as a new or existing password - not as a command to be ran by the shell

so you probably want something like this:

<?php

    include('Net/SSH2.php');
    $owed = $_POST["value"];
    $server = "127.0.0.1";
    $username = "xxx";
    $password = "xxx";

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

    echo $ssh->exec('cd /var/www/www');
    echo $ssh->exec('node bot.js');
    echo $ssh->write($owed);