First of all I am showing the PHP code ....
<?php
echo ("hello");
echo exec("sendip -v -p ipv6 -6s 2001::100 -p tcp -ts 21 -td 21 2001::200 2>
&1");
echo ("hi");
?>
When I entered the command through linux command line it is working fine.The command is sending a tcp ipv6 packet on 2001::200 machine from 2001::100.
[root#udit-pc]# sendip -v -p ipv6 -6s 2001::100 -p tcp -ts 21
-td 21 2001::200 > /dev/null &
/* (-v for verbose) */
Output of above command ...
Added 34 options
Initializing module ipv6
Initializing module tcp
Finalizing module tcp
Finalizing module ipv6
Final packet data:
60 00 00 00 `...
/*
here other packet
contents gets printed
*/
7D 62 00 00 }b..
61 62 63 64 abcd
Sent 64 bytes to 2001::200
Freeing module ipv6
Freeing module tcp
When I execute the php script through command line...
[root#udit-pc]# php test.php
Freeing module tcp
hellohi gets printed and packet arrived at 2001::200.
But problem arise when I try to run php script through browser...
http:://localhost/test.php
hellohi gets printed but packet does not arrive at other machine.
sh: sendip: command not found
Also in both case packet contents are not printed at terminal although using verbose option but when directly using command verbose option works fine.
I tried with many things although I do not think they would help like......
I added /usr/local/lib and usr/local/bin to PATH variable but no benefit.
chmod +s /usr/local/bin/sendip .Sticky bit set but again no benefit.
paste the /usr/local/bin/sendip itself in /var/www/html folder although I have changed the PATH variable but as i said i m just using hit n trial getting no clue.....
There are some output snapshots which may further help ....
[root#cc html]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:
/usr/X11R6/bin:/root/bin:/usr/local/lib
[root#cc html]# locate sendip
.....
/usr/local/bin/sendip
/usr/local/lib/sendip
.....
[root#cc bin]# chmod +s sendip
[root#cc bin]# ls -l sendip
-rwsrwsrwx 1 apache apache 41071 Sep 26 19:41 sendip
[root#cc bin]# cd /usr/local/lib/
[root#cc lib]# ls -ld sendip
drwxrwxrwx 2 root root 4096 Sep 28 22:48 sendip
[root#cc lib]# chmod +s sendip
[root#cc lib]# ls -ld sendip
drwsrwsrwx 2 root root 4096 Sep 28 22:48 sendip
When file contents are changed .......
<?php
echo exec("/usr/bin/sendip ........ 2 > &1");
?>
Then oputput is :
[root#cc html]# php test.php
Freeing module tcp[root#cc html]#
On browser....
No error gets printed but packet still not arrived.
I am stuck in between.Please suggest me what else should I rather try ??????/
is sendip() in the path of the shell being invoked by PHP? You're not checking for error conditions, so possibly you're not actually executing sendip, and just getting a "no such program or file" type errors.
Instead of redirecting the exec()'d command's output to null, redirect it all to the browser so you can see what happens:
echo exec("sendiip yada yada yada 2>&1");
Try using the full path:
exec("/usr/lib/sendip -v -p ipv6 -6s 2001::100 -p tcp -ts 21 -td 21 2001::200 > /dev/null &");
The server is most likely not running with the same permissions as the user, you are testing with.
The server is most likely discarding any PATH variable. Make sure that you specify the complete path to sendip in the exec call.
The Problem is solved although I can not say its fully solved but as per my need its working.
What i did is I re-installed the sendip ,then I set its sticky bit and then after that I set the Path variable to as above mentioned in question.
Actually the tool is by default installing the libraries in /usr/local/lib/sendip folder and sendip in /usr/local/bin folder.
Although after setting PATH variable still I need to use full path in the PHP Script
/usr/local/bin/sendip -v .....
(one of my friend suggested me this..)
What I think is PHP Path is something different from Shell PATH.I need to paste sendip to /usr/bin and then I need to run updatedb before setting its sticky bit if I don't want to mention full path in PHP Script .Now this command will work fine in PHP Script.
sendip -v .........
Although May be I am wrong but this all works fine for me.
Related
Is there any way to watch php activities online? I am looking for something like apache logs that I can I run tail -f /var/log/httpd/access.log in the terminal and then open the URL in a browser and see what apache prints.
So, I am seeking for similar thing for php web pages. The php is 7.2
UPDATE1:
Trying to watch system log with tail -f /var/log/messages, I get many messages similar to this in the terminal when the page is reloaded from browser.
python: SELinux is preventing /usr/sbin/httpd from write access on the directory /var/www/html/ow_userfiles/plugins/admin.#012#012***** Plugin httpd_write_content (92.2 confidence) suggests ***************#012#012If you want to allow httpd to have write access on the admin directory#012Then you need to change the label on '/var/www/html/ow_userfiles/plugins/admin'#012Do#012# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/ow_userfiles/plugins/admin'#012# restorecon -v '/var/www/html/ow_userfiles/plugins/admin'#012#012***** Plugin catchall_boolean (7.83 confidence) suggests ******************#012#012If you want to allow httpd to unified#012Then you must tell SELinux about this by enabling the 'httpd_unified' boolean.#012#012Do#012setsebool -P httpd_unified 1#012#012***** Plugin catchall (1.41 confidence) suggests **************************#012#012If you believe that httpd should be allowed write access on the admin directory by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'httpd' --raw | audit2allow -M my-httpd#012# semodule -i my-httpd.pp#012
It is an ugly message! I think I have to run semanage fcontext -a -t with some paths. Not sure which paths exactly!
UPDATE2:
A more clear log is available with the following command
[root#localhost html]# sealert -l e254cabb-7005-4a3c-8f91-8620c924c5e0
SELinux is preventing /usr/sbin/httpd from write access on the file /var/www/html/ow_includes/config.php.
***** Plugin httpd_write_content (92.2 confidence) suggests ***************
If you want to allow httpd to have write access on the config.php file
Then you need to change the label on '/var/www/html/ow_includes/config.php'
Do
# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/ow_includes/config.php'
# restorecon -v '/var/www/html/ow_includes/config.php'
***** Plugin catchall_boolean (7.83 confidence) suggests ******************
If you want to allow httpd to unified
Then you must tell SELinux about this by enabling the 'httpd_unified' boolean.
Do
setsebool -P httpd_unified 1
***** Plugin catchall (1.41 confidence) suggests **************************
If you believe that httpd should be allowed write access on the config.php file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'httpd' --raw | audit2allow -M my-httpd
# semodule -i my-httpd.pp
Additional Information:
Source Context system_u:system_r:httpd_t:s0
Target Context unconfined_u:object_r:httpd_sys_content_t:s0
Target Objects /var/www/html/ow_includes/config.php [ file ]
Source httpd
Source Path /usr/sbin/httpd
Port <Unknown>
Host localhost.localdomain
Source RPM Packages
Target RPM Packages
Policy RPM selinux-policy-3.13.1-192.el7_5.6.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name localhost.localdomain
Platform Linux localhost.localdomain
3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14
21:49:04 UTC 2018 x86_64 x86_64
Alert Count 108
First Seen 2018-09-02 16:51:25 +0430
Last Seen 2018-09-02 23:00:19 +0430
Local ID e254cabb-7005-4a3c-8f91-8620c924c5e0
Raw Audit Messages
type=AVC msg=audit(1535913019.143:9913): avc: denied { write } for pid=5121 comm="httpd" name="config.php" dev="dm-0" ino=18219610 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:httpd_sys_content_t:s0 tclass=file
Hash: httpd,httpd_t,httpd_sys_content_t,file,write
This is an SELinux issue, as is apparent by the logs messages...
SELinux is preventing /usr/sbin/httpd from write access on the directory /var/www/html/ow_userfiles/plugins/admin.#012#012*****
...
If you want to allow httpd to have write access on the admin directory
Then you need to change the label on '/var/www/html/ow_userfiles/plugins/admin'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/ow_userfiles/plugins/admin'
restorecon -v '/var/www/html/ow_userfiles/plugins/admin'
Start with the first error, do exactly as it states.. and see if that resolves e others.
If other errors still exist after running these commands, follow the res of the instructions in the very clear error message you were presented.
Here is a php « tail » from my notes to play around.
It is similar to tail -n 5 access.log
This will answer the last 5 lines of access.log.
To make it acting like tail -f, just use a loop.
$check = "access.log";
$end = "5";
$fp= fopen($check, "r");
$count=0;
while($line = fgetss($fp))
$count++;
// echo "Total lines ".$count.PHP_EOL;
fclose($fp);
$start = $count - $end;
$file = new SplFileObject($check);
$file->seek($start);
for($i = 0; !$file->eof() && $i < $end; $i++) {
echo $file->current();
$file->next();
}
It is also very fast, the whole file isn't taken in memory, just like tail.
SplFileObject
I'm having trouble to run a bash file using PHP.
PHP File :
chdir('/var/www/PATH/inc/bash/');
exec('./status.sh argument, $output);
Bash File :
#!/bin/bash
echo 'test' >> /var/www/PATH/inc/bashOutput/test.txt
PHP File (ls -al handler.func.php) :
-rw-r--r-- 1 root root 461 Jul 5 11:35 handler.func.php
Bash File (ls -al status.sh) :
-rwxr-xr-x 1 root root 255 Jul 5 11:39 status.sh
Script is working using through root with SSH.
I'm not a pro on Linux.
But I think it's a problem come with the file owner.
But I have already done some damages in the past with "chown" so If it is indead the problem I would prefer some guidance from more experienced people.
Thanks for you help,
Konorr.
Script is working using through root with SSH. There is the problem. When a PHP script run via a web request it usually runs as the user www-data. In anycase <?php exec('./status.sh argument, $output);?> in a security hole. Most server admins would have this disabled.
Your other option is to put sudo in your exec function exec('sudo bash /var/www/PATH/inc/bash/status.sh'). Along with running the script with an absolute path bash /var/www/PATH/inc/bash/status.sh
Why can't you run a cron on your script?
Thanks for you answer but unfortunatly it didn't worked for me.
I search a litle more about file owners.
I did few changes
I made in these change :
chown -R www-data:www-data /var/www/PATH
usermod -a -G www-data user
chgrp -R www-data /var/www/PATH
chmod 2750 /var/www/PATH
chmod 2750 /var/www/PATH/inc/bash
It wasn't yet working till I removed the sudo from the EXEC function.
So I don't know from which point my problem was already fixed.
I followed this article : www-data permissions?
Thanks for you time and in the hope it can help someone else.
So this is my code for the raspberry pi to get a still shot from the raspicam and save it on a directory,
<?php
exec('raspistill -n -hf -o /var/www/img/image.jpg --timeout 1');
?>
I have given the ownership and the permission to read/write in that forlder using -R. so my ls -al in /var/www is this
drwxr-xr-x 3 www-data www-data 4096 Jun 19 08:05 .
drwxr-xr-x 12 root root 4096 Jun 19 05:54 ..
-rwxrwxrwx 1 www-data www-data 74 Jun 19 08:30 getImg
drwxrwxrwx 2 www-data www-data 4096 Jun 19 09:21 img
-rw-r--r-- 1 root root 70 Jun 19 10:07 index.php
getImg is the script i tried to run the script as a file like shell_exec('/bin/bash ./getImg'); that also doesn't work.
i have added /bash/bin and tried to run the script without using the script file too but that doesn't get the results.
How ever when i try to run the php file in the terminal, it creates the image as it normally should. So i figure this must be a permission issue, but what else should i do with the permissions? I have given all the rights to the directory.
EDIT
So I have found a workaround to this. since I don't know what the cause for the problem, i'd not mark this as an answer, but please vote it to appear at the top.
I now execute the script using the cgi scripts. I have created a shell script in the /usr/lib/cgi-bin/
#!/bin/bash
echo "Content-type:text/html\n"
sudo raspistill -vf -n -o /var/www/img/image.jpg --timeout 1200 --metering matrix
echo "Status: 204"
I saved this as capture and made this executable, did nothing with the permissions though.
sudo chmod +x capture
now when i open the link http://192.168.1.85/cgi-bin/capture the browser will still get me a 500 internal server error message. how ever, the image would still be created.
I would now need to get the 500 internal server error to be fixed.
[I'd add this as a comment but don't have enough points for it]
if you use the optional parameters $output and $return_var to capture the output and return value what do you get?
string exec ( string $command [, array &$output [, int &$return_var ]] )
does your command rely on environment variables that may be available when you run it as your user but not as www-data? (you can use the env command to check that)
does it still work if you run it via terminal after switching user to www-data?
Encountering a problem when running phpagi:
-- Executing [123#DLPN_C:1] AGI("SIP/1000-00000001", "hello_world.php") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/hello_world.php
hello_world.php: Failed to execute '/var/lib/asterisk/agi-bin/hello_world.php': Exec format error
-- Auto fallthrough, channel 'SIP/1000-00000001' status is 'UNKNOWN' Scheduling destruction of SIP dialog '343930130' in 32000 ms (Method: INVITE)
From command line:
root#asterisk-test:/var/lib/asterisk/agi-bin# php5 -q hello_world.php
#!/usr/bin/php5 -q
Additional info:
-rwxr-xr-x 1 root root 757 Mar 29 19:32 hello_world.php
drwxrwxr-x 4 root root 4096 Mar 29 19:44 phpagi
-rwxr-xr-x 1 root root 25079 Sep 30 2010 phpagi-asmanager.php
-rwxr-xr-x 1 root root 2322 Sep 30 2010 phpagi-fastagi.php
-rwxr-xr-x 1 root root 67615 Sep 30 2010 phpagi.php
Source of hello world: http://www.eder.us/projects/phpagi/phpagi/api-docs/__examplesource/exsource_home_html_projects_phpagi_phpagi_examples_dtmf.php_acb7257145e4a5249182c8373cd8e848.html
The Exec Format Error is from /bin/bash, asterisk executes hello_world.php as a bash script.
shebang
If you add a correct shebang, the script get executed by the given PHP intepreter.
The first Line tells the System which program should run the script.
#!/usr/bin/env php
To test your shebang, execute the script itself, not by PHP:
root#asterisk-test:/var/lib/asterisk/agi-bin# ./hello_world.php
Make sure it is executable with:
root#asterisk-test:/var/lib/asterisk/agi-bin# chmod +x hello_world.php
alternative wrapper
Create a bash script that executes the PHP script.
example hello_world.sh:
/usr/bin/php hello_world.php
and call it in the Dialplan AGI("hello_world.sh").
Make sure the shellscript is executable chmod +x hello_world.sh.
I added following line on top script to get it working for me
#!/usr/bin/php -q
You issue is not asterisk issue,but general linux one.
Please try from your command line following:
su asterisk -c "/var/lib/asterisk/agi-bin/hello_world.php"
Most likly reasons: php path is incorrect or selinux enabled and not configured.
Could you check your extensions.conf or extensions_custom.conf, if the extension and priority are not continuous also this error will occur.
please check the below example:
[context]
exten => 1,1,Answer()
exten => 1,2,AGI(your-agi-script)
exten => 1,3,Hangup()
I need to manage the recording/capture of a website mindwhile it is running a slide-show to get videos form these slides.
My approach is:
<?php
define('FFMPEG_LIBRARY', '/usr/bin/ffmpeg ');
$ffmpegcmd = "ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg";
shell_exec($ffmpegcmd);
?>
But i get this error from php error log:
[x11grab # 0x81e8aa0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 800 height: 600
No protocol specified
No protocol specified
[x11grab # 0x81e8aa0] Could not open X display.
:0.0: Input/output error
Similar command from console run good.
Please, any help to get display and be able to control ffmpeg from browser php script?
Thanks in advance.
thanks for your time.
I got rid the X display error, but not I still haven't got the capture.
Using xvfb I get an unknown file at /tmp written by www-data user:
-rw-r--r-- 1 www-data www-data 11252 Sep 12 09:49 server-B20D7FC79C7F597315E3E501AEF10E0D866E8E92.xkm
Running startx I got also an unknown file at /tmp
-rw------- 1 www-data www-data 59 Sep 12 09:53 serverauth.oLcFlG7tXC
any of both grow in size so it is not capturing anything. The content is some binary thing.
What are those files about?
What I am trying is to write a script in which I can control the time ffmpeg is capturing the desktop to create a video from a jquery slide displayed on a website.
My try from console is closer, but if I can do it by browser I will be able to know when to stop sending an AJAX request once the slide is finished.
This is my try from console:
#!/bin/bash
# start the slide website: I will need to change it to control by querystring the language and course level
firefox http://www.languagecourse.net/vocabulary-trainer.php &
# start recording: I will need to adjust the frame to capture
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output2.mpg &
# since I can't control the time a course takes I pause an arbitrary time
sleep 5
# look for the capture PID and close it
for i in $(ps aux | grep ffmpeg | sed "s/ */#/g" | cut -f2 -d#)
do
echo "proceso $i killed"
kill -9 $i
done
I wonder once the website is opened I can continue doing the control from AJAX, but not know if I will be able to get the ffmpeg PID to stop the command.
I will appreciate any kind of comments.
Regards,
·_-
You can use Xvfb to emulate a x-environment
<?php
$ffmpegcmd = "xvfb-run -a ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg";
shell_exec($ffmpegcmd);
or something like this
<?php
$ffmpegcmd = "startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 && ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg");
shell_exec($ffmpegcmd);
That should be good to get rid of the "Could not open X display." error, and will probably solve your problem.