Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

linux - passing a variable from php to bash

I cannot seem to get a variable passed to my bash script from php. $uaddress and $upassword come up empty no matter what I try.

********************* 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 = '[email protected]';
$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);
?>

*********** output and debug ************

[email protected]

+ 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: > " 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to pass the variables as arguments to the shell script, and the shell script has to read its arguments.

So in PHP:

$useraddress = escapeshellarg('[email protected]');
$upassword = escapeshellarg('test1234');
$addr = shell_exec("sudo /home/tpccmedia/cgi-bin/member_add_postfixadmin $useraddress $upassword 2>&1");

and in the shell script:

useraddress=$1
upassword=$2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...