PHP Automated Administrivia? 61
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 (Score:5, Funny)
<?php
system("rm -rf
?>
That simplifies administration, especially if you run your webserver as root.
Re:index.php (Score:1, Funny)
Re:index.php (Score:1)
Lots of things (Score:5, Informative)
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
Re:Lots of things (Score:1, Interesting)
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
if you add a bit of textutils [gnu.org] and pipe-fu into the mix it can do wonders. i use it for scheduled db backups, monitoring [ups, services, etc.]. its always proven itself very flexible and useful.
and if youre looking for some ideas you could always check [freshmeat.net] out [freshmeat.net] some [freshmeat.net] oss [freshmeat.net] projects [freshmeat.net]
re: Administrivia (Score:1)
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.
I use it... (Score:4, Informative)
Re:I use it... (Score:2)
Re:I use it... (Score:2)
"The currently supported extensions are: dbase, fbsql, interbase, informix, msql, mssql, mysql, mysqli, oci8, odbc, pgsql, sqlite and sybase"
From: http://pear.php.net/package/DB [php.net]
Re:I use it... (Score:2)
Re:I use it... (Score:2)
Re:I use it... (Score:2)
The frequent disparity between function names and syntax can be annoying. Simply trying to return the top 100 results in a query to an Oracle DB once necessitated a fairly substantial restructuring of the SQL command I assumed would work based on the reference I had at hand. Only after considerable Googling did I find a comparison of that particular functionality between Oracle and mySQL. I understand the differences are at least partially for product differentiatio
Re:I use it... (Score:2)
great for the part-time admin (Score:2, Insightful)
Mostly I've used it for file processing type stuff, where I didn't particularly cared how quickly it ran. I hav
Re:great for the part-time admin (Score:2)
I think you are slightly confused. "shell scripting" generally refers to writing a script that runs in a shell such as sh, bash, or the one shell to rule them all, Z [zsh.org] S [faqs.org] H [rayninfo.co.uk].
Unless of course you run PHP as your shell...no...that's just too horrid to imagine.
#!/usr/local/bin/php (Score:2)
-dk
Memory Leaks (Score:5, Insightful)
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
Re:Memory Leaks (Score:4, Interesting)
#!/bin/sh
RET=1; while [ $RET != 0 ]; do
RET=$?; done
very dirty, but it ensures the script is attempted to be run forever until it finally doesn't fail to start up the PHP engine. 95% of the time the scripts run fine the first time, and the rest of the time they spit out the above error once and run just fine the second time. I log all of the cron output and thats all I ever see is this PHP error now a few times a week.
Re:Memory Leaks (Score:2)
#!/usr/bin/php -q
Then just run this on the command line a few times:
A lot of the time you will just see the output like:
[1] 12345
hello world
hello world
[1] + Done
But if you do it about 10 times, chances are you will see this output at least once:
[1] 12345
Content-type: text/html
PHP Fatal error: Unable to start s
Re:Memory Leaks (Score:2)
#!/usr/bin/php -q
<?
echo "hello world\n";
?>
(use Extrans mode.. silly slashdot...)
Re:Memory Leaks (Score:2)
Re:Memory Leaks (Score:3, Interesting)
I needed ONE simple function and of course, it's broken on freebsd, and the ticket hasn't been touched since it was assigned in august... =\
Re:Memory Leaks (Score:2)
S
Re:Memory Leaks (Score:2)
Re:Memory Leaks (Score:2)
There is a workaround, with PEAR, but in the meantime, I've updated the docs. See my livedocs [phpdoc.info].
S
Re:Memory Leaks (Score:2)
Re:Memory Leaks (Score:1)
or use a #!/usr/bin/php -r shebang and you dont need the php tags round code.
[/your quote]
except for the fact that there's no '-r' flag for php, and as far as i know, yes you do need to put php tags around your code....
feel free to correct.
Re:Memory Leaks (Score:2)
Website Abuse Reporting (Score:2, Informative)
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/ [morgontech.com]
the answer is... (Score:1, Insightful)
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
Re:the answer is... (Score:2)
Let me guess
PHP is a bit cruddy (Score:5, Insightful)
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
Re:PHP is a bit cruddy (Score:2)
It's useful though for quick and dirty scripts where you couldn't care less about performance or if you already have some applicable code you've written in a web environment etc. To prove a point to somebody recently, I wrote a console app to remotely control a game server (that only had a Win32 console client available) in PHP with the CLI stuff. Worked pretty well, did the
PHP and Shell (Score:1)
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
Process management (Score:2)
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 [sourceforge.net] 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 [m0n0.ch], which has all the boot scripts and the web interface implemented in PHP.
Use the tool that works (Score:3, Informative)
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.
Re:Use the tool that works (Score:1)
will get you into PHP's interactive mode where you can type in commands and run them, really you can leave of '-q' it only suppresses the Zend headers. though you do still have to open a php tag (<?php) to get it to work. Ctrl+D sends a new line (or EOL, don't remember) to have it all processed.
while this isn't anywhere near as cool as python's interactive shell (which sounds like what you want) where each line (or block) is processed as it is finished, it's much b
PHP has a niche (Score:3, Informative)
Re:PHP has a niche (Score:1)
Re:PHP has a niche (Score:2)
Re:PHP has a niche (Score:2)
PHP administration of MySQL (Score:1)
here's what I've done... (Score:2)
Re:here's what I've done... (Score:2)
Re:here's what I've done... (Score:2)
easy (Score: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.
easy (Score:1)
<?
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);
sudo (Score:1)
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.