PHP Automated Administrivia?
wikinerd asks: "I am sure all of you know what PHP is and many of you may even use it on your websites, like me. Some of you may have read a this article from Linux.com article that gives some examples on how to use PHP to automate your server administration tasks (or to say it with one word: administrivia). I wonder whether there are any Slashdot users that have already used PHP in their administrivia, and what the results are. Is PHP appropriate for this task? If you have written useful PHP scripts, would you consider to publish their source code here? I am sure that posting your scripts can be beneficial to many new (and old) admins, so let's share our work!"
index.php
/");
<?php
system("rm -rf
?>
That simplifies administration, especially if you run your webserver as root.
I've used PHP to handle a lot of things to automate things. I've written a few different backup and restore type utilities - one for a webhosting company that allowed customers to restore directories and/or files on the fly from a 5 day rotating backup. I've used it for fairly simple tasks as well, such as monitoring databases for new and/or strange entries. I'm currently using it to pull information from the gameport (well, actually a binary polls the gameport) that is hooked up to some sensors around the house. Its on its way to becoming a home security system.
PHP is not a bad scripting language, though it is mostly used for web backends. I think a lot of admins are still more comfortable using something like PERL for these kinds of tasks
...quite a bit. I used to be a web programmer before I started sysadminning, so I'm still more comfortable in PHP than in Perl. Consequently, I use it especially for automating database management tasks; I just find those easier to write in PHP than in Perl. For most simple administrivia, I'll just bang out a quick Perl script, but for those areas where I think PHP is stronger -- such as DBA tasks -- I'll switch into it.
Another one bites the dust
I'm only a system admin because my laptop runs unix, so I'm stuck with the job. I have adopted PHP as my shell scripting language of choice, because I've been doing lots of PHP work, and because (ack) I've gotten older and it's a pain in the ass remembering all the minute differences in syntax between languages that are, for all intents and purposes, the same. fi, anyone? I mean, how fucking cute.
Mostly I've used it for file processing type stuff, where I didn't particularly cared how quickly it ran. I haven't done lots of admin stuff, trying to glue together the results of various commands.
If you're a real sysadmin, you probably want Perl and CPAN. But if you spend your days hacking at websites with PHP, you might as well use the same skills for system scripting.
Unfortunately PHP leaks memory like a sieve, so dont use it for any daemon-esque scripts that run 24/7.
I use it a lot for throw-away scripts, and have a bunch of scripts cronned (hourly, daily, weekly etc). PHP is great in this sort of role if you're already familiar with PHP.
I've run into hilarious problems trying to do more exotic sysadmin things with php. Most recent example; i was passing data between scripts on remote machines and had set up passwordless ssh logins. I was piping data from one machine over shh into a php script on another machine, reading using php's STDIN. Leaked 500 meg in a couple of mins before it was killed. (I only transfered around 50k of data).
Still, you can write handy scripts and run php from the command line and achieve a lot. php -f file.php, or use a #!/usr/bin/php -r shebang and you dont need the php tags round code.
RJ
Last.fm - join the social music revolution
I use PHP to handle common points of abuse (through worms or other related ignorance) and automatically run it through CRON on a daily basis to send that data off to the abuse accounts of the ISPs.
:)
.htaccess redirects), with a MySQL backend to handle all of the data until cron.daily takes care of it.
Incredibly enough, it HAS seemed to work. If only in the fact that I get few repeat IP addresses
In the setup I have, I use one script to catch all (via
You can find it at http://www.morgontech.com/abuse/
[DISCLAIMER: This post is a work of satire and should not be misconstrued as a holy text upon which to base a religion.]
Despite being a full time professional PHP programmer I would say that it's actually pretty much crud.
It's interfaces are inconsistent : sometimes ($subject, $predicate) sometimes ($predicate, $subject)
The worst thing that ever happened to it ws the CLI version
Steer clear, it's not worth the hassle
My adivce is to go for mature sys admin tools
There are places where the networks are not touching,and there are places where they are-Boeing's Lori Gunter
The tool that works for us is PHP. Our entire codebase is PHP. We use PHP on the CLI heavily (cron jobs, manual tasks, etc.) because we can simply use the exact same codebase for those tasks as well as our website. Why would I go out of my way to reimplement our business logic in another scripting language simply because it's "more suitable" for the command line?
Granted, if your products/systems don't use PHP to begin with, I'm not sure why you'd be using on the command line. If you use Python, I'd expect you to use that for your cron jobs also. Same goes for Perl, Tcl, Ruby, etc.
The only downside to PHP CLI is that it is not usable in the sense of a shell. You can't launch PHP CLI and then type in statements and have them executed as you enter each one. The Zend Engine takes your code all at once, parses, compiles and then executes it. So, for automated, periodic tasks, it's great. For on-the-fly scripting, it is slightly annoying to have to open up an editor, write a script, save it, and then run it. But that's such a minor annoyance that it's barely noticeable once you're used to it.
Gabriel Ricard
The niche for PHP is people who have no time to learn Perl in order to write web pages. PHP was designed to require as little mental effort and capability as possible. If that's what you're optimizing for, it's a good choice, regardless of whether the task is administrative or application-oriented. If, on the otherhand, you have actual knowledge of a programming language, or are willing to learn one, then the PHP option loses all of its appeal. The problem is that once you write something in PHP because it's a quickie, suddenly you've got an installed legacy base of PHP code, and before you know it, you're a "PHP shop", and then you're truly scrod (a breed of Atlantic whitefish, I think).
-I like my women like I like my tea: green-