Skip to content

Commit

Permalink
process() and added key_pass support.
Browse files Browse the repository at this point in the history
remote_shell uses its own loop to handle shell output rather than
relying on read()
  • Loading branch information
tamagokun committed Jan 17, 2014
1 parent 2847b80 commit a3f904a
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/Pomander/RemoteShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function run($cmd)
$this->shell->setTimeout(0);
$this->shell->exec($cmd);

$output = $this->handle_data();
$output = $this->process();
$status = $this->shell->getExitStatus();
if($status === false) $status = -1;

Expand Down Expand Up @@ -52,18 +52,45 @@ protected function connect()
abort("ssh", "Login failed.");
}

protected function handle_data($output = '')
protected function process()
{
$output = "";
$offset = 0;
$this->shell->_initShell();
while( true ) {
$temp = $this->shell->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
switch( true ) {
case $temp === true:
return $output;
case $temp === false:
return false;
default:
$output .= $temp;
if( $this->handle_data(substr($output, $offset)) ) {
$offset = strlen($output);
}
}
}
}

protected function handle_data($output)
{
// hosts authenticity
$regex = '/(The authenticity of host .* \(yes\/no\))/s';
$res = $this->shell->read($regex, NET_SSH2_READ_REGEX);

if (preg_match($regex, $res) > 0) {
$output .= $res;
if (preg_match($regex, $output) > 0) {
$this->write("yes");
return true;
}

// key password
$regex = '/Enter passphrase for key .*/';

return $this->handle_data($output);
if( preg_match($regex, $output) > 0 ) {
$this->write($this->key_pass);
return true;
}

return $output.$res;
return false;
}
}

0 comments on commit a3f904a

Please sign in to comment.