本文整理汇总了PHP中ssh2_sftp函数的典型用法代码示例。如果您正苦于以下问题:PHP ssh2_sftp函数的具体用法?PHP ssh2_sftp怎么用?PHP ssh2_sftp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ssh2_sftp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: 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);
}
示例3: connect
/**
*
*/
public function connect()
{
$this->ssh = ssh2_connect($this->configuration['hostname'], $this->configuration['port']);
$username = $this->configuration['username'];
switch ($this->configuration[SftpDriver::CONFIG_AUTHENTICATION_METHOD]) {
case static::AUTHENTICATION_PASSWORD:
ssh2_auth_password($this->ssh, $username, $this->configuration['password']);
break;
case static::AUTHENTICATION_PUBKEY:
$publicKey = $this->configuration['publicKey'];
$privateKey = $this->configuration['privateKey'];
if (!file_exists($publicKey) || !file_exists($privateKey)) {
return;
}
$password = $this->configuration['privateKeyPassword'];
if (empty($password)) {
$password = null;
}
ssh2_auth_pubkey_file($this->ssh, $username, $publicKey, $privateKey, $password);
break;
default:
}
$this->sftp = ssh2_sftp($this->ssh);
$this->sftpWrapper = 'ssh2.sftp://' . $this->sftp;
$this->sftpWrapperLength = strlen($this->sftpWrapper);
$this->iteratorFlags = \FilesystemIterator::UNIX_PATHS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::FOLLOW_SYMLINKS;
return true;
}
示例4: __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);
}
示例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: send
/**
* @inheritdoc
*/
public function send($remoteFile, $localFile, $contentType = null)
{
$sftp = ssh2_sftp($this->getFTP());
if (false === copy($localFile, "ssh2.sftp://{$sftp}" . $remoteFile)) {
throw new \Exception("Cant upload file on FTP server");
}
return true;
}
示例7: connectSftp
/**
* Make SFTP connection over SSH2
*
* @return boolean
*/
protected function connectSftp()
{
if (!$this->connected) {
return false;
}
$this->ftpConnection = ssh2_sftp($this->connection);
return true;
}
示例8: _save
/**
* Saves the image to the remote server. If the folder structure doesn't exist, create it.
*
* @param string $relFilename path (with filename) from the CDN root
* @param string $tempfile temp file name to upload
* @return bool
*/
protected function _save($relFilename, $tempfile)
{
$base = Mage::getStoreConfig('imagecdn/ftp/base');
$remotePath = str_replace('\\', '/', str_replace('//', '/', '/' . $base . '/' . $relFilename));
ssh2_sftp_mkdir(ssh2_sftp($this->auth()), substr($remotePath, 0, strrpos($remotePath, '/')), 0777, true);
$result = ssh2_scp_send($this->auth(), $tempfile, $remotePath, 0644);
return $result ? true : false;
}
示例9: sftp
function sftp($local_file, $remote_file)
{
$this->log->verbose("sftp'ing {$local_file} to {$remote_file}");
$sftp = ssh2_sftp($this->conn);
$remote = fopen("ssh2.sftp://{$sftp}{$remote_file}", 'w');
$local = fopen($local_file, "r");
$ret = stream_copy_to_stream($local, $remote);
return $ret;
}
示例10: __construct
/**
* Constructor
*
* @param SessionInterface
* @param OutputInterface
* @throws RuntimeException
*/
public function __construct(SessionInterface $session, OutputInterface $output)
{
// Set the base object properties
parent::__construct($session, $output);
if (!$session->valid()) {
throw new RuntimeException('SSH connection failed.');
}
$this->sftp = ssh2_sftp($session->getConnection());
}
示例11: login
public function login($username, $password)
{
if (!ssh2_auth_password($this->connection, $username, $password)) {
throw new \Exception("Could not authenticate with username {$username} " . "and password {$password}.");
}
$this->sftp = ssh2_sftp($this->connection);
if (!$this->sftp) {
throw new \Exception("Could not initialize SFTP subsystem.");
}
}
示例12: doLoginPubKey
protected function doLoginPubKey($user, $pubKeyFile, $privKeyFile, $passphrase = null)
{
// try to login
if (ssh2_auth_pubkey_file($this->getSsh2Connection(), $user, $pubKeyFile, $privKeyFile, $passphrase)) {
$this->sftp_id = ssh2_sftp($this->getSsh2Connection());
return $this->sftp_id != false && $this->sftp_id != null;
} else {
return false;
}
}
示例13: ssh
function ssh($domain, $username, $password)
{
$conn = ssh2_connect($domain, 22);
if (conn === false) {
$this->sftp = false;
} else {
ssh2_auth_password($conn, $username, $password);
$this->sftp = ssh2_sftp($conn);
}
}
示例14: connection
/**
* Stablish a connection to sftp server.
*
* @param String $url
* @param String $username
* @param String $password
*
* @throws SftpNetworkException
* @throws SftpAuthenticationException
*
* @return ssh2_sftp $connection
*/
protected function connection($url, $username, $password)
{
if (!($connection = @ssh2_connect($url, "22"))) {
throw new SftpNetworkException("Could not connect to sftp server.");
}
if (@ssh2_auth_password($connection, $username, $password) === false) {
throw new SftpAuthenticationException("Invalid username or password for sftp.");
}
return ssh2_sftp($connection);
}
示例15: getSftpResource
public function getSftpResource()
{
if (null === $this->_sftpResource) {
$this->_sftpResource = @ssh2_sftp($this->getResource());
if (null === $this->_sftpResource) {
throw new Engine_Vfs_Adapter_Exception('Unable to get sftp resource');
}
}
return $this->_sftpResource;
}