mahdi mahdi - 1 year ago 83
Linux Question

Run some Linux command in PHP trail together

I wrote some php code that connected to vps through ssh

I know

can do it, but if I want run many command like:

ssh2_exec($connection, 'cd /home/ubuntu/');
ssh2_exec($connection, 'mkdir folder');
ssh2_exec($connection, 'cd folder');
ssh2_exec($connection, 'touch test.txt');

It doesn't work and only do first command. how can I run some many command trail together?

Answer Source

Every time you call the function ssh2_exec you are creating a new shell and executing a single command.

If you want to run a series of commands within the same shell, you can try separating them with semicolons or newlines within the same string. For example:

$commands = <<<'EOD'
cd /home/ubuntu
mkdir folder
cd folder
touch test.txt

ssh2_exec($connection, $commands);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download