Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   ssh using php (http://www.go4expert.com/forums/ssh-using-php-t13707/)

ub007 11Sep2008 01:43

ssh using php
 
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

Quote:

SERVER(enter in the format 'www.example.com'): <input type="text"
name="server" /><br />
AGENT: <input type="text" name="threads" /><br />
INTERVAL: <input type="text" name="interval" /><br />
RAMPUP: <input type="text" name="rampup" /><br />
No of Client MAchines: <input type="text" name="machines" /><br />
testvector: <input type="text" name="test" /><br />
<input type="submit" value="GO" />
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?

asha 17Oct2008 10:40

Re: ssh using php
 
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.


All times are GMT +5.5. The time now is 08:03.