note 104758 modified in function.ssh2-connect by danbrown

From: Date: Tue, 26 Jun 2012 16:18:07 +0000
Subject: note 104758 modified in function.ssh2-connect by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188870@lists.php.net to get a copy of this message
Due to a lack of complete examples, here&#39;s a simple SSH2 class for connecting to a server, authenticating with public key authentication, verifying the server&#39;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.&#13;&#10;&#60;?php&#13;&#10;class NiceSSH {&#13;&#10;&#9;// SSH Host&#13;&#10;&#9;private $ssh_host = &#39;myserver.example.com&#39;;&#13;&#10;&#9;// SSH Port&#13;&#10;&#9;private $ssh_port = 22;&#13;&#10;&#9;// SSH Server Fingerprint&#13;&#10;&#9;private $ssh_server_fp = &#39;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;;&#13;&#10;&#9;// SSH Username&#13;&#10;&#9;private $ssh_auth_user = &#39;username&#39;;&#13;&#10;&#9;// SSH Public Key File&#13;&#10;&#9;private $ssh_auth_pub = &#39;/home/username/.ssh/id_rsa.pub&#39;;&#13;&#10;&#9;// SSH Private Key File&#13;&#10;&#9;private $ssh_auth_priv = &#39;/home/username/.ssh/id_rsa&#39;;&#13;&#10;&#9;// SSH Private Key Passphrase (null == no passphrase)&#13;&#10;&#9;private $ssh_auth_pass;&#13;&#10;&#9;// SSH Connection&#13;&#10;&#9;private $connection;&#13;&#10;&#9;&#13;&#10;&#9;public function connect() {&#13;&#10;&#9;&#9;if (!($this-&#62;connection = ssh2_connect($this-&#62;ssh_host, $this-&#62;ssh_port))) {&#13;&#10;&#9;&#9;&#9;throw new Exception(&#39;Cannot connect to server&#39;);&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;$fingerprint = ssh2_fingerprint($this-&#62;connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);&#13;&#10;&#9;&#9;if (strcmp($this-&#62;ssh_server_fp, $fingerprint) !== 0) {&#13;&#10;&#9;&#9;&#9;throw new Exception(&#39;Unable to verify server identity!&#39;);&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;if (!ssh2_auth_pubkey_file($this-&#62;connection, $this-&#62;ssh_auth_user, $this-&#62;ssh_auth_pub, $this-&#62;ssh_auth_priv, $this-&#62;ssh_auth_pass)) {&#13;&#10;&#9;&#9;&#9;throw new Exception(&#39;Autentication rejected by server&#39;);&#13;&#10;&#9;&#9;}&#13;&#10;&#9;}&#13;&#10;&#9;public function exec($cmd) {&#13;&#10;&#9;&#9;if (!($stream = ssh2_exec($this-&#62;connection, $cmd))) {&#13;&#10;&#9;&#9;&#9;throw new Exception(&#39;SSH command failed&#39;);&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;stream_set_blocking($stream, true);&#13;&#10;&#9;&#9;$data = &#34;&#34;;&#13;&#10;&#9;&#9;while ($buf = fread($stream, 4096)) {&#13;&#10;&#9;&#9;&#9;$data .= $buf;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;fclose($stream);&#13;&#10;&#9;&#9;return $data;&#13;&#10;&#9;}&#13;&#10;&#9;public function disconnect() {&#13;&#10;&#9;&#9;$this-&#62;exec(&#39;echo &#34;EXITING&#34; &#38;&#38; exit;&#39;);&#13;&#10;&#9;&#9;$this-&#62;connection = null;&#13;&#10;&#9;}&#13;&#10;&#9;public function __destruct() {&#13;&#10;&#9;&#9;$this-&#62;disconnect();&#13;&#10;&#9;}&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;[EDIT BY danbrown AT php DOT net: Contains two bugfixes suggested by &#39;AlainC&#39; in user note #109185 (removed) on 26-JUN-2012.] --was-- 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(); } } ?> http://php.net/manual/en/function.ssh2-connect.php

« previous php.notes (#188870) next »