Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP执行shell命令?_Php_Linux_Shell_Ssh_Remote Server - Fatal编程技术网

通过PHP执行shell命令?

通过PHP执行shell命令?,php,linux,shell,ssh,remote-server,Php,Linux,Shell,Ssh,Remote Server,我正在尝试使用ssh连接到远程服务器,以检查是否存在特定的文件 我可以在命令行中使用ssh,但每当我尝试使用脚本时,它不会返回任何内容/我必须键入“exit”并点击enter返回命令行 步骤: sshroot@website.com cd ls ATMEXTRACT 我将所有这些命令放入输出中,使它们看起来像这样: $output = shell_exec("ssh root@website.com"); $ouput1 = shell_exec("cd .."); $ouput2 = shel

我正在尝试使用ssh连接到远程服务器,以检查是否存在特定的文件

我可以在命令行中使用ssh,但每当我尝试使用脚本时,它不会返回任何内容/我必须键入“exit”并点击enter返回命令行

步骤:

  • sshroot@website.com
  • cd
  • ls ATMEXTRACT
  • 我将所有这些命令放入输出中,使它们看起来像这样:

    $output = shell_exec("ssh root@website.com");
    $ouput1 = shell_exec("cd ..");
    $ouput2 = shell_exec("ls *ATMEXTRACT*");
    
    echo($output2);
    

    我不明白为什么它直接在命令行中工作,但在脚本中却失败了。非常感谢您的任何帮助

    以下是您以交互方式执行的操作:

    • 运行
      sshroot@website.com
      在当前shell中
      • ssh
      • ssh
      • ssh
        中输入
        exit
        ,现在退出
    • 发现自己回到了原来的壳中
    以下是您在脚本中执行的操作:

    • 运行
      sshroot@website.com
      在一个新的shell中并退出它
    • 在第二个shell中运行
      cd..
      ,然后退出它
    • 在第三个shell中运行ls*ATMEXTRACT*,然后退出它
    您可以尝试打开ssh命令并与之交互,但也可以省去麻烦,使用
    ssh
    的命令行功能指定要运行的命令:

    $output = shell_exec("ssh root@website.com 'cd .. && ls *ATMEXTRACT*'");
    

    请注意,PHP网站脚本很可能会失败,因为您需要设置身份验证机制。即使
    ssh也是如此root@website.com
    手动登录web服务器并尝试时,无需密码即可连接

    以下是您以交互方式执行的操作:

    • 运行
      sshroot@website.com
      在当前shell中
      • ssh
      • ssh
      • ssh
        中输入
        exit
        ,现在退出
    • 发现自己回到了原来的壳中
    以下是您在脚本中执行的操作:

    • 运行
      sshroot@website.com
      在一个新的shell中并退出它
    • 在第二个shell中运行
      cd..
      ,然后退出它
    • 在第三个shell中运行ls*ATMEXTRACT*,然后退出它
    您可以尝试打开ssh命令并与之交互,但也可以省去麻烦,使用
    ssh
    的命令行功能指定要运行的命令:

    $output = shell_exec("ssh root@website.com 'cd .. && ls *ATMEXTRACT*'");
    

    请注意,PHP网站脚本很可能会失败,因为您需要设置身份验证机制。即使
    ssh也是如此root@website.com
    手动登录web服务器并尝试时,无需密码即可连接

    我建议您使用PHP的ssh2模块。这将帮助您连接可通过适当的SSH端口访问的任何远程服务器

    您需要检查主机服务器上是否安装了像OpenSSL和ssh2这样的模块。 如果没有,请检查并安装上述模块

    安装并启用这些模块后

    请遵循此代码

    $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 
    

    我建议您使用PHP的ssh2模块。这将帮助您连接可通过适当的SSH端口访问的任何远程服务器

    您需要检查主机服务器上是否安装了像OpenSSL和ssh2这样的模块。 如果没有,请检查并安装上述模块

    安装并启用这些模块后

    请遵循此代码

    $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 
    

    理想情况下,这个问题可以用一个关于shell_exec如何工作的很长的解释来回答,并且可以解释您在这里的误解。我不知道从哪里开始,但同时,对于你正在做的事情,考虑PHP也考虑PHP的FTP功能,这个问题将被理想地回答关于如何解释SeelyExcel真的工作,并且将解释你的误解。我不知道从哪里开始,但同时,对于你正在做的事情,考虑PHP也考虑PHP的FTP功能,这似乎是有效的!非常感谢你!我真的很感谢你的帮助和透彻的解释:)这似乎奏效了!非常感谢你!我真的很感谢你的帮助和透彻的解释:)