diff --git a/netconf/Device.php b/netconf/Device.php index 1ac2179..b5b3642 100644 --- a/netconf/Device.php +++ b/netconf/Device.php @@ -34,21 +34,21 @@ class Device { * */ public function __construct(){ - if(func_num_args() ==1 && is_array(func_get_arg(0)) ) - { - $this->Device_array(func_get_arg(0)); - } - else - { - $this->Device_string(func_get_args()); - } + if(func_num_args() ==1 && is_array(func_get_arg(0)) ) + { + $this->Device_array(func_get_arg(0)); + } + else + { + $this->Device_string(func_get_args()); + } } /** This function is called when user passes list of string as arguments * while creating object of Device class */ public function Device_string($arr){ - if(count ($arr) == 4) { + if(count ($arr) == 4) { if(is_array($arr[3])) { $this->hello_rpc = $this->create_hello_rpc($arr[3]); $this->port = 830; @@ -69,15 +69,15 @@ public function Device_string($arr){ $this->hello_rpc = $this->create_hello_rpc($arr[4]); } } - else { + else { $this->port = 830; $this->hello_rpc = $this->default_hello_rpc(); } $this->hostName = $arr[0]; $this->userName = $arr[1]; $this->password = $arr[2]; - $this->connectTimeout = 10; - $this->replyTimeout =600; + $this->connectTimeout = 10; + $this->replyTimeout = 600; $this->is_connected = false; } @@ -86,46 +86,46 @@ public function Device_string($arr){ */ public function Device_array(array $params) { - if( $params["hostname"]!=null && !(empty($params["hostname"])) && (is_string($params["hostname"]))) { - $this->hostName = $params["hostname"]; - } - else{ - die ("host name should be string and should not be empty or null\n"); - } - - if (empty($params["username"]) || is_null( $params["username"] ) ){ - die ("user name should not be empty or null\n"); - } - else{ - $this->userName = $params["username"]; - } - - if (empty($params["password"]) || is_null( $params["password"] ) ){ - die("user name should not be empty or null\n"); - } - else{ - $this->password = $params["password"]; - } - - if($params["port"]!=null && !(empty($params["port"])) && is_numeric($params["port"]) ) - { - $this->port = $params["port"]; - } - else{ - $this->port = 830; - } - - if ( $params["capability"]!=null && ! (empty($params["capability"]) ) ){ - $this->hello_rpc= $this->create_hello_rpc($params["capability"]); - } - else { - $this->hello_rpc = $this->default_hello_rpc(); - } + if( $params["hostname"]!=null && !(empty($params["hostname"])) && (is_string($params["hostname"]))) { + $this->hostName = $params["hostname"]; + } + else{ + die ("host name should be string and should not be empty or null\n"); + } + + if (empty($params["username"]) || is_null( $params["username"] ) ){ + die ("user name should not be empty or null\n"); + } + else{ + $this->userName = $params["username"]; + } + + if (empty($params["password"]) || is_null( $params["password"] ) ){ + die("user name should not be empty or null\n"); + } + else{ + $this->password = $params["password"]; + } + + if($params["port"]!=null && !(empty($params["port"])) && is_numeric($params["port"]) ) + { + $this->port = $params["port"]; + } + else{ + $this->port = 830; + } + + if ( $params["capability"]!=null && ! (empty($params["capability"]) ) ){ + $this->hello_rpc= $this->create_hello_rpc($params["capability"]); + } + else { + $this->hello_rpc = $this->default_hello_rpc(); + } $this->connectTimeout=10; - $this->replyTimeout= 600; - $this->is_connected =false; - } - + $this->replyTimeout= 600; + $this->is_connected =false; + } + /** *Prepares a new object, either with default *client capabilities and default port 830, or with user specified @@ -133,62 +133,62 @@ public function Device_array(array $params) *operations. */ public function connect() { - $this->stream = expect_popen("ssh -o ConnectTimeout=$this->connectTimeout $this->userName@$this->hostName -p $this->port -s netconf"); - ini_set('expect.timeout', $this->replyTimeout); - $flag = true; - while ($flag) { - switch (expect_expectl($this->stream,array ( + $this->stream = expect_popen("ssh -o ConnectTimeout=$this->connectTimeout $this->userName@$this->hostName -p $this->port -s netconf"); + ini_set('expect.timeout', $this->replyTimeout); + $flag = true; + while ($flag) { + switch (expect_expectl($this->stream,array ( array("Password:","PASSWORD"), array("password:","PASSWORD"), array("yes/no)?","YESNO"), array("passphrase","PASSPHRASE"), array("]]>]]>","NOPASSPHRASE"), array(" ","SHELL"), - ))) { + ))) { + case "PASSWORD": + fwrite($this->stream,$this->password."\n"); + switch (expect_expectl($this->stream,array ( + array("Password:","PASSWORD"), + array("password:","PASSWORD"), + array("]]>]]>","hello"), + ))) { case "PASSWORD": - fwrite($this->stream,$this->password."\n"); - switch (expect_expectl($this->stream,array ( - array("Password:","PASSWORD"), - array("password:","PASSWORD"), - array("]]>]]>","hello"), - ))) { - case "PASSWORD": - throw new NetconfException("Wrong username or password"); - case "hello": - $this->send_hello($this->hello_rpc); - break; - } - $flag = false; + throw new NetconfException("Wrong username or password"); + case "hello": + $this->send_hello($this->hello_rpc); break; + } + $flag = false; + break; case "PASSPHRASE": fwrite($this->stream,$this->password."\n"); switch (expect_expectl($this->stream,array ( array("Password:","PASSWORD"), array("password:","PASSWORD"), array("]]>]]>","hello"), - ))) { - case "PASSWORD": - throw new NetconfException("Wrong username or password"); - case "hello": - $this->send_hello($this->hello_rpc); - break; + ))) { + case "PASSWORD": + throw new NetconfException("Wrong username or password"); + case "hello": + $this->send_hello($this->hello_rpc); + break; } $flag = false; break; - case "NOPASSPHRASE": - $this->send_hello($this->hello_rpc); - $flag = false; - break; - case "YESNO": - fwrite($this->stream,"yes\n"); // default value of yes for for new netconf host - break; - case "SHELL": - break; - case EXP_EOF : - throw new NetconfException("Timeout Connecting to device"); - default: - throw new NetconfException("Device not found/ unknown error occurred while connecting to Device"); - } + case "NOPASSPHRASE": + $this->send_hello($this->hello_rpc); + $flag = false; + break; + case "YESNO": + fwrite($this->stream,"yes\n"); // default value of yes for for new netconf host + break; + case "SHELL": + break; + case EXP_EOF : + throw new NetconfException("Timeout Connecting to device"); + default: + throw new NetconfException("Device not found/ unknown error occurred while connecting to Device"); + } } $this->is_connected = true; } @@ -197,34 +197,34 @@ public function connect() { *Sends the Hello capabilities to the netconf server. */ private function send_hello($hello) { - $reply = ""; - $reply = $this->get_rpc_reply($hello); - $serverCapability = $reply; - $this->last_rpc_reply = $reply; + $reply = ""; + $reply = $this->get_rpc_reply($hello); + $serverCapability = $reply; + $this->last_rpc_reply = $reply; } /** *Sends the RPC as a string and returns the response as a string. */ private function get_rpc_reply($rpc) { - $rpc_reply = ""; - fwrite($this->stream,$rpc."\n"); - while (1) { + $rpc_reply = ""; + fwrite($this->stream,$rpc."\n"); + while (1) { $line = fgets($this->stream); if (strncmp($line,"",5)==0) if (strpos($line,"]]>]]>")) - continue; + continue; else { - while (1) - { - $line = fgets($this->stream); - if (strpos($line,"]]>]]>")) - { - + while (1) + { + $line = fgets($this->stream); + if (strpos($line,"]]>]]>")) + { + $line = fgets($this->stream); break; - } - } + } + } } if ((strncmp($line,"]]>]]>",6))==0) break; @@ -256,28 +256,28 @@ public function execute_rpc($rpc) { $rpc_reply_string = $this->get_rpc_reply($rpcString); } $this->last_rpc_reply = $rpc_reply_string; - $rpc_reply = $this->convert_to_xml($rpc_reply_string); - return $rpc_reply; + $rpc_reply = $this->convert_to_xml($rpc_reply_string); + return $rpc_reply; } /** *Converts the string to XML. *@return XML object. */ - private function convert_to_xml($rpc_reply) { + private function convert_to_xml($rpc_reply) { $dom = new DomDocument(); $xml = $dom->loadXML($rpc_reply); if (!$xml) return false; $root = $dom->documentElement; return new XML($root,$dom); - } + } /** @retrun the last RPC Reply sent by Netconf server. */ public function get_last_rpc_reply() { - return $this->last_rpc_reply; + return $this->last_rpc_reply; } /** @@ -310,10 +310,10 @@ public function set_hostname($hostname) { *is the password which is to be set. */ public function set_password($password) { - if ($this->is_connected) - throw new NetconfException("Can't change the password for the live device. Close the device first"); - else - $this->password = $password; + if ($this->is_connected) + throw new NetconfException("Can't change the password for the live device. Close the device first"); + else + $this->password = $password; } /** @@ -322,10 +322,10 @@ public function set_password($password) { *is the port no. which is to be set. */ public function set_port($port) { - if ($this->is_connected) - throw new NetconfException("Can't change the port no for the live device. Close the device first"); - else - $this->port = $port; + if ($this->is_connected) + throw new NetconfException("Can't change the port no for the live device. Close the device first"); + else + $this->port = $port; } /** @@ -334,13 +334,13 @@ public function set_port($port) { *Client capabilities to be advertised to the Netconf server. * */ - public function setCapabilities($capabilities) { + public function setCapabilities($capabilities) { if($capabilities == null) die("Client capabilities cannot be null"); if($this->is_connected) { throw new NetconfException("Can't change clien capabilities on a live device. Close the - device first."); - $this->helloRpc = $this->create_hello_rpc($capabilities); + device first."); + $this->helloRpc = $this->create_hello_rpc($capabilities); } } @@ -350,11 +350,11 @@ public function setCapabilities($capabilities) { * is the connection timeout which is to be set */ public function setConnectTimeout($ctime){ - if($this->is_connected) - throw new NetconfException("Can't change connect timeout value for live device. Close the device first"); - else - $this->connectTimeout= $ctime; - } + if($this->is_connected) + throw new NetconfException("Can't change connect timeout value for live device. Close the device first"); + else + $this->connectTimeout= $ctime; + } /** * set replyTimeout of the Netconf server @@ -362,17 +362,17 @@ public function setConnectTimeout($ctime){ * is the reply timeout in which reply should come from server */ public function setReplyTimeout($rtime){ - if($this->is_connected){ - throw new NetconfException("Can't change reply timeout value for live device. Close the device first");} - else - $this->replyTimeout=$rtime; - } + if($this->is_connected){ + throw new NetconfException("Can't change reply timeout value for live device. Close the device first");} + else + $this->replyTimeout=$rtime; + } /** *Check if the last RPC reply returned from Netconf server has any error. *@return true if any errors are found in last RPC reply. */ - public function has_error() { + public function has_error() { if(!$this->is_connected) throw new NetconfException("No RPC executed yet, you need to establish a connection first"); if ($this->last_rpc_reply == "" || !(strstr($this->last_rpc_reply,""))) @@ -421,7 +421,7 @@ public function is_ok() { *Locks the candidate configuration. *@return true if successful. */ - public function lock_config() { + public function lock_config() { $rpc = ""; $rpc.= ""; $rpc.=""; @@ -457,7 +457,7 @@ public function unlock_config() { return true; } - private function starts_with($string,$substring) { + private function starts_with($string,$substring) { trim($substring); trim($string); $length = strlen($substring); @@ -517,10 +517,10 @@ public function load_xml_configuration($configuration,$loadType) { *@param loadType * You can choose "merge" or "replace" as the loadType. */ - public function load_text_configuration($configuration,$loadType) { + public function load_text_configuration($configuration,$loadType) { if ($loadType == null || (!($loadType == "merge") && !($loadType == "replace"))) throw new NetconfException ("'loadType' argument must be merge|replace\n"); - $rpc = ""; + $rpc = ""; $rpc.=""; $rpc.=""; $rpc.=""; @@ -537,7 +537,7 @@ public function load_text_configuration($configuration,$loadType) { $rpc.=""; $rpc.="]]>]]>\n"; $rpcReply = $this->get_rpc_reply($rpc); - $this->last_rpc_reply = $rpcReply; + $this->last_rpc_reply = $rpcReply; if ($this->has_error() || !$this->is_ok()) throw new LoadException("Load operation returned error"); } @@ -551,25 +551,25 @@ public function load_text_configuration($configuration,$loadType) { * will load 'ftp' under the 'systems services' hierarchy. *To load multiple set statements, separate them by '\n' character. */ - public function load_set_configuration($configuration) { - $rpc = ""; + public function load_set_configuration($configuration) { + $rpc = ""; $rpc.=""; $rpc.=""; $rpc.=$configuration; $rpc.=""; $rpc.=""; $rpc.=""; - $rpc.="]]>]]>\n"; + $rpc.="]]>]]>\n"; $rpcReply = $this->get_rpc_reply($rpc); $this->last_rpc_reply = $rpcReply; - if ($this->has_error() || !$this->is_ok()) + if ($this->has_error() || !$this->is_ok()) throw new LoadException("Load operation returned error"); } /** *Commit the candidate configuration. */ - public function commit() { + public function commit() { $rpc = ""; $rpc.=""; $rpc.=""; @@ -587,7 +587,7 @@ public function commit() { * Time in seconds, after which the previous active configuratio * is reverted back to. */ - public function commit_confirm($seconds) { + public function commit_confirm($seconds) { $rpc = ""; $rpc.=""; $rpc.=""; @@ -605,7 +605,7 @@ public function commit_confirm($seconds) { *Validate the candidate configuration. *@return true if validation successful. */ - public function validate() { + public function validate() { $rpc = ""; $rpc.=""; $rpc.=""; @@ -625,7 +625,7 @@ public function validate() { *Reboot the device corresponding to the Netconf Session. *@return RPC reply sent by Netconf servcer. */ - public function reboot() { + public function reboot() { $rpc = ""; $rpc.=""; $rpc.=""; @@ -640,23 +640,23 @@ public function reboot() { * Mode in which to open the configuration. * Permissible mode(s) : "private" */ - public function open_configuration($mode) { + public function open_configuration($mode) { $rpc = ""; $rpc.=""; $rpc.="<"; - $rpc.=$mode; - $rpc.="/>"; + $rpc.=$mode; + $rpc.="/>"; $rpc.=""; $rpc.=""; $rpc.="]]>]]>\n"; $rpcReply = $this->get_rpc_reply($rpc); $this->last_rpc_reply = $rpcReply; - } + } /** *This method should be called to close a private session, in case its started. */ - public function close_configuration() { + public function close_configuration() { $rpc = ""; $rpc.=""; $rpc.=""; @@ -672,7 +672,7 @@ public function close_configuration() { * the cli command to be executed. *@return result of the command,as a String. */ - public function run_cli_command() { + public function run_cli_command() { $rpcReply = ""; $format = "text"; if(func_num_args() == 2) @@ -707,8 +707,8 @@ public function run_cli_command() { *@param loadType * You can choose "merge" or "replace" as the loadType. */ - public function load_xml_file($configFile,$loadType) { - $configuration = ""; + public function load_xml_file($configFile,$loadType) { + $configuration = ""; $file = fopen($configFile,"r"); if (!$file) throw new NetconfException ("File not found error"); @@ -732,7 +732,7 @@ public function load_xml_file($configFile,$loadType) { *@param loadType * You can choose "merge" or "replace" as the loadType. */ - public function load_text_file($configFile,$loadType) { + public function load_text_file($configFile,$loadType) { $configuration = ""; $file = fopen($configFile,"r"); if (!$file) @@ -740,7 +740,7 @@ public function load_text_file($configFile,$loadType) { while ($line = fgets($file)) $configuration.=$line; fclose($file); - if ($loadType == null || (!($loadType == "merge") && !($loadType == "replace"))) + if ($loadType == null || (!($loadType == "merge") && !($loadType == "replace"))) throw new NetconfException("'loadType' argument must be merge|replace\n"); $this->load_text_configuration($configuration,$loadType); } @@ -753,7 +753,7 @@ public function load_text_file($configFile,$loadType) { * Path name of file containing configuration, in set format, * to be loaded. */ - public function load_set_file($configFile) { + public function load_set_file($configFile) { $configuration = ""; $file = fopen($configFile,"r"); if (!$file) @@ -764,7 +764,7 @@ public function load_set_file($configFile) { $this->load_set_configuration($configuration); } - private function get_config($target,$configTree) { + private function get_config($target,$configTree) { $rpc = ""; $rpc.=""; $rpc.=""; @@ -791,11 +791,11 @@ private function get_config($target,$configTree) { *<configuration></configuration> *return configuration data as XML object. */ - public function get_candidate_config() { + public function get_candidate_config() { if(func_num_args() == 1) return $this->convert_to_xml($this->get_config("candidate",func_get_arg(0))); return $this->convert_to_xml($this->get_config("candidate","")); - } + } /** *Retrieve the running configuration, or part of the configuration. @@ -807,7 +807,7 @@ public function get_candidate_config() { *<configuration></configuration> @return configuration data as XML object. */ - public function get_running_config() { + public function get_running_config() { if (func_num_args() ==1) return $this->convert_to_xml($this->get_config("running",func_get_arg(0))); return $this->convert_to_xml($this->get_config("running","")); @@ -836,7 +836,7 @@ public function get_running_config() { *NOTE : This parameter's value is redundant in case the file contains *configuration in 'set' format. */ - public function commit_this_configuration($configFile,$loadType) { + public function commit_this_configuration($configFile,$loadType) { $configuration = ""; $file = fopen($configFile,"r"); if (!$file) @@ -862,22 +862,22 @@ public function commit_this_configuration($configFile,$loadType) { /** *Closes the Netconf session */ - public function close() { - $rpc = ""; + public function close() { + $rpc = ""; $rpc.=""; $rpc.=""; $rpc.="]]>]]>\n"; $rpcReply = $this->get_rpc_reply($rpc); $this->last_rpc_reply = $rpcReply; - fclose($this->stream); + fclose($this->stream); $this->is_connected = $this->is_ok() ? false : true; - } + } /** * Create hello_rpc packet with user defined capabilities * @param capabilities * capabilities specified by user */ - private function create_hello_rpc(array $capabilities) { + private function create_hello_rpc(array $capabilities) { $hello_rpc = "\n"; $hello_rpc.="\n"; foreach ($capabilities as $capIter) { @@ -892,7 +892,7 @@ private function create_hello_rpc(array $capabilities) { /** * function to generate default capabilities of client */ - private function get_default_client_capabilities() { + private function get_default_client_capabilities() { $defaultCap[0] = "urn:ietf:params:xml:ns:netconf:base:1.0"; $defaultCap[1] = "urn:ietf:params:xml:ns:netconf:base:1.0#candidate"; $defaultCap[2] = "urn:ietf:params:xml:ns:netconf:base:1.0#confirmed-commit"; @@ -905,7 +905,7 @@ private function get_default_client_capabilities() { * function to generate default hello_rpc packet. * It calls get_default_client_capabilities() function to generate default capabilites of client */ - private function default_hello_rpc() { + private function default_hello_rpc() { $defaultCap = $this->get_default_client_capabilities(); return $this->create_hello_rpc($defaultCap); } @@ -919,9 +919,9 @@ private function default_hello_rpc() { * It will output alarm information which can be obtained from execute_rpc("get-alarm-information") */ public function __call($function,$args){ - $change=preg_replace('/_/','-',$function); - $reply=$this->execute_rpc($change); - return $reply; - } + $change=preg_replace('/_/','-',$function); + $reply=$this->execute_rpc($change); + return $reply; + } } ?>