auth = false; $this->ssh = false; $this->rawXml = ''; } public function connect_pass($host, $username, $password, $port=22) { if (!$this->sshconnect($host, $port)) return false; if (!$this->authpass($username, $password)) return false; if (!$this->startshell()) return false; return true; } public function connect_key($host, $username, $pubkeyfile, $privkeyfile, $passphrase=null, $port=22) { if (!$this->sshconnect($host, $port)) return false; if (!$this->authkey($username, $pubkeyfile, $privkeyfile, $passphrase)) return false; if (!$this->startshell()) return false; return true; } public function sshconnect($host, $port=22) { $this->ssh = ssh2_connect($host, $port); if (!$this->ssh) return false; return true; } public function authpass($username, $password) { if (!$this->ssh) return false; $this->auth = ssh2_auth_password($this->ssh, $username, $password); return $this->auth; } public function authkey($username, $pubkeyfile, $privkeyfile, $passphrase=null) { if (!$this->ssh) return false; $this->auth = ssh2_auth_pubkey_file($this->ssh, $username, $pubkeyfile, $privkeyfile, $passphrase); return $this->auth; } public function startshell($hostname=null, $encoding="us-ascii", $version="1.0") { if (!$this->auth) return false; $this->stream = ssh2_shell($this->ssh, 'vt102', null, 0, 0, SSH2_TERM_UNIT_CHARS); if (!$this->stream) return false; /* start junoscript shell */ fwrite($this->stream, "junoscript\n"); $s = ''; while (strstr($s, 'start') === FALSE) { $s = fgets($this->stream); $this->rawXml .= $s; } /* emitting PI and tag */ fwrite($this->stream, ''); fwrite($this->stream, "'); return true; } public function close() { if (!$this->stream) return false; $close = ''; fwrite($this->stream, $close); $s = ''; while (strstr($s, '/junoscript') === FALSE) { $s = fgets($this->stream); $this->rawXml .= $s; } fwrite($this->stream, ''); /* server resets connection now */ $this->auth = null; $this->stream = null; $this->ssh = null; return true; } public function exec($rpcstring) { $answer = ''; if (!$this->stream) return false; fwrite($this->stream, ''.$rpcstring.''); $s = ''; while (strstr($s, '/rpc-reply') === FALSE) { $s = fgets($this->stream); $this->rawXml .= $s; $answer .= $s; } return $answer; } } /* $j = new JunosRPC(); $result = $j->connect_key("juniper", "username", "id_rsa.pub", "id_rsa"); echo 'Connect: ', ($result ? 'true' : 'false'), "\n"; $answer = $j->exec(''); $result = $j->close(); echo 'Close: ', ($result ? 'true' : 'false'), "\n"; $xml = simplexml_load_string($answer); print_r($xml); */ ?>