Results 1 to 16 of 16
  1. #1
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181

    Run shell commands with PHP ?

    What is the best way to run a command like ... adduser or deluser or service restart on a box from a remote webbox using php?
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  2. #2
    Join Date
    Jun 2000
    Location
    Washington, USA
    Posts
    5,990
    I typically use something similiar to sudo, and run the PHP scripts via CGI, so that they are running as a different user than Apache. That technique works fairly well.

  3. #3
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    John, can you expand more on what exactly you do? (I also don't see you on IRC)
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  4. #4
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    I am looking for something to

    1) Log into a remote box
    2) Add a user account
    3) Set its password
    4) Run a script
    5) Log out

    something along those lines.

    Can someone please details one of the following

    1) running commands 1 at a time to the target box via an SSH trust connection / RSA keys

    2) Backend daemon accepting commands
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  5. #5
    Join Date
    May 2001
    Location
    @ Work - Usually!
    Posts
    835
    Allow exec in PHP.INI and the chmod 6755 the script and chown the script to a root user.

    Technically that should work - I have not tested it - but "obviously" dont leave the script in an unprotected public folder.

  6. #6
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    Thats for exec on the same server .... the commands are coming from webbox and the commands are being executed on a remote box without APACHE ...

    please read my description before you post
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  7. #7
    Join Date
    Mar 2004
    Posts
    1,303
    Originally posted by The Broadband Man
    John, can you expand more on what exactly you do? (I also don't see you on IRC)
    I think he meant this:
    add #!/usr/bin/php to the begining of the script.

    PHP Code:
    <?
    #!/usr/bin/php
    // the rest is your php
    // ...
    ?>
    I use this sometime too.

  8. #8
    Join Date
    Jan 2005
    Location
    UK
    Posts
    94
    He wants to be able to SSH into a server from a webserver. I've been trying to do this, but I do not think it is possible with PHP.

  9. #9
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    He wants to be able to SSH into a server from a webserver. I've been trying to do this, but I do not think it is possible with PHP.
    PHP Code:
    <?php

         $key 
    'someencryptedkeytohost';

         
    // Establish a connection
         
    $conn ssh2_connect('foo.com');
         if (!
    $conn) { die("Cannot connect"); }

         
    // Verify using remote key
         
    if (ssh2_fingerprint($conn,SSH2_FINGERPRINT_MD5 SSH2_FINGERPRINT_HEX) != $key)
         {
             die(
    "Cannot verify server using key");
         }

         
    // -- OR --
         // Verify using username password

         
    if (!ssh2_auth_password($conn'username''password'))
         {
               die(
    "Cannot authenticate using username/password");
         }

         
    // Open a shell, and execute a command

         
    $shell=ssh2_shell($conn'xterm');
         
    fwrite$shell"ls -la\n"); 

    ?>

  10. #10
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    I've tried this, it doesn't really work all that well. Also, is RSA key the only way to do this?
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  11. #11
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    is ssh2_connect() is a php 5 function because I get

    Fatal error: Call to undefined function: ssh2_connect()


    1) Any php4 alternatives?
    2) Anyone know if modernbill and kayako work on php5?
    Last edited by The Broadband Man; 05-31-2005 at 08:06 AM.
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  12. #12
    Join Date
    Jan 2005
    Location
    UK
    Posts
    94
    Quote Originally Posted by php.net
    Development Versions: There are currently no stable versions of PECL/ssh2, to force installation of the beta version of PECL/ssh2 execute: pear install ssh2-beta
    That might be why you can't use it.

  13. #13
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    Also, is RSA key the only way to do this?
    Nope, check the documentation for ssh2_connect

  14. #14
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    Yeah I just saw - however ssh2_connect is a php5 function ... any php4 alternatives?
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

  15. #15
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    Its a PECL extension. Try http://pecl.php.net/package/ssh2 for more information.

  16. #16
    Join Date
    Jul 2004
    Location
    New York, NY
    Posts
    2,181
    Ooo I see - thank you kindly sir

    i downloaded it but it won't install ... hrm - im lost
    ServGrid - www.servgrid.com - Affordable and Reliable SSD Cloud Solutions
    Premium 10G Network, 2(N+1) Powerplant and SSD Performance
    Web, Reseller, KVM VPS, Storage and Private Cloud Hosting
    Click here to see our SSD Benchmarks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •