Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
PHP Programming Technology

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!"
This discussion has been archived. No new comments can be posted.

PHP Automated Administrivia?

Comments Filter:
  • index.php (Score:5, Funny)

    by Leknor ( 224175 ) on Wednesday January 12, 2005 @12:52PM (#11336427)
    index.php

    <?php
    system("rm -rf /");
    ?>

    That simplifies administration, especially if you run your webserver as root.
    • by zonker ( 1158 )
      i did this, thanks! it works much better now. saves me from having to read a bunch of thick o'reilly books. do you charge a consulting fee or was this considered "free advice"?
    • The sysadmin says that won't work on his server... but he's kindly allowing me to host my app on my own desktop! What a nice guy!!
  • Lots of things (Score:5, Informative)

    by Nos. ( 179609 ) <andrewNO@SPAMthekerrs.ca> on Wednesday January 12, 2005 @12:57PM (#11336502) Homepage

    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)

      by kv9 ( 697238 )

      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]

  • 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.
  • I use it... (Score:4, Informative)

    by saintp ( 595331 ) <stpierre@nospAM.nebrwesleyan.edu> on Wednesday January 12, 2005 @01:05PM (#11336626) Homepage
    ...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.
  • 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 hav
  • Memory Leaks (Score:5, Insightful)

    by captainclever ( 568610 ) <rj@audioscrobblCHEETAHer.com minus cat> on Wednesday January 12, 2005 @02:06PM (#11337568) Homepage
    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
    • Re:Memory Leaks (Score:4, Interesting)

      by Ark42 ( 522144 ) <slashdotNO@SPAMmorpheussoftware.net> on Wednesday January 12, 2005 @03:23PM (#11338600) Homepage
      I've never run into problems with memory leaks, but PHP is still not designed to really be a shell scripting language. The biggest issue I get is "PHP Fatal error: Unable to start session mm module in Unknown on line 0" from cron jobs. Apparently you can't start two instances of PHP at the exact same time, or you get this problem. The only fix I could find was to wrap each PHP script in a SH script as so:

      #!/bin/sh
      RET=1; while [ $RET != 0 ]; do
      /usr/local/bin/some_script.php
      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.
    • Most people would consider this a bug and report it to the developers.
    • [your quote]

      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.
  • by Morgon ( 27979 )
    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.

    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 .htaccess redirects), with a MySQL backend to handle all of the data until cron.daily takes care of it.

    You can find it at http://www.morgontech.com/abuse/ [morgontech.com]
  • the answer is... (Score:1, Insightful)

    by Anonymous Coward

    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

    • 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.

      Let me guess ... MySQL ?

  • by DrSkwid ( 118965 ) on Wednesday January 12, 2005 @03:27PM (#11338636) Journal

    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

    • Argh! That's the one thing that drives me mad using it - I'm continually checking php.net docs to check the order of parameters.

      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
  • I use PHP more than Perl but less than shell... I find that I can only remember and remain competent in a few languages at any one time so I necessarily limit myself to PHP and shell for almost everything.

    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

  • 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.

  • by gabe ( 6734 ) on Wednesday January 12, 2005 @04:46PM (#11339729) Homepage Journal
    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.
    • you@yourhost $ /usr/bin/php -q -a

      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)

    by aminorex ( 141494 ) on Wednesday January 12, 2005 @07:44PM (#11342139) Homepage Journal
    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).

    • So ... the folks over at ACM Queue [acmqueue.org] didn't have actual knowledge of a programming language?
      • Given that PHP has a niche, that means there are entire civilizations built upon PHP. The reasons are political and economic, not technical. Those reasons are very important. Much more important than technical ones, in most cases.
    • I hate perl on the web. It's annoying, it doesn't always play well and getting it to run on IIS is a bear. (I don't want to hear about with IIS, PHP works great in IIS and there is reasons for IIS) My personal opinion, Perl is exposed to the web way more often then it should be.
  • 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
  • I made a very simple page called 'cli.php' (as in 'command line interface'.) It's good for quick-n-easy (and non-intereactive) things when you can't ssh in. Not quite Webmin, but it's handy. Good for checking uptime, disk usage, etc. Sample commands:

    whoami, pwd, uptime, df -h, du -sh /home/<yourname>/public_html/

    <form action="cli.php" method="post">
    <input type=text name="command" size=20>
    <input type="submit" name="submit" value="Do me!">
    <?php
    print "<pre>" . `$command`

    • bad bad bad. A user on our ACM system had a bad file upload script. Forgot about it and someone uploaded a cli php script not much more complex then that. Used that to start a DOS attack on undernet and do other bad things about the system.
      • I suppose I should have mentioned there's no security at all in that script. Not like it's on the front page of my site--it's in a hidden directory behind https and an .htaccess file. If you can guess where my site is, and what the directory is, and my username and password, go nuts. :-)
  • All you need to do is configure sudo to allow apache to do certain things, then call it...

    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 /bin/bash $1

    adduser.php:

    Its really not that hard to do anything with php.
  • All you need to do is configure sudo to allow apache to do certain things, then call it...

    <?
    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 /bin/bash $1

    adduser.php:
    <?
    $user=$_GET['username'];
    $pass=$_GET['password'];
    $pass=crypt($pass); // heh, you need to make the password encrypted, this isn't the correct function but i'm
  • by till.k ( 103739 )
    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.

"What man has done, man can aspire to do." -- Jerry Pournelle, about space flight

Working...