我正在尝试将ssh发送到远程服务器,以检查是否存在特定的文件。
我可以在命令行中使用ssh,但是每当我尝试使用我的脚本时,它都不会返回任何东西/我必须键入"exit“并按enter才能返回命令行。
步骤:
root@website.com
我将所有这些命令都放到了ouputs中,因此它们看起来如下:
$output = shell_exec("ssh root@website.com");
$ouput1 = shell_exec("cd ..");
$ouput2 = shell_exec("ls *ATMEXTRACT*");
echo($output2);我不明白为什么这在命令行中直接工作,但在脚本中失败了。任何帮助都非常感谢。
发布于 2019-10-29 22:18:52
下面是你的互动方式:
当前在ssh
exit in ssh中的ssh
ls *ATMEXTRACT*中的cd ..中运行ssh root@website.com,现在是exits
中
下面是您在脚本中所做的工作:
ssh root@website.com,在第二个shell中退出cd ..,在第三个shell中退出ls *ATMEXTRACT*,然后退出H 232F 233您可以尝试打开ssh命令并与其交互,但您也可以省去麻烦,使用ssh的命令行功能来指定要运行的命令:
$output = shell_exec("ssh root@website.com 'cd .. && ls *ATMEXTRACT*'");请注意,PHP网站脚本可能会失败,因为您需要设置身份验证机制。即使当您手动登录到web服务器并尝试时,即使ssh root@website.com没有密码连接,也是如此。
发布于 2019-11-19 06:08:04
我建议您使用PHP的ssh2模块。这将帮助您连接通过适当的SSH端口可到达的任何远程服务器。
您需要检查主机服务器上是否安装了像OpenSSL和ssh2这样的模块。如果没有,请检查此https://ehikioya.com/install-ssh2-php/并安装上述模块。
一旦安装并启用了这些模块。
遵循这段代码。
$server="website.com";
$server_pwd="Password";
//creating connection using server credentials
$connection = ssh2_connect($server, 22);
//authenticating username and password
if(ssh2_auth_password($connection, 'root', $server_pwd)){
echo "connected"
}else{
echo "could not connect to server";
}
ssh2_exec($connection, "ls /FULL_PATH/ATMEXTRACT"); //run your command here https://stackoverflow.com/questions/58615962
复制相似问题