3

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: > " 
0

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

7 Comments

You'd also definitely want to escape the arguments with escapeshellarg - especially the password field which is likely to contain special characters.
@tangrs Thanks. I also forgot to change the string's single quotes to double quotes, so that interpolation will work.
@Barmar no sir, $useraddress = escapeshellarg($useraddress); gives an undefined variable and escapeshellarg('$useraddress') will actually pass to BASH but as a string. paste.ee/p/arOIF bpaste.net/show/141824
Sorry, I misread your variable names. I've updated my answer.
@Barmar But that's the issue my friend. I need a variable there, not static data. That item changes every time. Passing a static data is fine, but dynamic is what borks.
|
2

Got it.

<?php
$upassword = 'test1234'; $uaddress = '[email protected]';
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.