本文整理汇总了PHP中ssh2_connect函数的典型用法代码示例。如果您正苦于以下问题:PHP ssh2_connect函数的具体用法?PHP ssh2_connect怎么用?PHP ssh2_connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ssh2_connect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sshiconn
function sshiconn($cmd, $pass, $ip, $sshp = 22)
{
$ip = $_REQUEST['ip'];
$pass = $_REQUEST['pass'];
$sshp = $_REQUEST['sshp'];
if (!isset($_REQUEST['sshp'])) {
$sshp = '22';
}
$connection = ssh2_connect($ip, $sshp);
if (!$connection) {
throw new Exception("fail: unable to establish connection\nPlease IP or if server is on and connected");
}
$pass_success = ssh2_auth_password($connection, 'root', $pass);
if (!$pass_success) {
throw new Exception("fail: unable to establish connection\nPlease Check your password");
}
$stream = ssh2_exec($connection, $cmd);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
print_r($cmd);
$output = stream_get_contents($stream);
fclose($stream);
fclose($errorStream);
ssh2_exec($connection, 'exit');
unset($connection);
return $output;
}
示例2: Authenticate
public function Authenticate($uid, $pass)
{
$this->AuthResult = false;
// Connect
$con = ssh2_connect($this->RemoteHost, $this->RemotePort);
if ($con === false) {
return ulLoginBackend::ERROR;
}
// Check fingerprint
if ($this->RemoteFingerprint != '') {
if (ssh2_fingerprint($con, SSH2_FINGERPRINT_SHA1 | SSH2_FINGERPRINT_HEX) != $this->RemoteFingerprint) {
return ulLoginBackend::ERROR;
}
}
// Test if server supports password-based authentication
$auth_methods = ssh2_auth_none($con, 'user');
if (!in_array('password', $auth_methods)) {
return ulLoginBackend::ERROR;
}
// Connect again, because we can only try to authenticate once on a connection
$con = ssh2_connect($this->RemoteHost, $this->RemotePort);
if ($con === false) {
return ulLoginBackend::ERROR;
}
// Try to authenticate
if (ssh2_auth_password($con, $uid, $pass)) {
$this->AuthResult = $uid;
return true;
} else {
return ulLoginBackend::BAD_CREDENTIALS;
}
}
示例3: handle
public function handle()
{
/**
* Estamblish SSH connection,
* put site under maintenance,
* pull chamges from git,
* install composer dependencies,
* execute migrations,
* put site online
*/
$remote = config('pckg.framework.' . DeployProject::class . '.remotes.default');
$path = $remote['root'];
$commands = ['cd ' . $path => 'Changing root directory', 'php ' . $path . 'console project:down' => 'Putting project offline', 'php ' . $path . 'console project:pull' => 'Executing project:pull', 'php ' . $path . 'console migrator:install' => 'Installing migrations', 'php ' . $path . 'console project:up' => 'Putting project up', 'php ' . $path . 'console cache:clear' => 'Clearing cache'];
$this->output('Estamblishing SSH connection.');
$sshConnection = ssh2_connect($remote['host'], $remote['port']);
$this->output('SSH connection estamblished.');
/**
* Authenticate.
*/
if (!ssh2_auth_password($sshConnection, $remote['username'], $remote['password'])) {
throw new Exception('Cannot estamblish SSH connection to remote');
}
foreach ($commands as $command => $notice) {
$this->output($notice);
$stream = ssh2_exec($sshConnection, $command);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
$errorStreamContent = stream_get_contents($errorStream);
$streamContent = stream_get_contents($stream);
$this->output($errorStreamContent . "\n" . $streamContent);
}
$this->output('Done!');
}
示例4: _connectandexecute
function _connectandexecute($hostname, $port, $fingerprint, $user, $pass, $command)
{
// connect via ssh2
$ssh = ssh2_connect($hostname, $port);
if (!$ssh) {
die("connection failed!");
}
$theirfingerprint = ssh2_fingerprint($ssh, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
if (strtoupper($theirfingerprint) != strtoupper($fingerprint)) {
die("fingerprint mismatch!");
}
if (!ssh2_auth_password($ssh, $user, $pass)) {
die("authentication failed!");
}
// shell, as Brocade really doesn't seem to like exec
if (!($sock = ssh2_shell($ssh, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))) {
die("failed to establish shell!\n");
}
fwrite($sock, "terminal length 0" . PHP_EOL);
fwrite($sock, $command . PHP_EOL);
sleep(1);
// seems to be a magic trick...
stream_set_blocking($sock, true);
$data = "";
while ($buf = fread($sock, 4096)) {
flush();
if (preg_match('/SSH@.+#$/', $buf)) {
break;
}
$data .= $buf;
}
fclose($sock);
return $data;
}
示例5: connect
/**
* Connect to host
*
* Connects to the host. Throws exception if the host is unable to be connected to. Will automatically
* verify the host fingerprint, if one was provided, and throw an exception if the fingerprint is not
* verified.
*
*/
public function connect()
{
//Attempt to connect to host
$link = ssh2_connect($this->_config['host'], $this->_config['port']);
//If host connection fails, throw exception
if (!$link) {
throw new Kohana_Exception('Unable to connect to :host on port :port', array(':host' => $host, ':port' => $port));
} else {
//Assign the connection link to the class property
$this->_conn_link = $link;
//If host fingerprint is not NULL, attempt to verify fingerprint
if (!is_null($this->_config['host_fingerprint'])) {
$verify = $this->verify_host_fingerprint();
//If the fingerprint is not verified, throw exception
if (!$verify) {
throw new Kohana_Exception('Unable to verify host fingerprint');
}
}
}
//Attempt to login user
if ($this->_config['authentication_method'] == 'KEY') {
$this->_connected = $this->login_key();
} else {
$this->_connected = $this->login_password();
}
$this->_connected && ($this->_sftp = ssh2_sftp($link));
}
示例6: get_list
function get_list($myhost, $usern, $passw, $mypath, &$data)
{
if (!function_exists("ssh2_connect")) {
die("function ssh2_connect doesn't exist");
}
if (!($conn = ssh2_connect($myhost, 22))) {
echo "fail: unable to establish connection\n";
} else {
if (!ssh2_auth_password($conn, $usern, $passw)) {
echo "fail: unable to authenticate\n";
} else {
if (!($stream = ssh2_exec($conn, "ls -1 " . $mypath))) {
echo "fail: unable to execute command\n";
} else {
stream_set_blocking($stream, true);
// allow command to finish
$data = "";
while ($buf = fread($stream, 4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
ssh2_exec($conn, 'exit');
}
示例7: setUp
public function setUp()
{
/*
* Create source connection
*/
$sourceSsh = ssh2_connect(getenv("SOURCE_SSH_HOST"), getenv("SOURCE_SSH_PORT"), array('hostkey' => 'ssh-rsa'));
if (!is_resource($sourceSsh)) {
$this->markTestSkipped("Could not connect to source.");
}
if (!@ssh2_auth_agent($sourceSsh, getenv("SOURCE_SSH_USER"))) {
$this->markTestSkipped("Couldn't authenticate on source. You might need to: eval `ssh-agent -s` && ssh-add");
}
$this->sourceSsh = $sourceSsh;
/*
* Create destination connection
*/
$destSsh = ssh2_connect(getenv("DEST_SSH_HOST"), getenv("DEST_SSH_PORT"), array('hostkey' => 'ssh-rsa'));
if (!is_resource($destSsh)) {
$this->markTestSkipped("Could not connect to destination.");
}
if (!@ssh2_auth_agent($destSsh, getenv("DEST_SSH_USER"))) {
$this->markTestSkipped("Couldn't authenticate on destination. You might need to: eval `ssh-agent -s` && ssh-add");
}
$this->destSsh = $destSsh;
}
示例8: __construct
function __construct($cfg)
{
// set default options if missing
static $defaults = array('port' => 22, 'user' => 'root');
if (is_object($cfg)) {
$cfg = json_decode(json_encode($cfg), 1);
}
foreach ($defaults as $k => $v) {
if (!isset($cfg[$k])) {
$cfg[$k] = $v;
}
}
// connect ssh2
$this->ssh2 = ssh2_connect($cfg['host'], $cfg['port']);
if (!$this->ssh2) {
throw new \Exception("can't connect trough ssh2\n");
}
// authorize
if (isset($cfg['key'])) {
// private/public key authentication requested
if (!ssh2_auth_pubkey_file($this->ssh2, $cfg['user'], $cfg['key']['pub'], $cfg['key']['pvt'], isset($cfg['key']['pass']) ? $cfg['key']['pass'] : NULL)) {
throw new \Exception("can't authorize via key");
}
} elseif (isset($cfg['pass'])) {
// username & password authentication
if (!ssh2_auth_password($this->ssh2, $cfg['user'], $cfg['pass'])) {
throw new \Exception("can't authorize via user & pass");
}
} else {
throw new \Exception("not enough authentication information provided");
}
$this->sftp = ssh2_sftp($this->ssh2);
}
示例9: ssh
public function ssh()
{
if (!function_exists("ssh2_connect")) {
die("function ssh2_connect doesn't exist");
}
// log in at server1.example.com on port 22
if (!($con = ssh2_connect("nao.local", 22))) {
echo "fail: unable to establish connection\n";
} else {
//try to authenticate with username root, password secretpassword
if (!ssh2_auth_password($con, "nao", "nao")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, "ls -al"))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream, 4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
}
示例10: connect
public function connect()
{
$methods = array();
if ($this->use_pubkey_file_) {
$methods['hostkey'] = 'ssh-rsa';
}
$conn = ssh2_connect($this->conf_['host'], $this->conf_['port'], $methods);
if (false === $conn) {
$this->setErr_(-1, sprintf('failed to connect [%s:%d]', $this->conf_['host'], $this->conf_['port']));
return false;
}
if ($this->use_pubkey_file_) {
$rc = ssh2_auth_pubkey_file($conn, $this->conf_['user'], $this->conf_['pubkey_file'], $this->conf_['privkey_file'], isset($this->conf_['passphrase']) ? $this->conf_['passphrase'] : NULL);
if (false === $rc) {
$this->setErr_(-1, sprintf('failed to auth with[%s:%s,%s,%s]', $this->conf_['user'], $this->conf_['pubkey_file'], $this->conf_['privkey_file'], $this->conf_['passphrase']));
return false;
}
} else {
$rc = ssh2_auth_password($conn, $this->conf_['user'], $this->conf_['passwd']);
if (false === $rc) {
$this->setErr_(-1, sprintf('failed to auth with[%s:%s]', $this->conf_['user'], $this->conf_['passwd']));
return false;
}
}
$this->conn_ = $conn;
return true;
}
示例11: connect
/**
* Connect operation
*/
public function connect()
{
if ($this->_ssh2 != null) {
// Already connected
return;
}
// Connect to server
$host = isset($this->_config['hostname']) ? $this->_config['hostname'] : 'localhost';
$port = isset($this->_config['port']) ? $this->_config['port'] : 22;
$username = isset($this->_config['username']) ? $this->_config['username'] : '';
$password = isset($this->_config['password']) ? $this->_config['password'] : null;
$this->_ssh2 = ssh2_connect($host, $port);
if ($this->_ssh2 === FALSE) {
throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_open_connection'), array(':host' => $host, 'port' => $port));
}
// Check fingerprint if it is specified
if (isset($this->_config['fingerprint'])) {
if (strtolower(ssh2_fingerprint($this->_ssh2)) != strtolower($this->_config['fingerprint'])) {
throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_fingerprint_validation'), array(':key' => ssh2_fingerprint($this->_ssh2)));
}
}
// Connect with certificate if it is specified
if (isset($this->_config['pubkeyfile']) and isset($this->_config['privkeyfile'])) {
if (!@ssh2_auth_pubkey_file($this->_ssh2, $username, $this->_config['pubkeyfile'], $this->_config['privkeyfile'], $password)) {
throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_authentication'));
}
} else {
if (!@ssh2_auth_password($this->_ssh2, $username, $password)) {
throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_authentication'));
}
}
// Enable SFTP mode
$this->_sftp = ssh2_sftp($this->_ssh2);
}
示例12: ssh2_connect
/**
*
*/
protected function ssh2_connect()
{
if (!($this->connection = ssh2_connect($this->host, $this->port, $this->methods, $this->callbacks))) {
throw new \RuntimeException('Could not connect to SftpProtocol remote: ' . $this->host . ':' . $this->port);
}
return $this;
}
示例13: getSFTPList
function getSFTPList()
{
$ftp_info = Context::getRequestVars();
if (!$ftp_info->ftp_host) {
$ftp_info->ftp_host = "127.0.0.1";
}
$connection = ssh2_connect($ftp_info->ftp_host, $ftp_info->ftp_port);
if (!ssh2_auth_password($connection, $ftp_info->ftp_user, $ftp_info->ftp_password)) {
return new Object(-1, 'msg_ftp_invalid_auth_info');
}
$sftp = ssh2_sftp($connection);
$curpwd = "ssh2.sftp://{$sftp}" . $this->pwd;
$dh = @opendir($curpwd);
if (!$dh) {
return new Object(-1, 'msg_ftp_invalid_path');
}
$list = array();
while (($file = readdir($dh)) !== false) {
if (!is_dir($curpwd . $file)) {
continue;
}
$list[] = $file . "/";
}
closedir($dh);
$this->add('list', $list);
}
示例14: connect
function connect($host, $port = 22)
{
$this->connection = @ssh2_connect($host, $port);
if (!$this->connection) {
return $this->raiseError("Could not connect to {$host} on port {$port}.");
}
}
示例15: __construct
public function __construct($config)
{
foreach ($config as $name => $value) {
$this->{$name} = $value;
}
$methods = array();
if (!isset($this->auth['method'])) {
error('Unspecified authentication method.');
}
// Connect
$this->connection = ssh2_connect($this->host, isset($this->port) ? $this->port : 22, $methods);
switch ($this->auth['method']) {
case 'pubkey':
if (!isset($this->auth['public'])) {
error('Public key filename not specified.');
}
if (!isset($this->auth['private'])) {
error('Private key filename not specified.');
}
if (!ssh2_auth_pubkey_file($this->connection, $this->auth['username'], $this->auth['public'], $this->auth['private'], isset($this->auth['passphrase']) ? $this->auth['passphrase'] : null)) {
error('Public key authentication failed.');
}
break;
case 'plain':
if (!ssh2_auth_password($this->connection, $this->auth['username'], $this->auth['password'])) {
error('Plain-text authentication failed.');
}
break;
default:
error('Unknown authentication method: "' . $this->auth['method'] . '".');
}
}