host = $config['hostname'];
$this->login = $config['login'];
$this->password = $config['password'];
$this->port = $config['port'];
if (!($this->ssh = ssh2_connect($this->host, $this->port)))
return FALSE;
if (!ssh2_auth_password($this->ssh, $this->login, $this->password))
return FALSE;
$this->buffer = "";
$this->stream = ssh2_exec($this->ssh, "xml-mode netconf need-trailer");
if (!$this->stream)
return FALSE;
stream_set_blocking($this->stream, 30);
/* Send Hello and Capabilities to the server */
$hello['capabilities']['capability'] = "urn:ietf:params:xml:ns:netconf:base:1.0";
$this->hello($hello);
/* Receive Hello and Capabilities from Server */
$capability = $this->parse_capabilities($this->recv());
return TRUE;
}
function parse_capabilities($hello)
{
if ($hello->getName() != "hello")
return FALSE;
foreach ($hello->capabilities->children() as $key => $value) {
if ($key == "capability") {
// printf("debug: capability: %s\n", $value);
}
}
$cap['session-id'] = (string) $hello->{"session-id"};
return $cap;
}
function parse_rpc_reply($msg)
{
if ($msg->getName() != "rpc-reply")
return FALSE;
$msg = $msg->children();
switch ($msg->getName()) {
case "ok":
return RPC_REPLY_OK;
case "rpc-error":
printf("RPC ERROR: %s\n", $msg->asXML());
$this->error_severity = (string) $msg->{'rpc-error'}->{'error-severity'};
$this->error_info = (string) $msg->{'rpc-error'}->{'error-info'};
$this->error_message = (string) $msg->{'rpc-error'}->{'error-message'};
if ($this->error_severity == "warning")
return RPC_REPLY_WARNING;
else
return RPC_REPLY_ERROR;
default:
// printf("debug: rpc-reply: %s\n", $msg->getName());
return $msg;
}
}
function xml_parse($msg)
{
$flags = LIBXML_COMPACT || LIBXML_NOCDATA || LIBXML_NOERROR;
$msg = trim(preg_replace('@@', '', $msg));
$xml = simplexml_load_string($msg, "SimpleXMLElement", $flags);
// printf("DEBUG recv: %s", $xml->asXML());
return $xml;
}
function recv()
{
while (($pos = strpos($this->buffer, "]]>]]>")) === FALSE) {
if (!($buf = fread($this->stream, 65536)))
return FALSE;
$this->buffer .= $buf;
}
$parse = explode("]]>]]>", $this->buffer, 2);
$this->buffer = $parse[1];
return $this->xml_parse($parse[0]);
}
function send($xml)
{
// printf("DEBUG send: %s\n", $xml->asXML());
fprintf($this->stream, "%s]]>]]>", $xml->asXML());
}
function array2xml($array, $node)
{
foreach ($array as $key => $value) {
if (is_array($value))
$this->array2xml($value, $node->addChild($key));
else {
if ($value === TRUE)
$node->addChild($key);
else
$node->addChild($key, $value);
}
}
}
function hello($param)
{
$hello = new simpleXMLElement("");
$this->array2xml($param, $hello);
$this->send($hello);
}
function rpc($param)
{
$rpc = new simpleXMLElement("");
$this->array2xml($param, $rpc);
$this->send($rpc);
return $this->parse_rpc_reply($this->recv());
}
function lock()
{
$rpc['lock']['target']['candidate'] = TRUE;
return $this->rpc($rpc);
}
function unlock()
{
$rpc['unlock']['target']['candidate'] = TRUE;
return $this->rpc($rpc);
}
function validate($source = "candidate")
{
$rpc['validate']['source'][$source] = TRUE;
return $this->rpc($rpc);
}
function discard_changes()
{
$rpc['discard-changes'] = TRUE;
return $this->rpc($rpc);
}
function get_config($source = "running", $filter = NULL)
{
$rpc['get-config']['source'][$source] = TRUE;
if ($filter)
$rpc['get-config']['filter'] = $filter;
return $this->rpc($rpc);
}
function copy_config($url, $operation = "merge", $error = "error", $target = "candidate")
{
$rpc['edit-config']['target'][$target] = TRUE;
$rpc['edit-config']['default-operation'] = $operation;
$rpc['edit-config']['error-option'] = $error;
$rpc['edit-config']['url'] = $url;
return $this->rpc($rpc);
}
function edit_config($config, $type = "url format=\"text\"", $operation = "merge", $error = "stop-on-error", $target = "candidate")
{
$rpc['edit-config']['target'][$target] = TRUE;
$rpc['edit-config']['default-operation'] = $operation;
$rpc['edit-config']['error-option'] = $error;
if ($type == "config")
$rpc['edit-config'][$type]['configuration'] = $config;
else if ($type == "config-text")
$rpc['edit-config'][$type]['configuration-text'] = $config;
else
$rpc['edit-config'][$type] = $config;
return $this->rpc($rpc);
}
function commit()
{
$rpc['commit'] = TRUE;
return $this->rpc($rpc);
}
function close()
{
$rpc['close-session'] = TRUE;
$result = $this->rpc($rpc);
fclose($this->ssh);
return $result;
}
}
?>