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 Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #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 Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    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
     
    ThePHPMaster, Apr 26, 2013 IP
  4. snshusat161

    snshusat161 Active Member

    Messages:
    122
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #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
    $outPut = shell_exec("ls -al");
    echo "<pre>$outPut</pre>";
    ?>
    
    PHP:
     
    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
    $outPut = shell_exec("echo password_for_the_user | sudo -S command_to_execute");
    echo "<pre>$outPut</pre>";
    ?>
    PHP:
     
    codespiderindia, Apr 26, 2013 IP
  7. snshusat161

    snshusat161 Active Member

    Messages:
    122
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #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,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #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,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #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).

    
    $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:
     
    nico_swd, May 1, 2013 IP