Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
0
PHP Client 0.2
[ Edited ]Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-18-2010 04:38 PM - last edited on 11-18-2010 05:46 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;
}
}
?>
0
Re: PHP Client 0.2
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
