note 92314 deleted from function.ssh2-shell by danbrown
| From: | danbrown@php.net | Date: | Sun, 19 Jul 2009 13:51:17 +0000 |
| Subject: | note 92314 deleted from function.ssh2-shell by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158476@lists.php.net to get a copy of this message | ||
Note Submitter: ADZ adz at jewc dot org
----
Note: As you probably can see...this is a really really long tutorial. Not because it's a hard
concept. But because I want to explain it in such detail that ANYBODY can get it. If you have a
little bit of experience in php already, it's fine too. Look through the source code I showed
below and if you don't understand a line, just look for it in my tutorial. I marked off 3 very
important concepts with a bunch of *********s.
Hope this helps a buncha people. It's my first time doing such an extensive tutorial. So gimmie
your input :)
I've had quite a bit of trouble getting ssh2 to work, mainly because I didn't understand
the concepts behind a lot of the commands used. That's why when I tried the scripts given by
different users below, the didn't work. I did a bit of research on each of the functions users
used...and I've made an (almost) failsafe script. And most importantly I will explain what
exactly is happening in the code, unlike a lot of users here, explain what every step does.
Here's the code. The function you will use is readwrite. What it does is it connects through
ssh using username/password authentication, sends a command and looks for a certain output. If it
finds that output it returns true. Obviously you will want to do something different (ex: get ALL
the output of a command, run multiple commands, etc). That's why I will explain exactly what is
happening in every line. Trust me, if you don't understand a part of the script READ THE
EXPLANATION!!!, you don't want to take any shortcuts here...otherwise you will get very
unexpected results if your situation is a wee bit different from mine......
=============== FULL SCRIPT =============
<?php
function readwrite ($write, $lookfor,$ip,$user,$pass) {
flush(); //Write Whatever we have before
//Connect and Authenticate
$connection = ssh2_connect($ip) or die ("can't connect");
ssh2_auth_password($connection,$user,$pass) or die("can't auth");
//Start the Shell
$shell = ssh2_shell($connection,"xterm");
//Here we are waiting for Shell to initialize
usleep(200000); //increase this a bit if you get unexpected results
$write = "echo '[start]'; {$write}; echo '[end]'";
$out = user_exec($shell, $write);
fclose($shell);
if(stristr($out, $lookfor)) { //it exists
return true;
}
}
function user_exec($shell,$cmd) {
fwrite($shell,$cmd . PHP_EOL); //write to the shell
$output = ""; //will store our output
$start = false; //have we started yet
$start_time = time(); //the time sarted
$max_time = 10; //time in seconds
while(((time()-$start_time) < $max_time)) { //if the x seconds haven't passed
$line = fgets($shell); //get the next line of output
if(!stristr($line,$cmd)) { //we don't want output that was out command (because it also
contains [start] and [end]
if(preg_match('/\[start\]/',$line)) { //if we see that [start] is in the line that
means we started
$start = true; //set start to true
}
elseif(preg_match('/\[end\]/',$line)) { //we're done
return $output; //return what we have (last line)
}
elseif($start && isset($line) && $line != "")
{
$output = $line; //return only last line (.= for all lines)
}
}
}
}
?>
============ EXPLANATION =============
I couldn't fit it into the comment so I placed it into wikibooks.
http://en.wikibooks.org/wiki/PHP_Programming/SSH_Class