通过PHP执行shell命令吗?

问题描述 投票:1回答:1

我正在尝试SSH到远程服务器,以检查是否存在特定文件。

我能够在命令行中使用ssh,但是每当我尝试使用脚本时,它都不会返回任何内容/我必须输入“ exit”并按Enter键以返回命令行。

步骤:1. ssh [email protected].光盘..3. ls ATMEXTRACT

我将所有这些命令放入输出中,因此它们看起来像这样:

$output = shell_exec("ssh [email protected]");
$ouput1 = shell_exec("cd ..");
$ouput2 = shell_exec("ls *ATMEXTRACT*");

echo($output2);

我对为什么它直接在命令行中起作用但在脚本中失败感到困惑。任何帮助都非常感激

php linux shell ssh remote-server
1个回答
0
投票

这是您的交互操作:

  • 在当前shell中运行ssh [email protected]
    • cd ..中输入ssh
    • ls *ATMEXTRACT*中输入ssh
    • exit中的输入ssh,现在退出
  • 回到原来的外壳中

这是您在脚本中所做的:

  • 在新的shell中运行ssh [email protected]并退出它
  • 在第二个shell中运行cd ..并退出它
  • 在第三个shell中运行ls *ATMEXTRACT*并退出它

[您可以尝试打开ssh命令并与之交互,但也可以省去麻烦,并使用ssh的命令行功能指定要运行的命令:

$output = shell_exec("ssh [email protected] 'cd .. && ls *ATMEXTRACT*'");
© www.soinside.com 2019 - 2024. All rights reserved.