SSH Client
In Jalno, the packages\base\SSH class is provided for connecting to a server through the SSH2 protocol.
Using this class, by supplying the domain name or IP address, the port, the username, and the password, you can easily connect to the server and run any command-line command on it.
The following methods are defined in the SSH class for connecting to a server.
| Method | Purpose |
|---|---|
| AuthByPassword(string $username, string $password): bool | Authentication |
| execute(string $comment) | Runs a command |
| connection() | Returns the established connection |
| getHost(): string | Gets the domain name or IP address |
| getPort(): int | Gets the port |
| getUsername(): string | Gets the username |
| getPassword(): string | Gets the password |
Establishing a connection to the server
To establish a connection to the server, you first need to create an object of the SSH class and pass it the host address and the relevant port.
The established connection is stored in the class's $connection property, and you can access it by calling the connection() method.
If the entered host address or port is incorrect, the \packages\base\ssh\ConnectionException exception is thrown.
The AuthByPassword method performs the authentication operation. It takes the username as its first argument and the password as its second argument, both as strings.
The method returns true if authentication is confirmed, and false otherwise.
Example
namespace packages\packagename\processes;
use packages\base\{Process, SSH};
class Main extends Process {
public function index(): void {
try {
$ssh = new SSH("www.example.com", 25143);
$ssh->AuthByPassword("ali", "123");
} catch (SSH\ConnectionException $e) { }
}
}
Running commands
The execute method is defined for running commands. Its input is the command you intend to run on the server, provided as a string.
The method returns the result of the sent command.
Before running commands, you need to check the return value of the AuthByPassword method. If the return value of AuthByPassword is false and the execute method is called, you receive a Warning.
Warning: ssh2_exec(): Connection not authenticated
Warning: ssh2_fetch_stream() expects parameter
Warning: stream_set_blocking() expects parameter
Warning: stream_get_contents() expects parameter
Example of incorrect code
namespace packages\packagename\processes;
use packages\base\{Process, SSH};
class Main extends Process {
public function index(): void {
try {
$ssh = new SSH("www.example.com", 25143);
$ssh->AuthByPassword("ali", "123");
$response = $ssh->execute("/usr/local/bin/php -i");
print_r($response);
} catch (SSH\ConnectionException $e) { }
}
}
Example of correct code
namespace packages\packagename\processes;
use packages\base\{Process, SSH};
class Main extends Process {
public function index(): void {
try {
$ssh = new SSH("www.example.com", 25143);
$AuthByPassword = $ssh->AuthByPassword("ali", "123");
if(!$AuthByPassword){
throw new SSH\ConnectionException();
}
$response = $ssh->execute("/usr/local/bin/php -i");
print_r($response);
} catch (SSH\ConnectionException $e) { }
}
}