Program execution

Running basic command


uname

codes/uname.php:

<?php
  echo shell_exec('uname -a');
?>

http://localhost:8080/program-execution/codes/uname.php:

$ curl -i http://localhost:8080/program-execution/codes/uname.php
HTTP/1.1 200 OK
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.3.12
Vary: Accept-Encoding
Content-Length: 88
Content-Type: text/html; charset=UTF-8

Linux 1bebf23dc289 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 GNU/Linux

crontab list

codes/crontab-list-tasks.php:

<pre>
<?php
echo shell_exec("crontab -l");
?>

http://localhost:8080/program-execution/codes/crontab-list-tasks.php:

$ curl -i http://localhost:8080/program-execution/codes/crontab-list-tasks.php
HTTP/1.1 200 OK
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.3.12
Content-Length: 6
Content-Type: text/html; charset=UTF-8

<pre>

crontab add

codes/crontab-add-task.php:

<pre>
<?php

$crontab = shell_exec("crontab -l");
$command = "00 09 * * 1-3 echo hello";

shell_exec("crontab <<EOF
${crontab}
${command}
EOF");

echo shell_exec("crontab -l");
?>

http://localhost/program-execution/codes/crontab-add-task.php:

$ curl -i http://localhost/program-execution/codes/crontab-add-task.php
HTTP/1.1 200 OK
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.3.12
Content-Length: 58
Content-Type: text/html; charset=UTF-8

<pre>

00 09 * * 1-3 echo hello

ping times

codes/ping-times.php:

<?php

  function ping($address, $count=4) {
    $command = "ping -c${count} ${address} | grep icmp_seq | awk -F '[:=]'  '{print $5}'| cut -f1 -d' ' | tr '\n' ',' ";
    $result = shell_exec($command);
    return substr($result, 0, -1);
  }

  echo ping('8.8.8.8', 3);
  
?>

http://localhost:8080/program-execution/codes/ping-times.php:

$ curl -i http://localhost:8080/program-execution/codes/ping-times.php
HTTP/1.1 200 OK
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.3.12
Content-Length: 15
Content-Type: text/html; charset=UTF-8

68.8,67.5,68.10

ping output

codes/ping.php:

<pre>
<?php

// get host
$host = $_GET['host'] ?? null;

// create result
$result = '';
if ($host) {
  $command = "ping -c1 {$host}";
  $result = shell_exec($command);
} else {
  $result = 'host not found';
}

// echo result
echo $result;

?>

http://localhost:8080/program-execution/codes/ping.php?host=8.8.8.8:

$ curl -i http://localhost:8080/program-execution/codes/ping.php?host=8.8.8.8
HTTP/1.1 200 OK
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.3.12
Vary: Accept-Encoding
Content-Length: 251
Content-Type: text/html; charset=UTF-8

<pre>
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=37 time=68.7 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 68.650/68.650/68.650/0.000 ms

References