简体   繁体   English

使用ssh2_exec远程执行sh脚本时,PHP无法正常工作

[英]PHP is not working while executing sh script remotely using ssh2_exec

I have a shell script makedir.sh as given below. 我有一个shell脚本makedir.sh ,如下所示。

sudo -H sh -c '
    mkdir /usr/local/testdir;
    if [ $? -eq 0 ];then
        echo "Successfull";
    else
        echo "Unsuccessfull";
    fi
'

I have given privileges to a user testuser to execute shell script with sudo, but without asking password.For this I add the below line in /etc/sudoers file, 我已授予用户testuser特权以sudo执行shell脚本,但无需询问密码。为此,我在/etc/sudoers文件中添加以下行,

testuser ALL=(ALL) NOPASSWD: ALL

And it works fine that, I could run commands with sudo, but without asking password. 它可以很好地工作,我可以使用sudo运行命令,而无需询问密码。 The above shell script also working fine without asking password, creating directory testdir inside /usr/local . 上面的shell脚本在不询问密码的情况下也可以正常工作,在/usr/local创建目录testdir My problem starts here. 我的问题从这里开始。 I want to run this shell script from a php file. 我想从php文件运行此shell脚本。 In the current scenario the shell script is residing in a remote machine with ip address 10.3.2.0. 在当前情况下,shell脚本位于IP地址为10.3.2.0的远程计算机中。 The following is my php code but, while running this php code, it is not creating any directory(testdir) inside /usr/local/ . 以下是我的php代码,但是在运行此php代码时,它没有在/usr/local/创建任何目录(testdir)。 Please advise as I am very beginner in PHP . 请指教,因为我是PHP初学者。

PHP Code PHP代码

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("10.3.2.0", "22")))
{
    echo "fail: unable to establish connection";
}
else
{
    if(!ssh2_auth_password($con, "testuser", "abcdef"))
    {
        echo "fail: unable to authenticate ";
    }
    else
    {
        $stream = ssh2_exec($con, "./makedir.sh");
            stream_set_blocking($stream, true);
        $item = "";
        while ($input = fread($stream,4096)) {
               $item .= $input;
        }
        echo $item;
    }
}

?>

SHELL SCRIPT - makedir.sh (residing at 10.3.2.0 - the same above shell script) 外壳脚本makedir.sh (位于10.3.2.0-与上面的Shell脚本相同)

#!/bin/sh
sudo -H sh -c '
    mkdir /usr/local/testdir;
    if [ $? -eq 0 ];then
        echo "Successfull";
    else
        echo "Unsuccessfull";
    fi
'

Thanks. 谢谢。

From what i know, sudo commands do not work from ssh2 module of php. 据我所知,sudo命令不适用于php的ssh2模块。 you will need to connect as the user who has permissions to the directory. 您将需要以具有目录权限的用户身份连接。

Well... Question was a year ago xD but still i may halp to use sudo with ssh2_exec you need to a little trick. 好吧...问题是一年前的xD,但我仍然可能会中止使用sudo和ssh2_exec,您需要一点技巧。 It is unsafe and not recommended but if you have no other choice use 这是不安全的,不建议使用,但是如果您没有其他选择,请使用

ssh2_exec($connection, "echo sudopw | yourscript.sh" . PHP_EOL);

and your script has to call sudo with the argument -S 并且您的脚本必须使用-S参数调用sudo

"sudo -S mkdir folder"

should work 应该管用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM