Shutting down your network via PHP

 
Post new topic   Reply to topic    PHP User Group Malta Forum Index // Links to PHP Good Tutorials
View previous topic :: View next topic  
Author Message
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 51
Location: Iklin

PostPosted: Tue Jan 22, 2008 12:33 pm    Post subject: Shutting down your network via PHP Reply with quote
Scenario
You have a Powerware (might work with other software) UPS that powers more than 1 PC but has limited Software, like Powerware's on Ubuntu for example.

This is where some SSH and PHP comes handy .. this is how I fixed it :



Somewhere on your server .. create a .htpasswd file :
mine is /home/yancho/.htpasswd by
Quote:
htpasswd -bc NAME username password



Somewhere else in your server .. create a PHP file .. find its directory and then in ur apache config do this :

Quote:

<Directory "/home/yancho/public_html/blabla/abc">
AuthType Basic
AuthName "Authentication Required"
AuthUserFile "/home/yancho/.htpasswd"
Require valid-user
Order Deny,Allow
Deny from all
Allow from 10.0.0.1
</Directory>


Change the bold .. the red is my IP .. do as u like .. more info here : http://httpd.apache.org/docs/1.3/mod/mod_access.html#allow

The PHP file i created is turnoff.php .. in it there is just these lines:
Code:

<?
echo "Shutting down the Ubuntu Server<br>";
$result = shell_exec('sudo /home/yancho/turnoff.sh');
echo $result;
?>


Ok now we create the turnoff.sh

Code:

#!/bin/bash
echo "Powering off the machine"
sudo shutdown -h now


Save and chmod +x turnoff.sh

Now we need to give the www-data which is the username Apache with PHP is running . .check urs by echoing from PHP script - using :
[php:1:76799d917d]
shell_exec('whoami');
[/php:1:76799d917d]

type sudo visudo
Quote:

www-data Ubuntu=(root) NOPASSWD:/home/yancho/turnoff.sh

My hostname : Ubuntu - find urs using hostname
root - i want it to run as root .. presuming u too .. else forgetting to shutdown

Now it is time to call the PHP file from the windows machine This might vary depending on your software

LanSafe > Configuration > Shutdown in 1 minute > Command > iexplore.exe [url]http://USERNAME_of_htpasswd:PASSWORD_of_htpasswd@10.0.0.2/~yancho/blabla/abc/turnoff.php[/url]


Testing it should see your machine powering off gracefully .. hope it works Smile

Hope it helps some of you keeping your PHP server up Smile

Cheers
_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Post new topic   Reply to topic    PHP User Group Malta Forum Index // Links to PHP Good Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

alexisRed v1.2 // Theme Created By: Andrew Charron and Web Hosting Bluebook // Icons in Part By: Travis Carden
Boards optimised using the phpBB-SEO mod found at phpbb-seo.com
Boards hosted courtesy of solutions-lab.net
Link Backs : PHPClasses.org - MT Page :: PHPUsergroups.org - MT Page



Powered by phpBB © 2001, 2002 phpBB Group