The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 22, 2019, 11:45:27 PM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Is there a PHP version of.....  (Read 5701 times)
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« on: July 12, 2007, 02:55:25 PM »

The Perl module that I love called IPC::PerlSSH

Essentially it allows you to run PERL on remote machines via SSH, so do any of you know of a class that will allow me to run CLI PHP on remote machines via SSH ?

http://search.cpan.org/~pevans/IPC-PerlSSH-0.06/lib/IPC/PerlSSH.pm

Thanks

Jason
Logged
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #1 on: July 12, 2007, 06:52:53 PM »

Hey Jason,

Couldn't you just script ssh itself?

ssh root@target /path/of/script.php

Or am I missing something?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #2 on: July 13, 2007, 04:28:10 AM »

yes but that's not the aim that I want.

For management  reasons it is MUCH simpler to have one control server that contains all the code and then simply contain all the remote servers' (workers) logon details etc and ssh in to run the code remotely without there ever being any actual code permantly stored on those servers.

It is a virtual way to deliver a crappy cluster style system with the only requirement being SSH access.

Personally I love it Smiley
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: July 13, 2007, 08:17:28 AM »

JD -

I have a similar rig, but went with a pull rather than push setup ie., when it's time to update, the master server pings a little page on the drones, which cause them to "call home" for their instructions. I lock it down so that they only hear a call home instruction from me, and of course, where they call home to is coded and fixed so that someone can't get into the middle of it. In one case, the drones call home under HTTPS because I'm paranoid.

The net result of this is that I can ping lots of drones at once really quickly and update very quickly, or I can do it on a schedule nice and slowly if it is non-critical. Pushing multithreaded is just a bit more cumbersome than simply pinging a bunch then handling HTTP requests in Apache for the code. Oh yeah, for got to say that the code for the drones is available via special apache calls (to drones that are in my DB only) so that the distribution of the code is about as processor friendly as possible.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
skyts
Rookie
**
Offline Offline

Posts: 22


View Profile
« Reply #4 on: July 13, 2007, 10:30:19 AM »

Wow that sounds very complicated to do. Reminds me of a "botnet".

Logged
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #5 on: July 13, 2007, 01:52:58 PM »

Yup Botnet would be a very good explanation of how it works Smiley

I get ya Ed but I really want the push rather than pull thing with nothing more SSH L/P so that the task that is run is solely in memory with never a file on the account permanently stored.

I guess I will have to think a little more about this and may have to delve into PHP myself and code one, or I could just stick to Perl via an eval locally to call PHP remotely. Ugly but it'll work I guess
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: July 13, 2007, 01:55:28 PM »

OK, a little confused.

If you want to code in PHP on a local machine and talk SSH via PHP to another box, you need this:
http://us2.php.net/manual/en/ref.ssh2.php

If you want to run PHP code on a remote box via SSH then you simply need to put
#! /usr/local/php [the script name]
at the top of the script (above the <?) and then chmod it to 755 and execute it.

If you want to code in PHP on a local machine to talk via SSH to another box and then run PHP code on it, well, a combo up here would work. Am I just confused?
« Last Edit: July 13, 2007, 01:58:06 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #7 on: July 13, 2007, 02:17:48 PM »

Quote
If you want to code in PHP on a local machine to talk via SSH to another box and then run PHP code on it, well, a combo up here would work.

You have it right mate.

I want to code in PHP on machine one, that speaks SSH to machine two, then runs equivalent to.... 
Code:
$ perl -e 'print "Hello World\n"'


Once I have that (which is roughly what the above Perl module gives) I would then want to wrap it all in a function so it can simply be something like
Code:
sshserver(foo@bar.com,username,password,'echo "hello world";)


Phew..... I hope that makes sense
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: July 13, 2007, 02:27:08 PM »

Right!

OK, here's essentially how the PHP SSH2 stuff looks:

Code:
<?php
$handle 
ssh2_connect('www.mydomain.com'22);
ssh2_auth_password($handle'jasond''mypassword');
$stream ssh2_exec($connection'/usr/local/bin/php -i');
?>


The return result is a stream as opposed to a simple string. The example above would echo back to you the phpInfo() page for <that> machine. Note that the example does not actually run a script in this case, it simply makes use of an interpreter feature. If you did this:

$stream = ssh2_exec($connection, '/www/sites/myphp/ascript.php);

AND ascript.php was chmod as 755 or some such AND you had #! /usr/local/bin/php at the front of it (which interpreter to use ... you know how that works) then the script would execute as you have outlined.

Once all that is done, my recommendation would be to wrap what this stuff into a class rather than functions, so that you could say something like this:

Code:
$ssh = new sshHandler();
if (!$ssh->connect($userName, $password)) { die('failed') }

echo $ssh->execute("ps aux");
$ssh ->disconnect();

I think that'd be rather slick actually. In fact, I might just do that myself goddammit! Wink

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #9 on: July 13, 2007, 02:36:08 PM »

or.......

http://devzone.zend.com/node/view/id/1712
Logged
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #10 on: July 13, 2007, 02:39:21 PM »

Ed. I get you completely but it required files to physically live on the remote server to run.

Is there an equivalent to the -e switch in Perl, for PHP ?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: July 13, 2007, 02:54:24 PM »

It's pretty close... try this:

php -r 'echo "hello world\n\n";'

... I think that's what you're looking for

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #12 on: July 13, 2007, 02:55:09 PM »

Hmmm... now that I look at that and our postings I am considerably more curious about what you're up to my friend...  Mobster

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #13 on: July 13, 2007, 03:21:46 PM »

the -r switch is EXACTLY what I needed to know.

Thank you my friend Smiley

As to what I am doing it is like many things in life, can (and probably will be) used for good and evil

On a simple level imagine it as pulling in extra CPU and or RAM resources as requiredfrom within the DB of available machines. A remote worker DB in essence.

The reason I want this in PHP, away from my comfort zone of Perl is a specific piece of code (in PHP) that has the potential of falling over due to CPU load. As it is written in PHP it made me think more about my old way of working and porting it to PHP.

When this is combined with SSHFS creating a meshed and global FS you have a lovely, centrally managed system for whatever you may want.

My moral. SSH is more MUCH MORE than an encrypted terminal
Logged
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #14 on: July 13, 2007, 05:17:19 PM »

Ahhhh just had an idea.

One piece of code on each of the remote servers is OK.
 if the server hasn't been accessed before we can send over a standard script, along the lines of...

Code:
#!/usr/bin/php -q
<?
eval $argv[0];
?>

and of course the name of the script, setting it up etc can all be taken place within your class you spoke of before.

So as well as -r this whould work as well Smiley
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!