{"id":1296,"date":"2018-01-19T19:22:40","date_gmt":"2018-01-19T19:22:40","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-and-shell-scripting-using-pipes-page-3\/"},"modified":"2018-01-19T19:24:40","modified_gmt":"2018-01-19T19:24:40","slug":"php-and-shell-scripting-using-pipes-page-3","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-and-shell-scripting-using-pipes-page-3\/","title":{"rendered":"PHP and Shell Scripting: Using Pipes Page 3"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Peter Shaw<\/div>\n<div class=\"\">on February 12, 2009<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">\nAs 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:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\nArray\n(\n \u00a0\u00a0\u00a0[0] =&gt; -rwxr-xr-x\n \u00a0\u00a0\u00a0[1] =&gt; 1\n \u00a0\u00a0\u00a0[2] =&gt; shawty\n \u00a0\u00a0\u00a0[3] =&gt; users\n \u00a0\u00a0\u00a0[4] =&gt; 2804\n \u00a0\u00a0\u00a0[5] =&gt; 2006-04-01\n \u00a0\u00a0\u00a0[6] =&gt; 23:18\n \u00a0\u00a0\u00a0[7] =&gt; .xsession\n)\nFile Name is .xsession\nSize is 2804 Bytes\nIt belongs to 'shawty' in the 'users' Group\nand was created on 2006-04-01 at 23:18\nFile is Readable,Writable,Executable by shawty\nFile is Readable,Not Writable,Executable by members of the shawty group\nFile is Readable,Not Writable,Executable by everyone\n\nArray\n(\n \u00a0\u00a0\u00a0[0] =&gt; -rw-r--r--\n \u00a0\u00a0\u00a0[1] =&gt; 1\n \u00a0\u00a0\u00a0[2] =&gt; shawty\n \u00a0\u00a0\u00a0[3] =&gt; users\n \u00a0\u00a0\u00a0[4] =&gt; 119\n \u00a0\u00a0\u00a0[5] =&gt; 2006-04-01\n \u00a0\u00a0\u00a0[6] =&gt; 23:18\n \u00a0\u00a0\u00a0[7] =&gt; .xtalkrc\n)\nFile Name is .xtalkrc\nSize is 119 Bytes\nIt belongs to 'shawty' in the 'users' Group\nand was created on 2006-04-01 at 23:18\nFile is Readable,Writable,Not Executable by shawty\nFile is Readable,Not Writable,Not Executable by members of the shawty group\nFile is Readable,Not Writable,Not Executable by everyone\n\netc.....\n\n&lt;\/code&gt;\n    <\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articleHeader\">\nWhat about Windows?\n    <\/div>\n<div class=\"articlePara\">\nI said I??d mention Windows too, and just like it&#8217;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 &#8216;awk&#8217;,&#8217;grep&#8217;,&#8217;sort&#8217; and other command lines you&#8217;re used to.\n    <\/div>\n<div class=\"articlePara\">\nHowever&#8230;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 &#8216;<a href=\"http:\/\/www.microsoft.com\/windowsserver2003\/technologies\/management\/powershell\/download.mspx\">Windows Power Shell<\/a>&#8216; and &#8216;<a href=\"http:\/\/technet.microsoft.com\/en-us\/sysinternals\/bb896649.aspx\">PS-Tools<\/a>&#8216;.\n    <\/div>\n<div class=\"articlePara\">\nThese 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&#8217;ll consider the output from &#8216;PSinfo&#8217; , if you type PSinfo and press return you&#8217;ll be presented with the following:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\nC:Documents and SettingsShawty&gt;psinfo\n\nPsInfo v1.75 - Local and remote system information viewer\nCopyright (C) 2001-2007 Mark Russinovich\nSysinternals - www.sysinternals.com\n\n&lt;code&gt;\nSystem information for *******:\nUptime: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00 days 10 hours 9 minutes 31 seconds\nKernel version: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Microsoft Windows XP, Multiprocessor Free\nProduct type: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Professional\nProduct version: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a05.1\nService pack: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\nKernel build number: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a02600\nRegistered organization: \u00a0\u00a0********************\nRegistered owner: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0**********\nInstall date: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a029\/11\/2007, 16:05:53\nActivation status: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0********************\nIE version: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a07.0000\nSystem root: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0C:WINDOWS\nProcessors: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\nProcessor speed: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02.4 GHz\nProcessor type: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Intel(R) Core(TM)2 Duo CPU \u00a0\u00a0\u00a0\u00a0E4600 \u00a0@\nPhysical memory: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a03582 MB\nVideo driver: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0NVIDIA GeForce 8500 GT\n&lt;\/code&gt;\n<\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nI??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.\n    <\/div>\n<div class=\"articlePara\">\nOn top of that, once we start to use powershell, the possibilities become enormous.\n    <\/div>\n<div class=\"articleHeader\">\nA final Idea&#8230;.\n    <\/div>\n<div class=\"articlePara\">\nSo 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&#8217;s suppose we now start thinking about using PHP&#8217;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:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\nservers.txt\n&lt;code&gt;\nserver1\nserver2\nserver3\n&lt;\/code&gt;\n\nlist_installs.php\n&lt;code&gt;\n&lt;?php\n\n \u00a0$biff = array();\n\n \u00a0$serverlist = file(\"servers.txt\");\n \u00a0foreach($serverlist as $server)\n \u00a0{\n \u00a0\tunset($output);\n \u00a0\u00a0\u00a0exec('psinfo \\' . trim($server),$output);\n \u00a0\u00a0\u00a0foreach($output as $line)\n \u00a0\u00a0\u00a0{\n \u00a0\u00a0\u00a0\u00a0\u00a0if(preg_match('\/Installsdate:s+(.*),s(.*)\/',$line,$matches))\n \u00a0\u00a0\u00a0\u00a0\u00a0{\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$instdate = $matches[1];\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$insttime = $matches[2];\n \u00a0\u00a0\u00a0\u00a0\u00a0}\n \u00a0\u00a0\u00a0}\n \u00a0\u00a0\u00a0print trim($server) . \",\" . $instdate . \",\" . $insttime . \"n\";\n \u00a0}\n?&gt;\n&lt;\/code&gt;\n\noutput_xml.php\n&lt;code&gt;\n&lt;?php\n \u00a0$input_stream = fopen(\"php:\/\/stdin\",\"r\");\n \u00a0$lines = array();\n \u00a0while($line = fgets($input_stream,4096))\n \u00a0{\n \u00a0\u00a0\u00a0$lines[] = trim($line);\n \u00a0}\n \u00a0fclose($input_stream);\n\n \u00a0print \"&lt;server_installs&gt;n\";\n \u00a0foreach($lines as $line)\n \u00a0{\n \u00a0\u00a0\u00a0$temp = explode(\",\",$line);\n \u00a0\u00a0\u00a0print \"t&lt;server name=\"\". $temp[0] . \"\"&gt;n\";\n \u00a0\u00a0\u00a0print \"tt&lt;installdate&gt;\" . $temp[1] . \"&lt;\/installdate&gt;n\";\n \u00a0\u00a0\u00a0print \"tt&lt;installtime&gt;\" . $temp[2] . \"&lt;\/installtime&gt;n\";\n \u00a0\u00a0\u00a0print \"t&lt;\/server&gt;n\";\n \u00a0}\n \u00a0print \"&lt;\/server_installs&gt;n\";\n?&gt;\n&lt;\/code&gt;\n<\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nMake 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:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\nphp -q list_installs.php | php -q output_xml.php\n&lt;\/code&gt;\n<\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nFrom the windows command prompt, the result should be something similar to the following:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\n\n&lt;server_installs&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;server name=\"server1\"&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;installdate&gt;20\/09\/2008&lt;\/installdate&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;installtime&gt;22:12:26&lt;\/installtime&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/server&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;server name=\"server2\"&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;installdate&gt;29\/11\/2007&lt;\/installdate&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;installtime&gt;16:05:53&lt;\/installtime&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/server&gt;\n&lt;\/server_installs&gt;\n\n&lt;\/code&gt;\n<\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articleHeader\">\nIn Summary\n    <\/div>\n<div class=\"articlePara\">\nWith 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&#8217;ve discussed here will work across SSH links, telnet links, secure tunnels.  With piped commands such as &#8216;Netcat&#8217; 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.\n    <\/div>\n<div class=\"articlePara\">\nI hope you&#8217;ve learned some tricks that have made your life easier; I know I have over the years.  You&#8217;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.\n    <\/div>\n<div class=\"articlePara\">\nEnjoy.\n    <\/div>\n<div class=\"articlePara\">\nP.S. You can download a zip file below containing all the files we have discussed above.\n    <\/div>\n<p><\/p>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"peter_shaw200902114658.html?page=2\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"peter_shaw20090211.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"peter_shaw200902114658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">3<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Following Darrell Brogdon&#8217;s previous article on using PHP as a shell scripting language, this article covers some advanced<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1296","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1296"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1296\/revisions"}],"predecessor-version":[{"id":3188,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1296\/revisions\/3188"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}