1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to execute a sudo command with php script

Discussion in 'PHP' started by snshusat161, Apr 26, 2013.

  1. #1
    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

    [​IMG]

    and if it's not running then the output should be
    [​IMG]

    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!
    snshusat161, Apr 26, 2013 IP
  2. jestep

    jestep Notable Member Premium Member

    Messages:
    3,486
    Likes Received:
    199
    Best Answers:
    16
    Trophy Points:
    280
    #2
    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.
    jestep, Apr 26, 2013 IP
  3. ThePHPMaster

    ThePHPMaster Active Member

    Messages:
    596
    Likes Received:
    30
    Best Answers:
    18
    Trophy Points:
    75
    #3
    If you have root password, create a file (ex: /var/to/password) with the root password (or any sudoer user for that matter):

    Code (Text):
    1.  
    2. exec('sudo -u root -S service fcserver start < ~/var/to/password');
    3. exec('sudo -u root -S service fcserver status < ~/var/to/password');
    4.  
    Source
    ThePHPMaster, Apr 26, 2013 IP
  4. snshusat161

    snshusat161 Member

    Messages:
    122
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    what should be the extension to this file password?
    snshusat161, Apr 26, 2013 IP
  5. codespiderindia

    codespiderindia Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    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:
    1. <?php
    2. $outPut = shell_exec("ls -al");
    3. echo "<pre>$outPut</pre>";
    4. ?>
    5.  
    codespiderindia, Apr 26, 2013 IP
  6. codespiderindia

    codespiderindia Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #6
    for sudo command here is the code below -
    PHP:
    1. <?php
    2. $outPut = shell_exec("echo password_for_the_user | sudo -S command_to_execute");
    3. echo "<pre>$outPut</pre>";
    4. ?>
    codespiderindia, Apr 26, 2013 IP
  7. snshusat161

    snshusat161 Member

    Messages:
    122
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    none of your advice is working guys. je step advice is little sensible at least. :) but the other two replies are just not working.
    snshusat161, Apr 27, 2013 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,119
    Likes Received:
    331
    Best Answers:
    17
    Trophy Points:
    325
    #8
    nico_swd, Apr 27, 2013 IP
  9. smashable

    smashable Peon

    Messages:
    1
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #9
    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,
    smashable, Apr 30, 2013 IP
    nico_swd likes this.
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,119
    Likes Received:
    331
    Best Answers:
    17
    Trophy Points:
    325
    #10
    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).

    PHP:
    1.  
    2. $key = new Crypt_RSA();
    3. // $key->setPassword($password); // Only if your key has a passphrase
    4. $data = file_get_contents('/path/to/rsa_id');
    5. $key->loadKey($data);
    6.  
    7. $ssh = new Net_SSH2('127.0.0.1', 22, 10);
    8.  
    9. if (!$ssh->login('root', $key))
    10. {
    11.     throw new Exception('Incorrect private key.');
    12. }
    13.  
    14. // Execute anything as root... be careful as hell with this!
    15. $response = $ssh->exec('service fcserver status');
    16.  
    nico_swd, May 1, 2013 IP