Junos Automation (Scripting)
Junos Automation (Scripting)

PHP Client 0.2

[ Edited ]
11.18.10   |  
‎11-18-2010 04:38 PM

Hi All,

 

This is the first version of a very basic PHP JunOScript client I wrote.

 

You can read this thread for more information.

 

If you have any tips etc I would love to hear them.

 

 

<?php
/*
	PHP JUNOScript Client 0.2 Beta
	Copyright Michael Dale 2010
	mdale@dalegroup.net
	
	Tested with PHP 5.2+ and a Juniper SRX210 JunOS 10.2R3
	
	To Use:
	
	1) Enable clear text JUNOScript on JunOS device (not a secure connection, only connect to device via a fully trusted network):
		set system services xnm-clear-text
	2) Create an instances of this class and setup user/pass details for JunOS device:
		$junos_obj	= new ipm_junos();
		$junos_obj->set_config(array('ip_address' => '192.168.1.1', 'username' => 'root', 'password' => ''));
	3) Login with:
		$junos_obj->login();
	4) Get the config:
		$junos_obj->get_configuration();
	5) Logout when finished:
		$junos_obj->logout();
	6) Once you have logged out you can load the output as an XML object:
		$junos_obj->load_xml();
	7) And to get it:
		$xml_object = $junos_obj->get_xml();
		
	You can send a manual rpc command while connected:
	$result = $junos_obj->rpc_command('<get-configuration></get-configuration>');
	
	Limitations:
	1) Doesn't disconnect correctly. It does logout but there is a maximum 10 seconds connection time.
	2) Slow. Probably not so useful if you have lots of devices.
		
	To Do:
	
	Lots! Ideas include:
	1) Better error handling
	2) More commands
	
	Email me mdale@dalegroup.net for help or to give me your ideas.
	
	Free for personal use only. Please contact me if you want to use for business purposes.
	
*/

class ipm_junos {

	private $config = NULL;
	private $connection = NULL;
	private $output = '';
	private $xml = NULL;

	function __construct() {
	
	}
	
	public function set_config($array) {
		if (isset($array['ip_address'])) {
			$this->config['ip_address'] = $array['ip_address'];
		}
		
		if (isset($array['port'])) {
			$this->config['port'] 		= (int) $array['port'];
		}
		else {
			$this->config['port'] 		= 3221;
		}
		
		if (isset($array['username'])) {
			$this->config['username'] 	= $array['username'];
		}
		if (isset($array['password'])) {
			$this->config['password'] 	= $array['password'];
		}
	}
	
	private function send_command($command) {
		if (!$this->create_connection()) {
			return false;
		}
		
		fwrite($this->connection, $command);
		
		$result = '';
		
		while (!feof($this->connection)) {
		   $temp_result = fgets($this->connection, 128);
		   if ($temp_result == NULL) {
				break;
		   }
		   $result .= $temp_result;
		   $this->set_output($temp_result);
		}
		
		return $result;
	}
	
	public function get_output() {
		return $this->output;
	}
	
	public function load_xml() {
		$this->xml = simplexml_load_string($this->get_output());
	}
	public function get_xml() {
		if ($this->xml != NULL) {
			return $this->xml;
		}
		else {
			return false;
		}
	}
	
	public function get_attribute($name) {
		$attrs = $this->xml->attributes();
		if (isset($attrs[$name])) {
			return (string) $attrs[$name];
		}
		return false;
	}
	
	private function set_output($text) {
		$this->output .= $text;
	}
	
	private function create_connection() {
		if (is_resource($this->connection)) {
			return true;
		}
		else {
			//create connection
			$this->connection = stream_socket_client("tcp://" . $this->config['ip_address'] . ':' . $this->config['port'], $errno, $errstr, 15);
			if (is_resource($this->connection)) {
				//set timeout (max connection time before disconnect)
				stream_set_timeout($this->connection, 10);
				return true;
			}
			else {
				return false;
			}
		}
	}
	
	private function close_connection() {
		if (is_resource($this->connection)) {
			fclose($this->connection);
			return true;
		}
		else {
			return true;
		}
	}
	
	public function rpc_command($string) {
		if (!$this->create_connection()) {
			return false;
		}
		$command	= '
		<rpc>
			'.(string)$string.'
		</rpc>			
		';
		return $this->send_command($command);
	}
	
	public function logout() {
		if (!$this->create_connection()) {
			return false;
		}
		$command	= '
		<rpc>
			<request-end-session/> 
		</rpc>			
		';
		$this->send_command($command);

		$command	= '
			</junoscript>		
		';
		$this->send_command($command);
		
		return $this->close_connection();
	}
	
	public function get_configuration() {
		if (!$this->create_connection()) {
			return false;
		}
		$command	= '
		<rpc>
			<get-configuration></get-configuration>
		</rpc>			
		';
		return $this->send_command($command);
	}
	
	public function login() {
		if (!$this->create_connection()) {
			return false;
		}
		
		$command	= '
		<?xml version="1.0" encoding="us-ascii"?>
			<junoscript version="1.0" hostname="" junos:key="" release="">
				<rpc>
					<request-login>
						<username>'.$this->config['username'].'</username>
						<challenge-response>'.$this->config['password'].'</challenge-response>
					</request-login>
				</rpc>
			
		';
		$result = $this->send_command($command);
		return $result;
	}
	
}

?>

 

 

 

3 REPLIES
Junos Automation (Scripting)

Re: PHP Client 0.2

11.29.10   |  
‎11-29-2010 03:13 AM

Good day sir,

 

Can you please give me an example code that will instruct a Juniper SRX device to block an IP address thereby preventing it from passing through the SRX. I would also like to know the JUNOS code for blocking an IP address.

 

Thank you very much.

Junos Automation (Scripting)

Re: PHP Client 0.2

10.24.12   |  
‎10-24-2012 02:11 AM

Hi,

 

I found your code very helpful, but i need some kind of help from you.

Can you please tell me that how i can send the multiple request by using the function named rpc_command, bacause i can't get the result of multiple request and second thing which is more concerned for me that i wait about 30 sec for getting the response of my request. Can you please help me in this regard.

 

Thanks a lot.

Junos Automation (Scripting)

Re: PHP Client 0.2

12.21.12   |  
‎12-21-2012 10:32 PM

Sorry to bump such an old thread, but did you ever expand on this class? There seems to be very few people working on PHP junosxml integration.

 

Gordon