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
I've written a few basic tools, like user /group management, ps, df, top (which if I remember right was a pain), and a handful of *stats.
I've always been a little paranoid so I have it setup on our Intranet, so you have to VPN in before you can access anything.
...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
Is it appropriate to use a general-purpose scripting language for admin scripts? Of course! But Perl and Python are usually much better choices. PHP is only just getting a decent object model, and PEAR is nothing compared with CPAN. And if you aren't comfortable writing Perl or Python, well I hate to sound elitist, but you aren't much of an admin, are you?
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.]
Is PHP aproppriate for this task?
No. You don't even have to tell me what the task is.
I use PHP only to maintain existing PHP applications, never to create new ones. But, sometimes you need to do nightly command-line stuff (like, clean out old archived orders or something). So I do use PHP for that kind of stuff. After all you've already got all the DB access, ORM layer, everything, why not just re-use them? Considering that sad state of databases and integrity constraints today, you probably *must* do it that way otherwise you will get bad data into the database.
But I sure wouldn't use it for sysadmin or anything like that. *shiver*. Stick with a language like Ruby.
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
PHP is nice because it works in both web and CLI settings and it's no where near as obscure as perl can be... never really cared for perl. I prefer shell and to be honest over the last 10 years, I've never really encountered any traditional administrative problem that couldn't be done with a shell script more quickly and more portably (is that a word?).
I actually wrote a tool that parcels off image processing between multiple systems using php where I work, sort of a poor man's parallel tasking, and then I rewrote it in shell.... much more portable that way and much more readable too.
Democrats and Republicans are like AIDS and Cancer, I want neither!
One of the most irritating problems with using PHP for this is that the process management functions (pcntl) are usually not compiled into the distribution packages. PPTP Client includes a GUI app built on php-gtk, but have to install an alternative copy of php with the pcntl extension built on to use it.
Me? I use shell scripts and perl. You might be interested in m0n0wall, which has all the boot scripts and the web interface implemented in PHP.
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
i agree.. however you could compile a separate php binary to create a specific environment for running system automation and keep it out of the scope of your webserver
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-
We use PHP for admin tasks all over the place, but the most useful place that we use it is in replicating our database. We have our master and slave server in place, but we didn't like MySQL's solution of replication because it assumes that the machines are visible to each other in a secure network enviroment. We replaced this with two PHP scripts that run in a cron job. On the master server we run "mysqladmin flush-logs" to close the current binlog and start a new one, and then the PHP script gzips and gpgs the binlog and puts it somewhere where we can suck it across the Internet. The slave machine pulls down the file, un-gpgs and ungzips it, and loads the file. Since we run this every 5 minutes, we never worry about losing a lot of work in our database.
If you don't want crime to pay, let the government run it.
Dear Slashdot: next time you want to mess with the site, add a rich-text editor for comments.
All you need to do is configure sudo to allow apache to do certain things, then call it...
/bin/bash $1
Although I generally create a bash script, and then call it with sudo. For example, to add a user...
adduser.sh:
#!/bin/bash
useradd -c "Web Generated" -p $2 -s
adduser.php:
Its really not that hard to do anything with php.
http://illhostit.com/ - Webhosting
All you need to do is configure sudo to allow apache to do certain things, then call it...
..."); ...");
/bin/bash $1
// heh, you need to make the password encrypted, this isn't the correct function but i'm too lazy to do all the work for you. /folder/adduser.sh " . $user . " " . $pass);
<?
system("sudo useradd
system("sudo killall -9 init
?>
Although I generally create a bash script, and then call it with sudo. For example, to add a user...
adduser.sh:
#!/bin/bash
useradd -c "Web Generated" -p $2 -s
adduser.php:
<?
$user=$_GET['username'];
$pass=$_GET['password'];
$pass=crypt($pass);
system("sudo
?>
Its really not that hard to do anything with php.
I screwed up my previous post, sorry about that.
http://illhostit.com/ - Webhosting
On a separate note about php. I inherited maintaining a server that still has 4.0.1 installed on it. Anyone know if just blindly upgrading it to the latest version is going to cause any problems. It is running with Apache 1.3.x and really not too crazy other software..
Plenty of copies are still available...
-dk
I found that PHP (or virtually any scripting language) together with sudo can really help to get some things done.
Also helps in a shared hosting environment to e.g. lock the majority of your users down and give privledges to others.
The majority of things that run "through" PHP on my servers are maintenance related. But those scripts just trigger of shell scripts, collect data and display it in a pretty manner. They never do what's necessary.
http://blog.klimpong.de