ssh using php

Discussion in 'PHP' started by ub007, Sep 10, 2008.

  1. ub007

    ub007 New Member

    Joined:
    Sep 10, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have setup an intranet of 10 machines(all running Ubuntu).One of them is the server hosting lets say Example Web Page. I use the other 9 machines to load test the server.Also intend to set up one PC as the master.So I have a master with 8 slaves testing www.example.com.The test scripts are installed on all the 9 machines & I have a browser form interface on my master node....

    The form looks like

    The GO button calls another php script which ssh s into the slave nodes and run the test scripts

    So i could do a test like ->
    load server(Example Web Page) using 3 client machines; stressing vector 'webserver' (//one of the many testing programs installed on the client machines)

    A sample php code would look (this is only for 3 machines)

    Code:
    $machineValues = array("192.168.11.30", "root", "password","david", "
    192.168.11.19", "root", "password2","david2", "192.168.11.32", "root2",
    "password3","david3");
    if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't
    exist");
    // log in at server1.example.com on port 22
    
    for ( $counter = 0; $counter < sizeof($machineValues); $counter =
    $counter+4) {
    if($number_of_machines<$number){
    
    if(!($con = ssh2_connect($machineValues[$counter], 22))){
    echo "fail: unable to establish connection\n";
    echo "Machine number $counter is $machineValues[$counter]";
    } else {
    // try to authenticate with username root, password secretpassword
        if(!ssh2_auth_password($con, $machineValues[$counter+1],
    $machineValues[$counter+2])) {
          echo "fail: unable to authenticate\n";
        } else {
        // allright, we're in!
           echo "okay: logged in...\n";
           // execute a command
           $stream = ssh2_exec($con, 'python/home/'.$machineValues[$counter+3].'/Desktop/hello.py');
           stream_set_blocking($stream, true);
           while($line = fgets($stream)) {
           flush();
           echo $line."<br />";
                    }
            }
        }
    }
    $number_of_machines++;
    }
    
    ?>
    Now,lets say I select from the form

    No of Client MAchines : 1

    The code ssh into 192.168.11.30 and runs the script.

    However i still got 'ip' => "192.168.11.19" and 'ip' => "192.168.11.32" available and I may wish to use that after a certain time delay...

    So i wish i could create some code that would enable the browser interface to give a message something like
    "you got 2 more machines available!" Would you like to add more?:thumbs:

    I would add one more field in the form
    More Client MAchines: <input type="text" name="machines" /><br />

    Eg.User selects 1, when they click submit,the PHP code should ssh into the next machine available which is "192.168.11.19"....and then report back,"you got 1 more machine available!" and so on.......

    How would i do that?
     
  2. asha

    asha New Member

    Joined:
    Nov 9, 2006
    Messages:
    44
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Homemaker
    Location:
    Kolkata
    Home Page:
    http://whatanindianrecipe.com
    I don't know how much possible it is with PHP, but in Perl there are lot of modules at http://cpan.org to help you out.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice