src/Controller.php
public function connect($host, $auth, $port = 22, $timeout = 10)
	{
		$this->ssh = new SSH2($host, $port, $timeout);

		if (!isset($auth['key']) && isset($auth['username'])) {
			// Login via username/password

			$username = $auth['username'];
			$password = isset($auth['password']) ? $auth['password'] : '';

			if (!$this->ssh->login($username, $password))
				throw new LoginFailedException(Yii::t(
					'Yii2SshConsole',
					'Login failed for user {username} using password {answer}!',
					[
						'username' => $username,
						'answer' => !empty($password) ? 1 : 0
					]
				));
			else
				return true;
		} elseif (isset($auth['key']) and isset($auth['username'])) {
			// Login via private key

			$username = $auth['username'];
			$password = isset($auth['key_password']) ? $auth['key_password'] : '';

			$key = new RSA;
			if (!empty($password)) {
				$key->setPassword($password);
			}
			$key->loadKey(file_get_contents($auth['key']));

			if (!$this->ssh->login($username, $key))
				throw new LoginFailedException(Yii::t(
					'Yii2SshConsole',
					'Login failed for user {username} using key with password {answer}!',
					[
						'username' => $username,
						'answer' => !empty($password) ? 1 : 0
					]
				));
			else
				return true;
		} else {
			// No username given

			throw new LoginUnknownException(Yii::t(
					'Yii2SshConsole',
					'No username given!'
			));
		}

		return false;
	}
public function connect($host, $auth, $port = 22, $timeout = 10)
	{
		$this->ssh = new SSH2($host, $port, $timeout);

		if (!isset($auth['key']) && isset($auth['username'])) {
			// Login via username/password

			$username = $auth['username'];
			$password = isset($auth['password']) ? $auth['password'] : '';

			if (!$this->ssh->login($username, $password))
				throw new LoginFailedException(Yii::t(
					'Yii2SshConsole',
					'Login failed for user {username} using password {answer}!',
					[
						'username' => $username,
						'answer' => !empty($password) ? 1 : 0
					]
				));
			else
				return true;
		} elseif (isset($auth['key']) and isset($auth['username'])) {
			// Login via private key

			$username = $auth['username'];
			$password = isset($auth['key_password']) ? $auth['key_password'] : '';

			$key = new RSA;
			if (!empty($password)) {
				$key->setPassword($password);
			}
			$key->loadKey(file_get_contents($auth['key']));

			if (!$this->ssh->login($username, $key))
				throw new LoginFailedException(Yii::t(
					'Yii2SshConsole',
					'Login failed for user {username} using key with password {answer}!',
					[
						'username' => $username,
						'answer' => !empty($password) ? 1 : 0
					]
				));
			else
				return true;
		} else {
			// No username given

			throw new LoginUnknownException(Yii::t(
					'Yii2SshConsole',
					'No username given!'
			));
		}

		return false;
	}
public function connect ( $ host , $ auth , $ port = 22 , $ timeout = 10 ) { $ this -> ssh = new SSH2 ( $ host , $ port , $ timeout ) ; if ( ! isset ( $ auth [ 'key' ] ) && isset ( $ auth [ 'username' ] ) ) { // Login via username/password $ username = $ auth [ 'username' ] ; $ password = isset ( $ auth [ 'password' ] ) ? $ auth [ 'password' ] : '' ; if ( ! $ this -> ssh -> login ( $ username , $ password ) ) throw new LoginFailedException ( Yii :: t ( 'Yii2SshConsole' , 'Login failed for user {username} using password {answer}!' , [ 'username' => $ username , 'answer' => ! empty ( $ password ) ? 1 : 0 ] ) ) ; else return true ; } elseif ( isset ( $ auth [ 'key' ] ) and isset ( $ auth [ 'username' ] ) ) { // Login via private key $ username = $ auth [ 'username' ] ; $ password = isset ( $ auth [ 'key_password' ] ) ? $ auth [ 'key_password' ] : '' ; $ key = new RSA ; if ( ! empty ( $ password ) ) { $ key -> setPassword ( $ password ) ; } $ key -> loadKey ( file_get_contents ( $ auth [ 'key' ] ) ) ; if ( ! $ this -> ssh -> login ( $ username , $ key ) ) throw new LoginFailedException ( Yii :: t ( 'Yii2SshConsole' , 'Login failed for user {username} using key with password {answer}!' , [ 'username' => $ username , 'answer' => ! empty ( $ password ) ? 1 : 0 ] ) ) ; else return true ; } else { // No username given throw new LoginUnknownException ( Yii :: t ( 'Yii2SshConsole' , 'No username given!' ) ) ; } return false ; }
Connect to the ssh server.

@param string $host
@param array $auth
Login via username/password
[
'username' => 'myname',
'password' => 'mypassword', // can be empty
]
or via private key
[
'key' => '/path/to/private.key',
'password' => 'mykeypassword', // can be empty
]
@param integer $port Default 22
@param integer $timeout Default 10 seconds

@throws \yii2sshconsole\LoginFailedException If the login failes
@throws \yii2sshconsole\LoginUnknownException If no username is set

@return bool
Connect to the ssh server .