bash: Make this script allow execution of php parser - php

Currently I start an Armagetron Server with the following script
#!/bin/sh
tron="/home/duke/aa/bin/armagetronad-dedicated"
var="/home/duke/aa/servers/demo/var/"
log="${var}console_log.txt"
userconfigdir="/home/duke/aa/servers/demo/var/customize/config/"
parser="/home/duke/aa/scripts/parser.php"
ladderlog="${var}ladderlog.txt"
cmds="${var}cmd.txt"
tail -n0 -f -s 0.01 $ladderlog | $parser | $tron --userconfigdir $userconfigdir --vardir $var | tee -a $log
This runs the server and applys the php script in $parser
However, I want to start my server like this:
#!/bin/bash
screen -S $1 -X quit # avoiding to run the same server twice
screen -dmS $1 /home/duke/aa/scripts/srv $1
srv:
screen -S $1 -X logtstamp on
screen -S $1 -X logfile /home/duke/aa/servers/$1/logs
screen -S $1 -X log on
while true; do # start a loop to allow server restart if it crashes
/home/duke/aa/bin/armagetronad-dedicated --vardir /home/duke/aa/servers/$1/var --userdatadir /home/duke/aa/servers/$1/var/customize #run the server
echo "###Server has crashed. Ctrl-C to cancel reboot...###"
sleep 5
done # end the loop
the only problem here is I don't know how to specify a php parser to run on the server (im a noob). How would i modify the second scripts to allow this?

put all the code in your first block inside the while loop in the srv script, i.e.
srv:
#!/bin/bash
tron="/home/duke/aa/bin/armagetronad-dedicated"
var="/home/duke/aa/servers/demo/var/"
log="${var}console_log.txt"
userconfigdir="/home/duke/aa/servers/demo/var/customize/config/"
parser="/home/duke/aa/scripts/parser.php"
ladderlog="${var}ladderlog.txt"
cmds="${var}cmd.txt"
screen -S $1 -X logtstamp on
screen -S $1 -X logfile /home/duke/aa/servers/$1/logs
screen -S $1 -X log on
while true; do # start a loop to allow server restart if it crashes
tail -n0 -f -s 0.01 $ladderlog | $parser | $tron --userconfigdir $userconfigdir --vardir $var | tee -a $log
# /home/duke/aa/bin/armagetronad-dedicated --vardir /home/duke/aa/servers/$1/var --userdatadir /home/duke/aa/servers/$1/var/customize #run the server
echo "###Server has crashed. Ctrl-C to cancel reboot...###"
sleep 5
done # end the loop
(Does this look right?)
I hope this helps.

Related

shell_exec won't stop even though add new shell_exec to stop the other one

I've got a PHP script that needs to run the .sh file using shell_exec
echo shell_exec('sh /var/www/html/daloradius/start.sh > /dev/null 2>/dev/null &');
I just dump it into background. This is my start.sh
sudo tcpdump port 1812 -w testing.pcap
we know that tcpdump always listen all the time, I tried to resolve this (stop the tcpdump process) with the button that triggering another shell_exec which is stop.sh
pid=$(ps aux | grep "sudo tcpdump" | head -1 | cut -d '.' -f 1 | cut -d ' ' -f 7)
sudo kill $pid
Stop.sh is doing fine when I tested it in cli, but when I click the button that triggering start.sh and I tried to stop it with the button that triggering stop.sh it doesn't work. The tcpdump won't stop, but when I try to stop it in cli using stop.sh it's work well. Can anybody gimme solution to force stop the tcpdump things? Thank you
You are trying to use bash when you should be orchestrating the process from php.
Here, we get the PID of the command and kill it from PHP. Replace the sleep statement with whatever code you have.
<?php
# Script must be run with sudo to start tcpdump
# Be security conscious when running ANY code here
$pcap_file = 'testing.pcap';
$filter = 'port 1812'
$command = "tcpdump $filter -w $pcap_file" . ' > /dev/null 2>&1 & echo $!;';
$pid = (int)shell_exec($command);
echo "[INFO] $pid tcpdump: Writing to $pcap_file\n";
# Some important code. Using sleep as a stand-in.
shell_exec("sleep 5");
echo "[INFO] $pid tcpdump: Ending capture\n";
shell_exec("kill -9 $pid");
Please note that tcpdump has the -c option to stop ofter n packets received and you can rotate files with -G. You may want to read up on tcpdump's manpage to get the most out of it.

Bash does not execute the certbot command

Bash does not execute the certbot command, how to find out why?
When I use ls command to test normal bash functions
php:
$cmd = "/bin/bash /usr/local/openresty/nginx/html/pHpServer-PG/api/cert.bash test.com";
echo $cmd;
$ww = shell_exec($cmd);
var_dump($ww);
bash:
#!/bin/bash
certbot certonly --rsa-key-size 4096 --webroot --agree-tos --no-eff-email --email email#gmail.com -w /usr/local/openresty/nginx/html -d $1 -d admin.$1 -d www.$1
I use php-fpm and nginx with user root
Some way to extract bash error?
Do you specify the variable in -d $1 -d admin.$1 -d www.$1 port of the code, so $1?
That suggests you're passing a domain name to the code.
You can also run the bash script with bash -x, so e.g. if you named your script certificate.sh, run
bash -x certificate.sh domain.com
where domain.com is your domain name.

Start a bash script later in PHP

I'm trying to start a bash script later in PHP so I allowed it in visudo.
www-data ALL = (root) NOPASSWD: /sbin/iptables
www-data ALL = (root) NOPASSWD: /usr/bin/at
The script removeuserIP is just doing sudo iptables ... and is working:
#!/bin/bash
sudo iptables -t nat -D PREROUTING -s $1 -j ACCEPT;
sudo iptables -D FORWARD -s $1 -j ACCEPT;
and in the PHP code, I put this line:
$msg=exec("echo /var/www/scripts/removeuserIP $ipaddress | at now + 1 minutes");
but the issue is it's starting the script right now. I checked in /log/var/auth.log and indeed, it's starting the command right now.
I tried it in a terminal directly and there was no issue, it is starting later (with an argument of course):
echo /var/www/scripts/removeuserIP $ipaddress | at now + 1 minutes
I also tried to do it like this in a terminal but this one is not working too because it doesn't understand there is an argument for the file:
sudo at now +1 minutes -f /var/www/scripts/removeuserIP 172.24.1.115
I really don't understand why it is starting right now even if it should start 1 minute later and not now.
Would it be acceptable to put a time delay in removeuserIP script?
#!/bin/bash
sleep 1m
sudo iptables -t nat -D PREROUTING -s $1 -j ACCEPT;
sudo iptables -D FORWARD -s $1 -j ACCEPT;
Solution: Finally, after checking /var/log/apache2/error.log, I saw that it doesn't have the permission to use at.
In fact you have to go /etc/at.deny and remove the line www-date with at. There is probably a security reason for why it's forbidden by default and a better way to do this, but at least it's working.

Running tasks in background in laravel 4.2

I have a condition where I have to start iperf server as daemon on specified port and if iperf server is running, I have to send response to client. I tried
shell_exec('iperf -s -p {port} -D');
but it doesn't return control / infinite loop starts.
The server will start but the code below the shell_exec will never be executed.
Anyone has a solution or suggestion how I should approach this to get the result?
Your command iperf -s -p {port} -D happens to have stderr output, try doing this:
$outfile = "/tmp/erroutperf.out";
$port = 8080;
shell_exec("iperf -s -p $port -D > $outfile 2>&1");
basically the additional command > /tmp/erroutperf.out 2>&1, tells bash to save
both stderr output and stdout of a program (iperf) to a file /tmp/erroutperf.out
getting the output of the command is:
file_get_contents($outfile);

Tail -f | Grep <regex> | php script.php <grep result>

Ok, so I have a ssh connection open to a remote server. I'm running a tail on the logs and if an ID shows up in the logs I need to do an insert into the database.
So I have my ssh tail working and I have it piping into my grep function which is giving me the IDs I need. The next step is that as those IDs are found it needs to immediately kick off a php script.
What I thought it would look like is:
ssh -t <user>#<host> "tail -f /APP/logs/foo.log" | grep -oh "'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'" | php myscript.php <grep result>
And yes my regex is horrible, wanted to use [0-9]{8}, but what I have gets the job done.
Any suggestions? Tried looking at -exec, awk, or any other tool. I could write the result to its own file and then read the new file, but that doesn't catch the streaming ids.
-=-=-=-=-EDIT-=-=-=-=-=-
So here is what I'm using:
ssh -t <user>#<host> "tail -f /APP/logs/foo.log" |grep "^javax.ejb.ObjectNotFoundException" |awk '/[0-9]/ { system("php myscript.php "$6) }'
And if I use tail -l #lines it works, or if after a while I ctrl-c, it then works. The behavior I wanted though was to as the tail got a bad ID to kick off the script to fix the bad ID. Not wait until an EOF or some tail buffer...
I'm having similar problem. There's something funny with tail -f and grep -o combination when ssh.
So on local server, if you do
tail -f myfile.log |grep -o keyword
It grep just fine.
But if you do it from remote server....
ssh user#server 'tail -f myfile.log |grep -o keyword'
doesn't work
But if you remove -f from tail or -o from grep, work just fine... weird :-/

Categories