Merge pull request #746 from terrafrost/ssh-custom-socket-1.0

SSH2: make it so you can connect using open sockets / 1.0 branch

* terrafrost/ssh-custom-socket-1.0:
  SSH2: move where $host is defined
  SSH2: make it so you can connect using open sockets
This commit is contained in:
Andreas Fischer 2015-07-21 12:53:24 +02:00
commit 06ec04e3fe
2 changed files with 40 additions and 17 deletions

View File

@ -874,7 +874,9 @@ class Net_SSH2
/** /**
* Default Constructor. * Default Constructor.
* *
* @param String $host * $host can either be a string, representing the host, or a stream resource.
*
* @param Mixed $host
* @param optional Integer $port * @param optional Integer $port
* @param optional Integer $timeout * @param optional Integer $timeout
* @see Net_SSH2::login() * @see Net_SSH2::login()
@ -978,9 +980,16 @@ class Net_SSH2
34 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST') 34 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST')
); );
$this->host = $host; if (is_resource($host)) {
$this->port = $port; $this->fsock = $host;
$this->timeout = $timeout; return;
}
if (is_string($host)) {
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
}
} }
/** /**
@ -1013,23 +1022,24 @@ class Net_SSH2
$this->curTimeout = $this->timeout; $this->curTimeout = $this->timeout;
$host = $this->host . ':' . $this->port;
$this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5 $this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 if (!is_resource($this->fsock)) {
$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout); $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
if (!$this->fsock) { $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout);
user_error(rtrim("Cannot connect to $host. Error $errno. $errstr")); if (!$this->fsock) {
return false; $host = $this->host . ':' . $this->port;
} user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
$elapsed = strtok(microtime(), ' ') + strtok('') - $start; return false;
}
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
$this->curTimeout-= $elapsed; $this->curTimeout-= $elapsed;
if ($this->curTimeout <= 0) { if ($this->curTimeout <= 0) {
$this->is_timeout = true; $this->is_timeout = true;
return false; return false;
}
} }
/* According to the SSH2 specs, /* According to the SSH2 specs,

View File

@ -85,4 +85,17 @@ class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase
$this->assertInternalType('string', $ssh->getServerPublicHostKey()); $this->assertInternalType('string', $ssh->getServerPublicHostKey());
} }
public function testOpenSocketConnect()
{
$fsock = fsockopen($this->getEnv('SSH_HOSTNAME'), 22);
$ssh = new Net_SSH2($fsock);
$username = $this->getEnv('SSH_USERNAME');
$password = $this->getEnv('SSH_PASSWORD');
$this->assertTrue(
$ssh->login($username, $password),
'SSH2 login using an open socket failed.'
);
}
} }