PHPBuilder - PHP and Shell Scripting: Using Pipes Page 3



RSS Twitter
Articles Php Functions

PHP and Shell Scripting: Using Pipes - Page 3

by: Peter Shaw
|
February 12, 2009

As part of this article, I’ve provided a slightly more lengthy example that allows you to use the pipe techniques above to produce a list like this:
<code>
Array
(
    [0] => -rwxr-xr-x
    [1] => 1
    [2] => shawty
    [3] => users
    [4] => 2804
    [5] => 2006-04-01
    [6] => 23:18
    [7] => .xsession
)
File Name is .xsession
Size is 2804 Bytes
It belongs to 'shawty' in the 'users' Group
and was created on 2006-04-01 at 23:18
File is Readable,Writable,Executable by shawty
File is Readable,Not Writable,Executable by members of the shawty group
File is Readable,Not Writable,Executable by everyone

Array
(
    [0] => -rw-r--r--
    [1] => 1
    [2] => shawty
    [3] => users
    [4] => 119
    [5] => 2006-04-01
    [6] => 23:18
    [7] => .xtalkrc
)
File Name is .xtalkrc
Size is 119 Bytes
It belongs to 'shawty' in the 'users' Group
and was created on 2006-04-01 at 23:18
File is Readable,Writable,Not Executable by shawty
File is Readable,Not Writable,Not Executable by members of the shawty group
File is Readable,Not Writable,Not Executable by everyone

etc.....

</code>
    
What about Windows?
I said I’d mention Windows too, and just like it's Linux counterpart, the Windows command line can use pipes too, and in the same manner, and as I mentioned before if you go to the Gnu WIn32 project you can combine your scripts with all the usual 'awk','grep','sort' and other command lines you're used to.
However...that’s just the start, under Windows we are fortunate to have 2 sets of tools at our disposal than can find out some clever information. These tools come in the form of 'Windows Power Shell' and 'PS-Tools'.
These tools can query and retrieve all manner of system information, usually not just from a local machine but from a remote one also. As an example, we'll consider the output from 'PSinfo' , if you type PSinfo and press return you'll be presented with the following:
C:\Documents and Settings\Shawty>psinfo

PsInfo v1.75 - Local and remote system information viewer
Copyright (C) 2001-2007 Mark Russinovich
Sysinternals - www.sysinternals.com

<code>
System information for \\*******:
Uptime:                    0 days 10 hours 9 minutes 31 seconds
Kernel version:            Microsoft Windows XP, Multiprocessor Free
Product type:              Professional
Product version:           5.1
Service pack:              2
Kernel build number:       2600
Registered organization:   ********************
Registered owner:          **********
Install date:              29/11/2007, 16:05:53
Activation status:         ********************
IE version:                7.0000
System root:               C:\WINDOWS
Processors:                2
Processor speed:           2.4 GHz
Processor type:            Intel(R) Core(TM)2 Duo CPU     E4600  @
Physical memory:           3582 MB
Video driver:              NVIDIA GeForce 8500 GT
</code>
I’ve removed some of the key details for reasons of security, but you can see that it provides a basic overview of your PC. This output could be piped into a PHP filter, saved into a database, and then printed to the console or any other destination, maybe even another pipe. Within the PStools suite, we have the possibility to display Running process lists, Running services lists, Event log entry’s and many many more variables.
On top of that, once we start to use powershell, the possibilities become enormous.
A final Idea....
So you can use PHP to construct some very advanced pipe filters, but is that all you can do? Not by any stretch of the imagination. Let's suppose we now start thinking about using PHP's built in Exec functions for running processes. We could then use the file handling commands to read in a list of servers, then exec a given set of commands on those servers and collect the output. This output could then be piped out to another PHP script, and sent to a database, or turned into an XML feed. I’ll leave a final solution to this for you the reader to work out, but as a starting point, you could use something like this:
servers.txt
<code>
server1
server2
server3
</code>

list_installs.php
<code>
<?php

  $biff = array();

  $serverlist = file("servers.txt");
  foreach($serverlist as $server)
  {
  	unset($output);
    exec('psinfo \\\\' . trim($server),$output);
    foreach($output as $line)
    {
      if(preg_match('/Install\sdate:\s+(.*),\s(.*)/',$line,$matches))
      {
        $instdate = $matches[1];
        $insttime = $matches[2];
      }
    }
    print trim($server) . "," . $instdate . "," . $insttime . "\n";
  }
?>
</code>

output_xml.php
<code>
<?php
  $input_stream = fopen("php://stdin","r");
  $lines = array();
  while($line = fgets($input_stream,4096))
  {
    $lines[] = trim($line);
  }
  fclose($input_stream);

  print "<server_installs>\n";
  foreach($lines as $line)
  {
    $temp = explode(",",$line);
    print "\t<server name=\"". $temp[0] . "\">\n";
    print "\t\t<installdate>" . $temp[1] . "</installdate>\n";
    print "\t\t<installtime>" . $temp[2] . "</installtime>\n";
    print "\t</server>\n";
  }
  print "</server_installs>\n";
?>
</code>
Make sure all 3 files are in the same folder, and that psinfo is in your path; also remember to alter servers.txt to a list of your own Windows servers then run:
<code>
php -q list_installs.php | php -q output_xml.php
</code>
From the windows command prompt, the result should be something similar to the following:
<code>

<server_installs>
        <server name="server1">
                <installdate>20/09/2008</installdate>
                <installtime>22:12:26</installtime>
        </server>
        <server name="server2">
                <installdate>29/11/2007</installdate>
                <installtime>16:05:53</installtime>
        </server>
</server_installs>

</code>
In Summary
With a little imagination, and armed with a few techniques, PHP can be a powerful ally, you can connect things to other things and the only limits are your PHP ability’s and your imagination. Note also, everything we've discussed here will work across SSH links, telnet links, secure tunnels. With piped commands such as 'Netcat' you can pipe the output from your routers and switches, you can list and report on remote FTP directories if you use these techniques with wget and lynx, and with a little patience you can make scripts that run un-modified on both Windows and most Linux/Unix variants.
I hope you've learned some tricks that have made your life easier; I know I have over the years. You'll very quickly find that you build up a rather large library of small chunks of PHP code, all of which can be chained together in some way, like a giant digital Lego set.
Enjoy.
P.S. You can download a zip file below containing all the files we have discussed above.



« Previous Page
1
|
2
|
3

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Peter Shaw

Comment:



Comment:

(Maximum characters: 1200). You have characters left.