Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a variable from php to bash

Tags:

linux

bash

php

ssh

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: > " 
like image 460
brad Avatar asked May 26 '26 02:05

brad


2 Answers

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
like image 158
Barmar Avatar answered May 27 '26 19:05

Barmar


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
like image 22
brad Avatar answered May 27 '26 18:05

brad