Hello all, I am trying to write a php program which can execute service fcserver status to find whether my chat room is running or not. If it is running then output should be and if it's not running then the output should be If it is not running then there should be an start button to start the chat room. and when somebody clicks on that button, my script executes service fcserver start command. well, i can't execute commands like "service fcserver status" or "service fcserver start" as www-data user. I need a root permission. please help me!! what are the ways to get this little task done without compromising with server security? Thank you!
Get someone to write a perl or bash script that can start and stop the chat service. You can then use exec to call the script. I would not want to start and stop directly from php if it requires root or an escalated user privilege to access it.
If you have root password, create a file (ex: /var/to/password) with the root password (or any sudoer user for that matter): exec('sudo -u root -S service fcserver start < ~/var/to/password'); exec('sudo -u root -S service fcserver status < ~/var/to/password'); Code (markup): Source
Normally we can execute the shell commands through PHP. PHP have the inbuilt functions that can help us to execute the shell commands. The basic inbuilt functions are shell_exec('command_to_execute') and exec('command_to_execute'). Following example shows how we can list all the items in a specific directory in the server. <?php $outPut = shell_exec("ls -al"); echo "<pre>$outPut</pre>"; ?> PHP:
for sudo command here is the code below - <?php $outPut = shell_exec("echo password_for_the_user | sudo -S command_to_execute"); echo "<pre>$outPut</pre>"; ?> PHP:
none of your advice is working guys. je step advice is little sensible at least. but the other two replies are just not working.
I think phpseclib, a pure PHP SSH implementation, would be better. In fact their websites has a pretty good discussion of how to do sudo over SSH: http://phpseclib.sourceforge.net/ssh/examples.html#password,sudo,
Yep, phpseclib is pretty nice. I've been using it for some time as well. Although the example is still a little complicated. I use a key to connect to the server (passwords should be disabled entirely, in my opinion, for security reasons). $key = new Crypt_RSA(); // $key->setPassword($password); // Only if your key has a passphrase $data = file_get_contents('/path/to/rsa_id'); $key->loadKey($data); $ssh = new Net_SSH2('127.0.0.1', 22, 10); if (!$ssh->login('root', $key)) { throw new Exception('Incorrect private key.'); } // Execute anything as root... be careful as hell with this! $response = $ssh->exec('service fcserver status'); PHP: