0

我写了一些通过 ssh 连接到 vps 的 php 代码

我知道ssh2_exec可以做到,但是如果我想运行许多命令,例如:

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

它不起作用,只执行第一个命令。我怎样才能一起运行一些命令跟踪?

4

2 回答 2

1

你可以在一行上写多个命令,用;或分隔&&所以你可以按照下面的代码

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

或者

ssh2_exec($connection, 'cd /home/ubuntu/ && mkdir folder && cd folder &&touch test.txt');
于 2016-09-16T09:40:27.483 回答
0

每次调用该函数时ssh2_exec,您都在创建一个新的 shell 并执行一个命令。

如果你想在同一个 shell 中运行一系列命令,你可以尝试在同一个字符串中用分号或换行符分隔它们。例如:

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

ssh2_exec($connection, $commands);
于 2016-09-16T09:42:36.393 回答