I am trying to get the results (CPU Temperature remote machine) from a .sh file using curl, Instead I receive the code from the .sh.
my current code curl -s http://192.168.1.5/cgi-bin/temp.sh
instead of returning the temp it returns the .sh file code
I am running Apache2, and if I place the sh in a index.php file, I can grep the output, but that seems like a step too much.
Thanks for any advice.
Tried changing file to .cgi instead. Same results
CodePudding user response:
The reason you're getting the code of the temp.sh
script rather than its output is because curl is simply downloading the file. To actually execute the script and return its output, you need to run it on the server and then retrieve the output.
Here are the steps you could follow:
- Make sure the file is executable by using
chmod x temp.sh
. - Add a shebang line to the beginning of your script, which specifies
the interpreter to use for executing the script. For a bash script,
it should look like this:
#!/bin/bash
.
Modify your Apache2 configuration to allow the execution of shell scripts. You can do this by adding the following lines to your Apache2 configuration file:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin/">
AllowOverride None
Options ExecCGI
Require all granted
</Directory>
Restart Apache2 to apply the changes: sudo service apache2 restart
.
You should now be able to access the script's output
With kind regards.
CodePudding user response:
I found just by changing my curl command to the following resolved the issue
adding a pipe and sh returns the result
curl -s http://192.168.1.5/cgi-bin/temp.sh | sh