简体   繁体   English

phpseclib SSH2不执行Shell脚本

[英]Phpseclib SSH2 not executing Shell Scripts

Currently trying to create a PHP web application to run a variety of Shell Scripts to handle website generation and cleanup on delete. 当前正在尝试创建一个PHP Web应用程序以运行各种Shell脚本来处理网站生成和删除操作。

Here's a snippet from my deletePropertyProcess.php : 这是我的deletePropertyProcess.php中的片段:

$ssh = new Net_SSH2($server);

if(!$ssh->login("username", "password")) {
  $result['result'] = 'ERROR';
  $result['message'] = 'Login failed to server ' . $server;
} else {
  $result = $ssh->exec("cd /var/www/sites ; sudo /usr/local/bin/delete_property_folder.sh -c $comp -p $prop");
  echo $result;
}

Where delete_property_folder.sh is a bash script on my remote server and -c $comp -p $prop are my script options/parameters. 其中delete_property_folder.sh是我的远程服务器上的bash脚本,而-c $comp -p $prop是我的脚本选项/参数。

The fun part is, all of this works flawlessly via Putty. 有趣的是,所有这些都可以通过Putty完美地工作。 Running this functionality from PHP via Ajax returns the exit status of the Shell Script and the anticipated output from Ajax, but none of the changes handled by my Shell Script are actually applied. 通过Ajax从PHP运行此功能会返回Shell脚本的退出状态和Ajax的预期输出,但是我的Shell Script处理的所有更改均未实际应用。

Like I said, this runs perfectly from Putty so I doubt the issue is the Shell Script but here it is anyways: 就像我说的那样,这是从Putty完美运行的,所以我怀疑问题是Shell脚本,但无论如何这里都是:

#!/bin/bash

function fix_file_permissions() {
    chown -Rf username:username $1/$2

    echo $?;
}

function delete_specified_folder() {
    rm -rf $1/$2

    echo $?;
}

##Main Script Body
POSITIONAL=()
while [[ $# -gt 0 ]]
do
    key="$1"

    case $key in
        -c|--company)
            COMPANY="$2"
            shift
            shift
        ;;
        -p|--property)
            PROPERTY="$2"
            shift
            shift
        ;;
        *) #unknown option
            POSITIONAL+=("$1") #save it in an array for later
            shift
        ;;
    esac
done
set -- "${POSITIONAL[@]}" #restore positional parameters

PERMISSIONS_FIXED=$(fix_file_permissions $COMPANY $PROPERTY)
if [ $PERMISSIONS_FIXED -eq 0 ]; then
    FOLDER_DELETED=$(delete_specified_folder $COMPANY $PROPERTY)
    echo $FOLDER_DELETED
    exit
fi
echo 1

At this point, any recommendations would be useful. 在这一点上,任何建议都是有用的。 Let me know if you need any more information. 让我知道您是否需要更多信息。

I've managed to get it working by feeding the sudo password via --STDIN to the Shell Script's full path using the following: 我通过使用以下命令通过--STDIN向Shell脚本的完整路径提供了sudo密码,从而使其能够正常工作:

$result = $ssh->exec("cd /var/www/sites; echo 'password' | sudo -S /usr/local/bin/delete_property_folder.sh -c $comp -p $prop");

echo -ing your password with sudo -S allows you to pass your password via the pipe into the sudo shell script execution. 使用sudo -S echo -ing您的密码sudo -S允许您通过管道将密码传递到sudo shell脚本执行中。

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

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