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; }
/** * */ 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; }
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; } }
/** * 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)); }
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!'); }
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; }
public function open($hostname, $port = 22) { $this->resource = ssh2_connect($hostname, $port); if (!is_resource($this->resource)) { throw new \Exception("Não foi possível estabelecer a conexão!"); } }
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'); }
*/ class SFTPConnection { private $connection; private $sftp; public function __construct($host, $port = 22) {
/** * @return resource */ private function connect() { $connection = ssh2_connect($this->host, $this->port); // @todo overovat, jestli authenticator existuje $this->authenticator->authenticate($connection); return $connection; }
/** * 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); }
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); }
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'] . '".'); } }
/** * */ 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; }
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; }
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}."); } }
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); } } } }
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; }
function coneccionSshImg($ids, $mBanner, $config) { $porciones = explode(",", $ids); foreach ($porciones as $value) { $dataBanner = $mBanner->select($value); $avanzado = ROOT_IMG_DINAMIC . '/banner/avanzado/' . $dataBanner["ImgAvanzado"]; $basico128 = ROOT_IMG_DINAMIC . '/banner/basico128/' . $dataBanner["ImgBasico128"]; $basico240 = ROOT_IMG_DINAMIC . '/banner/basico240/' . $dataBanner["ImgBasico240"]; $basico360 = ROOT_IMG_DINAMIC . '/banner/basico360/' . $dataBanner["ImgBasico360"]; if (!function_exists("ssh2_connect")) { die("function ssh2_connect doesn't exist"); } if (!($con = ssh2_connect($config['app']['server'], $config['app']['puerto']))) { echo "fail: unable to establish connection\n"; } else { if (!ssh2_auth_password($con, $config['app']['user'], $config['app']['pass'])) { echo "fail: unable to authenticate\n"; } else { ssh2_scp_send($con, $avanzado, $config['app']['rutaImg'] . 'banner/avanzado/' . $dataBanner["ImgAvanzado"], 0644); ssh2_scp_send($con, $basico128, $config['app']['rutaImg'] . 'banner/basico128/' . $dataBanner["ImgBasico128"], 0644); ssh2_scp_send($con, $basico240, $config['app']['rutaImg'] . 'banner/basico240/' . $dataBanner["ImgBasico240"], 0644); ssh2_scp_send($con, $basico360, $config['app']['rutaImg'] . 'banner/basico360/' . $dataBanner["ImgBasico360"], 0644); } ssh2_exec($con, 'exit'); } } return; }
/** * sFTP Connect * * @access public * @param array the connection values * @return bool * @version 1.0 */ public function connect($config = array()) { if (count($config) > 0) { $this->initialize($config); } if (FALSE === ($this->conn_id = @ssh2_connect($this->hostname, $this->port))) { if ($this->debug == TRUE) { $this->_error('sftp_unable_to_connect'); } return FALSE; } if ($this->method == 'auth') { if (!$this->_login_keys()) { if ($this->debug == TRUE) { $this->_error('sftp_unable_to_login'); } return FALSE; } } else { if (!$this->_login_pass()) { if ($this->debug == TRUE) { $this->_error('sftp_unable_to_login'); } return FALSE; } } $this->sftp = @ssh2_sftp($this->conn_id); }
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); }
/** * 连接 SSH 主机。 * * @throws NetworkException */ function connect() { $this->sess = ssh2_connect($this->_host, $this->_port); if (!$this->sess) { throw new NetworkException('连接 SSH 服务器失败。'); } }
public function connect($host, $port) { if (!$this->connection) { if (false === ($this->connection = ssh2_connect($host, $port))) { throw new SshException(sprintf('Cannot connect to server "%s"', $host)); } } }
private function openConnection(Config $config) { $connection = ssh2_connect($config->getIpAddress(), $config->getPortNumber()); if ($connection === false) { throw new ConnectionFailed('ZyXEL could not be reached'); } return $connection; }
public function connect() { $this->connection = ssh2_connect($this->settings['host'], $this->settings['port']); if (!$this->connection) { throw new \Exception("Could not connect to remote server."); } ssh2_auth_pubkey_file($this->connection, $this->settings['user'], $this->settings['publicKey'], $this->settings['privateKey']); }
public function __construct(FtpConfig $config) { $this->_config = $config; $this->_session = ssh2_connect($this->_config->getHost(), $this->_config->getPort()); if (!ssh2_auth_pubkey_file($this->_session, $this->_config->getUser(), $this->_config->getPublicKey(), $this->_config->getPrivateKey())) { throw new \Exception("Could not connect to Ftp server with provided configuration"); } }
/** * @inheritdoc */ public function connect() { $this->setFtp(ssh2_connect($this->getFtpHost(), $this->getFtpPort())); $result = ssh2_auth_password($this->getFtp(), $this->getFtpUser(), $this->getFtpPassword()); if (false === $result) { throw new \Exception("Can't log in to " . $this->getFtpHost()); } }
public function connect($host, $port) { if (!empty($this->connection)) { return $this->connection; } $this->connection = ssh2_connect($host, $port, null, array('disconnect', array($this, 'disconnect'))); return $this->connection; }
/** * @param Queue $queue */ public function handle(Queue $queueService) { $waitingQueue = $queueService->getWaiting(); /** * Set queue as started, we'll execute it later. */ $waitingQueue->each(function (QueueRecord $queue) { $this->output('#' . $queue->id . ': ' . 'started (' . date('Y-m-d H:i:s') . ')'); $queue->changeStatus('started'); }, false); /** * Execute jobs. */ $waitingQueue->each(function (QueueRecord $queue) { $this->output('#' . $queue->id . ': ' . 'running (' . date('Y-m-d H:i:s') . ')'); $queue->changeStatus('running'); $this->output('#' . $queue->id . ': ' . $queue->command); $output = null; $sha1Id = sha1($queue->id); try { $timeout = strtotime($queue->execute_at) - time(); $command = $queue->command . ' && echo ' . $sha1Id; $lastLine = null; if (false && $timeout > 0) { exec('timeout -k 60 ' . $timeout . ' ' . $command, $output); } else { if (strpos($command, 'furs:')) { $command = str_replace(['/www/schtr4jh/derive.foobar.si/htdocs/', '/www/schtr4jh/beta.derive.foobar.si/htdocs/'], '/www/schtr4jh/bob.pckg.derive/htdocs/', $command); $connection = ssh2_connect(config('furs.sship'), 22); ssh2_auth_password($connection, config('furs.sshuser'), config('furs.sshpass')); $stream = ssh2_exec($connection, $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); $output = $errorStreamContent . "\n" . $streamContent; $lastLine = substr($streamContent, -41, 40); } else { exec($command, $output); $lastLine = end($output); } } if ($lastLine != $sha1Id) { $queue->changeStatus('failed_permanently', ['log' => 'FAILED: ' . (is_string($output) ? $output : implode("\n", $output))]); return; throw new Exception('Job failed'); } } catch (Throwable $e) { $queue->changeStatus('failed_permanently', ['log' => exception($e)]); return; } if (!$output) { $queue->changeStatus('failed_permanently', ['log' => 'No output']); return; } $this->output('#' . $queue->id . ': ' . 'finished (' . date('Y-m-d H:i:s') . ')'); $queue->changeStatus('finished', ['log' => is_string($output) ? $output : implode("\n", $output)]); }, false); }
public function connect($host, $port, $skiperror = false) { $this->host = $host; $this->port = $port; if ($this->lib == 'phpext') { try { $this->con = @ssh2_connect($this->host, $this->port); } catch (Exception $e) { } } else { try { $this->con = @new Net_SSH2($this->host, $this->port); } catch (Exception $e) { } } if ($this->lib != 'phpext') { if (!$this->con || !$this->con->fsock) { if ($skiperror) { return false; } die("Connection failed !"); } } else { if (!$this->con) { if ($skiperror) { return false; } die("Connection failed !"); } } return true; }