Unix/Linux Go Back    


Web Programming Discuss Web Programming and Web Server Administration, including LAMP, Apache, MySQL, HTML, SEO, and other Web APIs and topics.

Passing variable from PHP to bash script

Web Programming


Tags
apache config, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
kieranfoley's Unix or Linux Image
kieranfoley kieranfoley is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 December 2017, 8:50 AM EST
Posts: 47
Thanks: 11
Thanked 1 Time in 1 Post
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

Sponsored Links
    #2  
Old Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 June 2018, 10:34 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 2,071
Thanks: 2
Thanked 290 Times in 267 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
kieranfoley's Unix or Linux Image
kieranfoley kieranfoley is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 December 2017, 8:50 AM EST
Posts: 47
Thanks: 11
Thanked 1 Time in 1 Post
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 usernameLinuxassword 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 Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 June 2018, 10:34 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 2,071
Thanks: 2
Thanked 290 Times in 267 Posts
Are the username and password literals, and can you post the balance of the script that includes the curl statement?
Sponsored Links
    #5  
Old Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
kieranfoley's Unix or Linux Image
kieranfoley kieranfoley is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 December 2017, 8:50 AM EST
Posts: 47
Thanks: 11
Thanked 1 Time in 1 Post
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

Sponsored Links
    #6  
Old Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 June 2018, 10:34 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 2,071
Thanks: 2
Thanked 290 Times in 267 Posts
Add


Code:
echo $1 >>/tmp/test.log

ahead of the curl statement
Sponsored Links
    #7  
Old Unix and Linux 12-08-2017   -   Original Discussion by kieranfoley
kieranfoley's Unix or Linux Image
kieranfoley kieranfoley is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 December 2017, 8:50 AM EST
Posts: 47
Thanks: 11
Thanked 1 Time in 1 Post
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]#

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing variable from bash to perl script NileshJ Shell Programming and Scripting 6 02-03-2016 10:23 AM
Passing string as variable(s) in bash Starting_Leaf Shell Programming and Scripting 2 04-06-2013 02:35 PM
Trouble with passing Variable from bash to awk gsub command Nostyx Shell Programming and Scripting 9 12-09-2011 07:32 AM
Passing Bash variable to javascript numele Shell Programming and Scripting 3 04-12-2010 06:38 PM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM



All times are GMT -4. The time now is 10:37 PM.