3

我似乎无法从 php 将变量传递给我的 bash 脚本。无论我尝试什么,$uaddress 和 $uppassword 都是空的。

** * ** * ** * ** * ** * ** * ** * bash * ** * ** * ** * ** * ***

#!/bin/bash -x
useraddress=$uaddress
upassword=$upassword
ssh -p 222 -6 2400:8900::f03c:91f:fe69:8af "/var/www/localhost/htdocs/postfixadmin/scripts/postfixadmin-cli mailbox add" $useraddress --password $upassword --password2 $upassword  .ssh

** * ** * ** ** php * ** * ** * ** * ** * *** _

<?php
$upassword = 'test1234'; $uaddress = 'mytestuser@tpccmedia.com';
$addr = shell_exec('sudo /home/tpccmedia/cgi-bin/member_add_postfixadmin 2>&1'); echo $uaddress; echo $upassword;
//$addr = shell_exec('ssh -p 222 -6 2400:8900::f03c:91f:fe69:8af /var/www/localhost/htdocs/postfixadmin/scripts/postfixadmin-cli mailbox add; echo $useraddress; --password; echo $upassword; --password2; echo $upassword; .ssh');
echo "<pre>$addr</pre>";
var_dump($addr);
?>

** * ** * ** * ** 输出和调试 ** * ** * ** * ** *

mytestuser@tpccmedia.comtest1234

+ useraddress=
+ upassword=
+ ssh -p 2222 -6 2400:8900::f03c:91ff:fe69:8aaf '/var/www/localhost/htdocs/postfixadmin/scripts/postfixadmin-cli mailbox add' --password --password2 .ssh

Welcome to Postfixadmin-CLI v0.2
---------------------------------------------------------------
Path: /var/www/localhost/htdocs/postfixadmin
---------------------------------------------------------------

Username:  
> 

string(404) "+ useraddress= + upassword= + ssh -p 2222 -6 2400:8900::f03c:91ff:fe69:8aaf '/var/www/localhost/htdocs/postfixadmin/scripts/postfixadmin-cli mailbox add' --password --password2 .ssh Welcome to Postfixadmin-CLI v0.2 --------------------------------------------------------------- Path: /var/www/localhost/htdocs/postfixadmin --------------------------------------------------------------- Username: > " 
4

2 回答 2

8

您需要将变量作为参数传递给 shell 脚本,并且 shell 脚本必须读取它的参数。

所以在 PHP 中:

$useraddress = escapeshellarg('mytestuser@tpccmedia.com');
$upassword = escapeshellarg('test1234');
$addr = shell_exec("sudo /home/tpccmedia/cgi-bin/member_add_postfixadmin $useraddress $upassword 2>&1");

在shell脚本中:

useraddress=$1
upassword=$2
于 2013-10-18T23:49:22.887 回答
2

知道了。

<?php
$upassword = 'test1234'; $uaddress = 'mytestuser@tpcmedia.com';
$uaddress = escapeshellarg($uaddress);
$upassword = escapeshellarg($upassword);
$addr = shell_exec("sudo /home/tpcmedia/cgi-bin/member_add_postfixadmin $uaddress $upassword 2>&1");
?>


#!/bin/bash -x
uaddress=$1
upassword=$2
ssh -p 2222 -6 2400:8900::f03c:91ff:fe69:8aaf "/var/www/localhost/htdocs/postfixadmin/scripts/postfixadmin-cli mailbox add" $uaddress --password $upassword --password2 $upassword
于 2013-10-19T02:30:08.990 回答