Passing variable from PHP to bash script


 
Thread Tools Search this Thread
Top Forums Web Development Passing variable from PHP to bash script
# 1  
Old 12-08-2017
Passing variable from PHP to bash script

I am totally new to PHP and I am trying to create a script that will as a user for a hostname and then use the "hostname" variable to generate a report using REST API.

I am able to create the html script and php script to GET the "hostname" but I am having trouble passing the hostname variable to the bash script which generates the report using REST API.

This is the html script
Code:
<html>
<body>

<form action="test.php" method="get">
Enter Hostname: <input type="text" name="hostname"><br>
<input type="submit">
</form>

</body>
</html>

This is my php script

Code:
Hostname to Query is <?php echo $_GET["hostname"];

$hostname = $argv[1];

#echo "This is $hostname\n";

#$result = shell_exec('/var/www/html/scripts/REST-API/dpa_reports.sh ' . $hostname);
$result = shell_exec('/var/www/html/scripts/REST-API/dpa_reports.sh ' . $hostname);

echo $result;

?><br>

---------- Post updated at 12:38 PM ---------- Previous update was at 12:33 PM ----------

if I do the following from the command line it passes the "hostname" variable to the bash script dpa_report.h script fine. But when I submit the "hostname" from my browser it does not pass the variable...

this command line works

Code:
php test.php hostname-test

# 2  
Old 12-08-2017
php variable $hostname is undefined.
Replace
Code:
$hostname=$argv[1]

with
Code:
$hostname=$_GET["hostname"];

Assuming that you are using Apache as the web server, you can do the following:
Start an additional terminal window, and run "tail -f /var/log/apache2/error.log". You will get al running list of errors from your web development.
# 3  
Old 12-08-2017
Thank jgt That fixed that particular problem. Now I am getting a curl error though. If I run the curl from the command line I have no issue but when it is called from the php script which calls the bash script I get this error

Code:
curl: (7) Failed to connect to 172.16.120.152: Permission denied

The curl command works fine when I do it from the command line.

Any idea why this is happening?

---------- Post updated at 03:36 PM ---------- Previous update was at 03:34 PM ----------

This is the curl command (I have taken out the username and password

curl -u usernameSmilieassword http://seieadpa01:9004/apollo-api/no...%3dseiemccli01

---------- Post updated at 03:38 PM ---------- Previous update was at 03:36 PM ----------

sorry I never put code around

Code:
curl  -u username:password http://seieadpa01:9004/apollo-api/nodes/?query=name%3dhostname

# 4  
Old 12-08-2017
Are the username and password literals, and can you post the balance of the script that includes the curl statement?
# 5  
Old 12-08-2017
This the bash script (dpa_report.sh) that creates the REST -API report. I have taken out the actual username and password for the purposes of this post. If I run this script from the command line and pass it the hostname it works. For example

Code:
./dpa_report hostname

works. The hostname is set to $1 in the script.

But when dpa_report.sh is called from the php script I get the permission errors.


Code:
#!/bin/bash

export HOME=/var/www/html/scripts/REST-API
export TEMP_DIR=/var/www/html/scripts/REST-API/Temp

curl  -u "username:password" http://seieadpa01:9004/apollo-api/nodes/?query=name%3d$1 > $TEMP_DIR/test_file.txt

HOST_ID=`cat $TEMP_DIR/test_file.txt | grep "<id>"`

cat $HOME/host_query_input_file.out | awk -v ID="$HOST_ID" '{if ($1~/\<id>/) $1=ID ;print}' > $TEMP_DIR/host_query_input_file.out

curl -u username:password -X POST -H "Content-Type:application/vnd.emc.apollo-v1+xml" --data "@/var/www/html/scripts/REST-API/host_query_input_file.out" http://seieadpa01:9004/dpa-api/report -o $TEMP_DIR/test_xml.out

LINK=$(grep link $TEMP_DIR/test_xml.out)
LINK="${LINK#*>}"
LINK="${LINK%<*}"

export LINK

sleep 5


curl -u username:password $LINK > /var/www/html/REPORTS/$1.html

# 6  
Old 12-08-2017
Add
Code:
echo $1 >>/tmp/test.log

ahead of the curl statement
# 7  
Old 12-08-2017
I actually had that in there earlier and it does write to that file with the hostname. the hostname I have been using for testing is seiemccli01

This is the file entry after I run (172.16.117.145 is the apache web server)

Code:
http://172.16.117.145/host_read.html

Code:
[root@seinwnprd02 html]# cat /tmp/test.out
seiemccli01
[root@seinwnprd02 html]#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable from bash to perl script

Hi All, I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving error : Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50. Can ... (6 Replies)
Discussion started by: NileshJ
6 Replies

2. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

3. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

4. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

5. Shell Programming and Scripting

Passing variables from bash to php-cli

Hello everyone, I've been looking how to pass variables between bash and php-cli in 1 file. So far i got this: #!/bin/bash echo "This is bash" php << EOF <?php echo "This is php\n"; ?> EOF I would now like to be able to pass a variable declared in the bash to the php. I already... (0 Replies)
Discussion started by: robbee
0 Replies

6. Shell Programming and Scripting

help passing variable from script

Hello, How can I pass a variable into a 2nd file? I'm running a script: ls -la $1 >pem99 cat pem99 | awk '{ print $3}' >us99 cat us99 | read us98 This then tells me the owner of a file. my second file is: echo OWNER GROUP OTHERS echo echo --data-- $us98 ... (2 Replies)
Discussion started by: Grueben
2 Replies

7. Shell Programming and Scripting

Passing Bash variable to javascript

How do I pass a bash variable to a javascript? I've tried #!/bin/bash echo "Content-type: text/html" echo "" echo "<html>" echo "<head>" counter=0 echo '<script> window.parent.document.forms.counter.value = "$counter"; </script>' I have an iframe script which I am trying to pass a... (3 Replies)
Discussion started by: numele
3 Replies

8. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

9. Shell Programming and Scripting

Passing options to a bash script

I'm just not sure where to start looking into this. I want to be able to create switches for my script. Back in the day I'd make my scripts interactive...you know: echo "what report do you want" echo "A)boxes with errors" echo "B)boxes with more than 5 errors" echo "C)Service groups that have... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

10. UNIX for Dummies Questions & Answers

passing variable to my script

Hello everybody: Im trying to run the following script on my sol9 machine: line='' ((lineCount= 0)) export lineCount more /tmp/MSISDNs | wc -l > /tmp/tmp cat /tmp/tmp | read lineCount export lineCount; while (( lineCount > 0 )) do line= tail -$lineCount... (5 Replies)
Discussion started by: aladdin
5 Replies
Login or Register to Ask a Question