Merge pull request #748 from terrafrost/ssh-custom-socket-2.0

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

* terrafrost/ssh-custom-socket-2.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:32 +02:00
commit 650a4ce266
2 changed files with 40 additions and 17 deletions

View File

@ -869,7 +869,9 @@ class SSH2
/**
* 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 $timeout
* @see \phpseclib\Net\SSH2::login()
@ -954,10 +956,17 @@ class SSH2
34 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST')
);
if (is_resource($host)) {
$this->fsock = $host;
return;
}
if (is_string($host)) {
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
}
}
/**
* Set Crypto Engine Mode
@ -989,13 +998,13 @@ class SSH2
$this->curTimeout = $this->timeout;
$host = $this->host . ':' . $this->port;
$this->last_packet = microtime(true);
if (!is_resource($this->fsock)) {
$start = microtime(true);
$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout);
if (!$this->fsock) {
$host = $this->host . ':' . $this->port;
user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
return false;
}
@ -1007,6 +1016,7 @@ class SSH2
$this->is_timeout = true;
return false;
}
}
/* According to the SSH2 specs,

View File

@ -87,4 +87,17 @@ class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase
$this->assertInternalType('string', $ssh->getServerPublicHostKey());
}
public function testOpenSocketConnect()
{
$fsock = fsockopen($this->getEnv('SSH_HOSTNAME'), 22);
$ssh = new 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.'
);
}
}