note 104758 added to function.ssh2-connect
| From: | SteveKamerman at osu1 dot php dot net | Date: | Tue, 05 Jul 2011 22:06:07 +0000 |
| Subject: | note 104758 added to function.ssh2-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181268@lists.php.net to get a copy of this message | ||
Due to a lack of complete examples, here's a simple SSH2 class for connecting to a server,
authenticating with public key authentication, verifying the server's fingerprint, issuing
commands and reading their STDOUT and properly disconnecting. Note: You may need to make sure you
commands produce output so the response can be pulled. Some people suggest that the command is not
executed until you pull the response back.
<?php
class NiceSSH {
// SSH Host
private $ssh_host = 'myserver.example.com';
// SSH Port
private $ssh_port = 22;
// SSH Server Fingerprint
private $ssh_server_fp = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// SSH Username
private $ssh_auth_user = 'username';
// SSH Public Key File
private $ssh_auth_pub = '/home/username/.ssh/id_rsa.pub';
// SSH Private Key File
private $ssh_auth_priv = '/home/username/.ssh/id_rsa';
// SSH Private Key Passphrase (null == no passphrase)
private $ssh_auth_pass;
// SSH Connection
private $connection;
public function connect() {
if (!($this->connection = ssh2_connect($this->ssh_host, $this->ssh_port))) {
throw new Exception('Cannot connect to server');
}
$fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 |
SSH2_FINGERPRINT_HEX);
if (strcmp($this->ssh_master_fp, $fingerprint) !== 0) {
throw new Exception('Unable to verify server identity!');
}
if (!ssh2_auth_pubkey_file($this->connection, $this->ssh_auth_user, $this->ssh_auth_pub,
$this->ssh_auth_priv, $this->ssh_auth_pass)) {
throw new Exception('Autentication rejected by server');
}
}
public function exec($cmd) {
if (!($stream = ssh2_exec($this->connection, $cmd))) {
throw new Exception('SSH command failed');
}
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream, 4096)) {
$data .= $buf;
}
fclose($stream);
return $data;
}
public function disconnect() {
$this->exec($this->connection, 'echo "EXITING" && exit;');
$this->connection = null;
}
public function __destruct() {
$this->disconnect();
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 99.9.85.218
----
Manual Page -- http://www.php.net/manual/en/function.ssh2-connect.php
Edit -- https://master.php.net/note/edit/104758
Del: integrated -- https://master.php.net/note/delete/104758/integrated
Del: useless -- https://master.php.net/note/delete/104758/useless
Del: bad code -- https://master.php.net/note/delete/104758/bad+code
Del: spam -- https://master.php.net/note/delete/104758/spam
Del: non-english -- https://master.php.net/note/delete/104758/non-english
Del: in docs -- https://master.php.net/note/delete/104758/in+docs
Del: other reasons-- https://master.php.net/note/delete/104758
Reject -- https://master.php.net/note/reject/104758
Search -- https://master.php.net/manage/user-notes.php