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 = '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);
?>

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

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: > " 
Barmar
  • 741,623
  • 53
  • 500
  • 612
brad
  • 870
  • 2
  • 13
  • 38

2 Answers2

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('mytestuser@tpccmedia.com');
$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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You'd also definitely want to escape the arguments with [escapeshellarg](http://www.php.net/manual/en/function.escapeshellarg.php) - especially the password field which is likely to contain special characters. – tangrs Oct 18 '13 at 23:55
  • @tangrs Thanks. I also forgot to change the string's single quotes to double quotes, so that interpolation will work. – Barmar Oct 19 '13 at 00:00
  • @Barmar no sir, $useraddress = escapeshellarg($useraddress); gives an undefined variable and escapeshellarg('$useraddress') will actually pass to BASH but as a string. http://paste.ee/p/arOIF http://bpaste.net/show/141824/ – brad Oct 19 '13 at 00:56
  • Sorry, I misread your variable names. I've updated my answer. – Barmar Oct 19 '13 at 01:27
  • @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. – brad Oct 19 '13 at 02:16
  • Then replace the static data with a variable containing the data? – tangrs Oct 19 '13 at 02:27
  • Come on, do a little programming of your own. You don't have to copy my code verbatim. Understand what it's doing, and apply it to your needs. – Barmar Oct 19 '13 at 02:30
2

Got it.

<?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
brad
  • 870
  • 2
  • 13
  • 38