Migrated from ncclient to pyEZ

This commit is contained in:
David Barroso committed 2015-03-16 09:52:36 +01:00
1 parent 1b672f716c
commit d4d3c20929
1 file changed
+13 -38
+13 -38
View File
@@ -1,7 +1,7 @@
from base import NetworkDriver from base import NetworkDriver
from ncclient import manager from jnpr.junos import Device
from jnpr.junos.utils.config import Config
def __execute_rpc__(conn, rpc_command): def __execute_rpc__(conn, rpc_command):
output = conn.rpc(str(rpc_command)) output = conn.rpc(str(rpc_command))
@@ -18,60 +18,35 @@ class JunOSDriver(NetworkDriver):
self.hostname = hostname self.hostname = hostname
self.user = user self.user = user
self.password = password self.password = password
self.device = Device(hostname, user=user, password=password)
def open(self): def open(self):
self.device = manager.connect( self.device.open()
host=self.hostname, self.device.bind( cu=Config )
port=830, self.device.cu.lock()
username=self.user,
password=self.password,
timeout=10,
hostkey_verify=False,
device_params={'name':'junos'},
)
rpc_command = '<lock><target><candidate/></target></lock>'
__execute_rpc__(self.device, rpc_command)
def close(self): def close(self):
rpc_command = '<unlock><target><candidate/></target></unlock>' self.device.cu.unlock()
__execute_rpc__(self.device, rpc_command) self.device.close()
self.device.close_session()
def load_candidate_config(self, filename=None, config=None): def load_candidate_config(self, filename=None, config=None):
if filename is None: if filename is None:
configuration = config configuration = config
else: else:
with open(filename) as f: with open(filename) as f:
configuration = f.read() configuration = f.read()
rpc_command = ''' self.device.cu.load(configuration, format='text', overwrite=True)
<load-configuration format="text" action="replace">
<configuration-text>
%s
</configuration-text>
</load-configuration>
''' % configuration
__execute_rpc__(self.device, rpc_command)
def compare_config(self): def compare_config(self):
rpc_command = ''' return self.device.cu.diff()
<get-configuration changed="changed" database= "candidate" format="text" compare="rollback" rollback="0">
</get-configuration>'''
conf = __execute_rpc__(self.device, rpc_command)
return conf.xpath('/rpc-reply/configuration-information/configuration-output')[0].text
def commit_config(self): def commit_config(self):
rpc_command = '<commit/>' self.device.cu.commit()
__execute_rpc__(self.device, rpc_command)
def discard_config(self): def discard_config(self):
rpc_command = '<discard-changes/>' self.device.cu.rollback(rb_id=0)
__execute_rpc__(self.device, rpc_command)
def rollback(self): def rollback(self):
rpc_command = '<load-configuration rollback="1"/>' self.device.cu.rollback(rb_id=1)
__execute_rpc__(self.device, rpc_command)
self.commit_config() self.commit_config()