![]() Join Up! 96812 members and counting! |
|
|||
PHP and Shell Scripting: Using Pipes
Peter Shaw
Following Darrell Brogdon's previous article on using PHP as a shell scripting language, this article covers some advanced uses of the language and shows some tricks that make PHP an extremely useful language to work with.
Unlike Darrel's article however, this one will also cover the Windows version of PHP as well as the Linux/Unix one. First, however, there are a couple of differences you need to be aware of.
So what are the differences?
The differences between the two environments are mostly due to operating system differences as opposed to differences in PHP itself. The big one has to be case sensitivity in file names; this has nailed me on many an occasions. A very good habit to get into to avoid this, is to always use lowercase filenames no matter what. How you do it is up to you, but lowercase filenames have always worked for me. The other thing you need to be very aware of are the differences in the file system.
Windows uses a system of drive letters and separate disks, where as most posix based systems (this is the technical name for Linux and Unix variants) treat all file systems and disks as one large directory tree. If you're only working in one folder this should not present a problem, but if you’re working with globs and other file system functions, you need to be aware of this.
The 3rd most important thing is one of binary versus text when writing files. Under Linux/Unix, all files are handled identically and that is as a byte stream, whereas under Windows the situation is a little different. Windows treats text files as a separate type than binary files. Thankfully in the latest builds of PHP this is no longer an issue to be concerned with as the fopen calls now default to binary mode.
The last thing to be aware of is line endings. Under Windows these take the form \r\n as opposed to just \n on Linux/Unix.
So to sum up, watch out for position in the file system, check your filenames and be cautious of line endings.
Why are these differences important in a Shell environment?
First and foremost, the case situation will manifest itself when running scripts. A script named 'MyScript.php' will happily run from the command prompt using 'PHP -q myscript.php' on Windows, but will fail miserably on Linux. File system position will show up when using glob functions, and other directory scanning functions. Lastly the line ending issue is most likely to surprise you if your parsing files, text streams or the output from commands. From this point on, I’ll not mention these points again, but if something is not working as you expect, then keep them in mind.
So what neat things can we do with PHP in the shell?
Well, the answer to that is pretty much anything. I use PHP for a huge amount of different tasks, from dumping files to chucking together quick test models or proof of concept ideas. Over the years however, one of the great things I’ve found with PHP is its ability to hack together quick pipe filters.
For those of you who are not up to speed on pipe filters, a quick explanation is in order. When the predecessors of today’s modern OS's where developed back in the 60's the idea of one tool, many uses was a very common one. Shell programming spawned a whole generation of people who spent insane amounts of time writing ridiculously long command lines, to perform some quite long tasks. It’s for this very reason that today most OS's (Windows included) have a very rich set of separate programs that each do a small but efficient task.
Consider this example (running under Linux):
If you install the Gawk package from the win32 GNU utilities page at http://gnuwin32.sourceforge.net/ then you can also do the following:
Which will give:
As you can see the outputs are very similar, but not identical, however this is not an article about shell programming in general, it's about putting PHP to some advanced uses. You can see from the examples above that commands are joined with the vertical bar character '|' as this is the pipe character, and to cut this short basically means take the output of one command and feed to the next via the standard input stream.
This means if we read the standard input stream using the php file stream 'php://stdin' then we can use PHP to construct blocks of code that can participate in the filter chain.
Enough talking already, give us some code!!!!
Ok ok, I hear you. So by way of an example to show what I’m on about, we'll put together a small example under Linux that shows what those file permission flags in a file listing mean.
First, if we do an 'ls -al' we should see something like this:
The first thing we need to do here is to read this into a PHP script.
If you run this from a Linux command line using 'ls -al | php -q fancydir.php' (replace php file name as required), you'll get something like the following:
As you can see, the directory listing is in an array, line by line as sent to the script. We can now use this array to go over each line and redisplay it, something like this:
This would give you a number listing something like the following:
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:
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:
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:
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:
From the windows command prompt, the result should be something similar to the following:
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 containing all the files we have discussed above.
|