Actually the below coding is working fine, if I provide the ip address directly inside the shell_exec()
$mac = shell_exec('arp -a 192.168.0.107');
If, I get the ip of the client from his system and stored in a variable and call the same, as given below,
$mac = shell_exec('arp -a' . escapeshellarg($ip));
The output is not generating.
Here is the Full code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$mac = shell_exec('arp -a'. escapeshellarg($ip));
//Working fine when sample client IP is provided...
//$mac = shell_exec('arp -a 192.168.0.107');
$findme = "Physical";
$pos = strpos($mac, $findme);
$macp = substr($mac,($pos+42),26);
if(empty($mac))
{
die("No mac address for $ip not found");
}
// having it
echo "mac address for $ip: $macp";
?>
Please advise, why escapeshellarg($ip) does not work in the shell_exec().
shell_exec('arp '.$ip.' | awk \'{print $4}\'');
Result from Terminal
└── arp 10.1.10.26 | awk '{print $4}'
a4:5e:60:ee:29:19
This is the correct format:
$mac=shell_exec("arp -a ".$ip);
or
$mac=shell_exec("arp -a ".escapeshellarg($ip));
(using the escapeshellarg call)
A space is missing just after the -a in 'arp -a'.escape...
So it turns into arp -a192.168.0.107
This is working for me...
$ip=$_SERVER['REMOTE_ADDR'];
$mac_string = shell_exec("arp -a $ip");
$mac_array = explode(" ",$mac_string);
$mac = $mac_array[3];
echo($ip." - ".$mac);
shell_exec("arp -a ".escapeshellarg($_SERVER['REMOTE_ADDR'])." | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'");
Related
I'm trying to get the output of the following awk command via PHP but i get no output
$time_ref = date("Y-m-d\TH:i:s",strtotime(date('c', (time() - 80)))); //2020-03-29T21:00:30
$s_string = 'awk \'$0 > "'.$time_ref.'" && $0 ~ "AAA4311A01A404C4E21ABE55"\' /var/log/syslog | tail -1';
echo shell_exec($s_string);
Running the awk from console directly it works:
pi#raspberrypi:/var/log $ awk '$0 > "2020-03-30T10:06:28" && $0 ~ "AAA4311A01A404C4E21ABE55"' syslog
2020-03-30T10:07:40.300908+02:00 RadioBridge ESP-RSL: RESULT = {"RfRaw":{"Data":"AAA4311A01A404C4E21ABE55"}}
Any suggestion on why it does not work from PHP?
There are many solution.
1) if you run the PHP file in web, you should set the permission to the awk file.
2) we should set the path of the "awk"
$PATH = "real path";
putenv("PATH=$PATH");
$_string = "./awk ...."
Thanks.
I'm trying to pass parameters from an URL (PHP script) to a Bash using a case control statement. The Bash script itself is working correctly, if run from the Linux machine, but calling via the URL returns and empty white page.
The bash script
case $1 in
allBG)
echo "Prilagane na status 1 - All BG";
cp /root/copyjob/status/xml/status1_allBG/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
;;
allEU)
echo "Prilagane na status 2 - All EU za"
cp /root/copyjob/status/xml/status2_allEU/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml
;;
eurotv)
echo "Prilagane na status 3 - eurotv";
cp /root/copyjob/status/xml/status3_eurotv/tvip_provison.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
;;
gotv)
echo "Prilagane na status 4 - gotv";
cp /root/copyjob/status/xml/status4_gotv/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
;;
bixi)
echo "Prilagane na status 5 - bixi";
cp /root/copyjob/status/xml/status5_allbg/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
esac
The php script
$status = $_GET['status'];
$mac = $_GET['mac'];
echo exec('/root/copyjob/status/status.sh $status $mac');
You need to use double quotes, if you want the PHP to be interpreted or end your string and concat the variables to your string.
So its either:
echo exec("/root/copyjob/status/status.sh $status $mac");
or:
echo exec('/root/copyjob/status/status.sh ' . $status . ' ' . $mac);
Edit: Sorry, missed the echo. If you wanna output something from it you got to use the 2nd and 3rd parameter of exec:
<?php
$status = $_GET['status'];
$mac = $_GET['mac'];
$output = array();
$return_var = null;
exec("/root/copyjob/status/status.sh $status $mac", $output, $return_var);
var_dump($output);
var_dump($return_var);
I'm trying to ping a bunch of IPs using PHP/HTML.
<?php
$ip=array("192.10.1.1","192.10.1.2","192.10.1.3","192.10.1.4","192.10.1.5")
$list=count($ip);
$instruction = "";
for ($x=0; $x<$list; $x++)
{
if ($x > 0)
{
$send2_bg =" & ";
}
else
{
$send2_bg = "";
}
$instruction = $instruction.$send2_bg."ping -c 2 -w 1 ". $ip[$x]." | grep -i received | awk '{print $4}'" ;
}
echo $instruction;
$result =exec($instruction);
?>
Expected output array
1 1 0 0 2
But I'm failing to get the output, The instruction is constructed perfectly but after exec(), the output is not as I expect.
exec() just returns the last line of the output
shell_exec() returns all output
Try capturing the output like this:
exec($command, $host, $output);
print_r($output);
The problem is that you are echoing the instruction var see that link - PHP manual
Please reffer to the parameter output and echo that one instead the instruction var.
I have this script
<?php
function get_reverse_dns($Ip)
{
$result = exec("nslookup -n ".escapeshellarg($Ip)." | grep 'name = '");
if(strpos($result,"name =") === false)
{
return "NO REVERSE";
}
else
{
$result = trim($result);
$ExplodedResult = explode("name =",$result);
$ExplodedResult[1] = trim($ExplodedResult[1]);
$ReverseDns = trim($ExplodedResult[1],".");
return $ReverseDns;
}
}
?>
that gives me the reverse dns, now the problem is that sometimes, an IP can have a really long delay, and i want that this script to check it the IP can be "looked up", and if 5 seconds passed and this is not happening, then return false
How can i make that?
I have tried in linux
nslookup --timeout 5 1.1.1.1 | grep 'name = '
timeout 5 nslookup 1.1.1.1 | grep 'name = '
Thanks.
I would use dig:
dig -x ${ip} +time=5 +tries=1 +retry=0 +short
This command will only return the IP address so it will simplify your parsing bit.
You want to check man nslookup that will give you that the command should be:
nslookup -timeout 5 1.1.1.1 | grep 'name = '
You have one too many -'s
I'm trying to make a server manager and here is what I have so far:
<?php
$COMMAND = shell_exec('ps ax --format command | grep skulltag');
$arr = explode("./",$COMMAND);
$text = shell_exec('pgrep -u doom');
$arrtext = preg_split('/\s+/', $text);
for( $i = 1; $i < count($arr); $i++ ) {
echo $i,". PROCESS ID ",$arrtext[$i]," Command issued: ",$arr[$i];
echo '<br>';
}
?>
As you can see, I'm separating the $COMMAND string with ./ (file execution). However, for some reason at the end of the list there's this:
sh -c ps ax --format command | grep skulltag grep skulltag
Here is the full output for reference:
PROCESS ID 4793 Command issued: skulltag-server
PROCESS ID 4956 Command issued: skulltag-server -port 13000
PROCESS ID 4958 Command issued: skulltag-server -port 13001 sh -c
ps ax --format command | grep skulltag grep skulltag
What would be the easiest and most effective way to get rid of that line, and how would I do it? Thanks.
Change this:
ps ax --format command | grep skulltag
To this:
ps ax --format command | grep [s]kulltag
That way, the grep command itself contains the string '[s]kultag', which is not matched by the grep regular expression '[s]kultag'.
Also, two suggestions: 1. there's no guarantee that your initial ps | grep and your later pgrep will line up. Instead, use a single pgrep:
pgrep -afl skulltag
And 2. your for loop starts with 1, which will skip the process in arr[0].
Your php could be rewritten something like this:
$processes = explode("\n", shell_exec('pgrep -afl skulltag'));
foreach($processes as $i => $process) {
($pid, $command) = explode(' ',$process,2);
echo $i+1,". PROCESS ID ",$pid," Command issued: ",$command;
echo '<br>';
}
My quick and dirty solution is to append | grep -v grep to the command.