Program Execution Through PHP

One of several powerful PHP functions is program execution function. Those provides means to executes commands on the system itself, and means secure such commands. Just simply imagine that you have a better flexible terminal right in front of you. One sure great advantage is that it working on cross platform OS. If you have PHP based on Linux server, you may passed tons of Linux commands on it and so on if you have PHP based on others OS such as Windows or Macintosh. Especially a PHP through Linux which combines with BASH shell script, this sturdy kit can act as your personal control panel controlling any machine you want.

Well, in this chance, I’d like to share you about a specific case happened to me while I had trouble to manage tens of internal servers around me. What I need is a small tool which able to give information whether the servers are alive or not from everywhere. Everywhere means that I can check it from anywhere at anytime, in my house or with my PDA far away from the office or simply using (wifi) public internet access with laptop. So I can take an action quickly if the problem appears (except for electricity or failure hardware problems). Even that I can get in one of the public IP and check it out the box one by one over SSH tunnel, but it will took lots of time.

To solve this, here’s a simple logic a bit of PHP code containing exec with ping command. exec command purposed to execute an external system command, which is ping. This small application prepared with two submit button, one for single IP ping action and one for all listed IP’s server machine. This is just a variation of the program so that it purpose to any use. Take a look at the core code below:


<?
if (isset($_POST['submit']))
{
$host=$_POST['host'];
$max_count=3;
echo("Hasil:<br>");
exec("ping -c $max_count $host",$list);
echo(count($list));
for ($i = 0; $i < count($list); $i++) echo $i . $list[$i]."<br>";
}
?>


Note that the use of ping –c parameter only works when the application hosted on Linux server. If you have it on Windows server, you may use ping –n. The using of this count parameter are purposed to limit the output result (from $max_count variable). Now, here below is the response if I take a test to yahoo.com server with Ping Custom button:



The others button, contain list of all internal IP machines database. Once the button hit, it will process whole of IP’s sequentially. Even though that there’s a small numbers of latency, but it’s worthy. Take a look at below result:



This kit is a pure web based application, but browser is not a must have to run it on the client. Get a Linux box near you, and supply it a text based browser (such lynx or elinks) and you can use it under terminal. Here’s below what it will look like within the console box (it’s a Fedora Core 4 Server accessed from public RedHat Enterprise Linux AS server 4 with elinks and remotes from Windows XP as client over puTTy):



The example above is a basic model script utilizing PHP program execution function. There are lot of ways you can take control a machine remotely appropriate to your necessity. But one sure thing is; create a system with safe environment. Saving the time, saving the money and saving the system. Aarghh… you know what I’m talking about.

Labels:


PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment

 

Post a Comment

Leave comments here...