本文整理汇总了Python中jnpr.junos.utils.config.Config.pdiff方法的典型用法代码示例。如果您正苦于以下问题:Python Config.pdiff方法的具体用法?Python Config.pdiff怎么用?Python Config.pdiff使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jnpr.junos.utils.config.Config
的用法示例。
在下文中一共展示了Config.pdiff方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: deployConfig
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
def deployConfig(my_device_list_dict, my_username, my_password, my_config_template_file):
my_hostname=""
try:
my_hostname=my_device_list_dict["mgmt_ip"]
printProgress("INFO",my_hostname,"Connecting to device through netconf.")
dev=Device(my_hostname,user=my_username,password=my_password)
dev.open()
dev.timeout=3*60
cu = Config(dev)
printProgress("INFO",my_hostname,"Going to load template the config now.")
# Determine if template file is in "set" or "bracketed" format
if isSet(my_config_template_file):
rsp=cu.load(template_path=my_config_template_file,format='set',template_vars=my_device_list_dict)
else:
rsp=cu.load(template_path=my_config_template_file,template_vars=my_device_list_dict)
printProgress("INFO",my_hostname,"Performing diff between active and candidate config.")
cu.pdiff()
printProgress("INFO",my_hostname,"Performing commit check")
if cu.commit_check():
printProgress("INFO",my_hostname,"commit check was successfull.")
printProgress("INFO",my_hostname,"performing commit now.")
commit_status=cu.commit()
printProgress("INFO",my_hostname,"disconnecting from device.")
dev.close()
return commit_status
else:
return False
except Exception,err:
printProgress("ERROR",my_hostname,"Encountered exception while deploying config")
printProgress("ERROR",my_hostname,str(err))
return False
示例2: edit_config
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
def edit_config(self,dev,ip):
conf='set policy-options prefix-list blacklisted-ips'
_conf=''
#print p,dev
ips=ip.split()
for i in ips:
print "ip=",i
if IPAddress(i) in IPNetwork("116.197.188.0/23") or IPAddress(i) in IPNetwork("116.197.190.0/23") or IPAddress(i) in IPNetwork("193.110.49.0/23") or IPAddress(i) in IPNetwork("193.110.55.0/23") or IPAddress(i) in IPNetwork("66.129.239.0/23") or IPAddress(i) in IPNetwork("66.129.241.0/23"):
print "Internal IP"
continue
_conf=_conf+conf+' '+i+'\n'
print "conf", _conf
try:
cu=Config(dev)
#print '1'
cu.load(_conf,format='set')
#fp.write(str(datetime.now())+"the conf has been added to "+p[0]+" \n"+_conf+" \n")
print dev.facts['hostname']
cu.pdiff()
cu.commit(confirm=3)
print "Waiting for 2 mins before final commit..."
cu.commit()
#fp.write(str(datetime.now())+" the commit has been done "+" \n")
print "the config has been committed for",dev.facts['hostname']
except Exception as e:
print "commit failed:",e
示例3: _normal_device
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
def _normal_device(dev):
x=loadyaml(site.getsitepackages()[1]+'/jnpr/junos/op/phyport.yml')
table=x['PhyPortTable'](dev)
table.get()
ke=table.keys()
cu=Config(dev)
print cu
for i in ke:
sw = dev.rpc.get_config(filter_xml=etree.XML('<configuration><interfaces><interface><name>'+i+'</name></interface></interfaces></configuration>'))
s=etree.tostring(sw)
#print s
e = ET.XML(s)
x=etree_to_dict(e)
#print x
if(x['configuration']==''):
print 'Unused port '+i+ ' disabled'
set_cmd='set interfaces '+i+' disable description "unused port"'
cu.load(set_cmd, format='set')
else:
try:
if(x['configuration']['interfaces']['interface']['unit']['family']['inet']==''):
#print 'Unused port '+i+ ' disabled'
set_cmd='set interfaces '+i+' disable description "unused port"'
cu.load(set_cmd, format='set')
except:
pass
cu.pdiff()
cu.commit(confirm=1)
print "the config has been committed"
示例4: __init__
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
class Jconfig:
def __init__(self, host, user='rancid', password='xxxxx', t_vars={}):
self.host = host
self.user = user
self.password = password
self.t_vars = t_vars
self.dev = Device(host=self.host, user=self.user, password=self.password, gather_facts=False)
try:
self.dev.open()
self.dev.timeout = 300
self.cfg = Config(self.dev)
except Exception as err:
print err
sys.exit(1)
def loadconf(self, t_file):
try:
print "=> Loading file %s on %s" % (t_file, self.host)
self.cfg.load(template_path=t_file, template_vars=self.t_vars, overwrite=False, merge=True)
except Exception as err:
print err
def askcommit(self):
try:
print "== Configuration diff on %s" % (self.host)
self.cfg.pdiff()
answer = raw_input('Do you want to commit change ? [y/n]')
if answer[0] == 'y':
self.cfg.commit()
except Exception as err:
print err
def commit(self):
try:
print "== Configuration diff on %s" % (self.host)
self.cfg.pdiff()
self.cfg.commit()
except Exception as err:
print err
def getconf(self):
try:
conf = self.dev.rpc.get_configuration(dict(format='text'))
return etree.tostring(conf, method="text")
except Exception as err:
print err
示例5: loadVRFConfig
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
def loadVRFConfig(VRF_number):
if 1 < int(VRF_number) < 255:
dev = Device(host="172.2.3.5", user="mario", ssh_private_key_file="/root/.ssh/VRF")
dev.open()
conf = Config(dev)
variables = {}
variables["VRF_instance"] = VRF_number
conf.load(template_path="templateVRF_set.conf", template_vars=variables, format="set", merge=True)
conf.pdiff()
if conf.commit_check():
print "This config would commit succesfully"
else:
print "Config puck`d up"
dev.close()
else:
print "The VRF number must be between 2 and 254"
示例6: main
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
def main():
'''
I will test the case with applying config for 1 minute
'''
pwd = getpass()
ip_addr = raw_input('''Enter Juniper SRX IP"(184.105.247.76)": ''')
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
try:
a_device = Device(**juniper_srx)
a_device.open()
cfg = Config(a_device)
cfg.lock()
cfg.load(path="exercice4_config.xml" , format="xml", merge=True)
print "#"*80
print "Displaying the differences between the running config and the candidate config:"
print "#"*80
cfg.pdiff()
print "+"*80
print "Applying config"
print "+"*80
cfg.commit(comment="Applying config from exercice4_config.xml")
print "-"*80
print "reverting config back"
cfg.rollback(1)
cfg.pdiff()
cfg.commit()
print "-"*80
print "\n"*2
print
except:
print
print "Authentication Error"
print
示例7: TestConfig
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
#.........这里部分代码省略.........
self.conf.rpc.commit_configuration = MagicMock()
self.assertTrue(self.conf.commit_check())
@patch('jnpr.junos.utils.config.JXML.rpc_error')
def test_commit_check_exception(self, mock_jxml):
class MyException(Exception):
xml = 'test'
self.conf.rpc.commit_configuration = MagicMock(side_effect=MyException)
# with self.assertRaises(AttributeError):
self.conf.commit_check()
def test_config_commit_check_exception_RpcError(self):
ex = RpcError(rsp='ok')
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex)
self.assertTrue(self.conf.commit_check())
import xml.etree.ElementTree as ET
xmldata = """<data><company name="Juniper">
<code>pyez</code>
<year>2013</year>
</company></data>"""
root = ET.fromstring(xmldata)
el = root.find('company')
ex = RpcError(rsp=el)
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex)
self.assertRaises(CommitError, self.conf.commit_check)
def test_config_diff(self):
self.conf.rpc.get_configuration = MagicMock()
self.conf.diff()
self.conf.rpc.get_configuration.\
assert_called_with(
{'compare': 'rollback', 'rollback': '0', 'format': 'text'})
def test_config_pdiff(self):
self.conf.diff = MagicMock(return_value='Stuff')
self.conf.pdiff()
self.conf.diff.assert_called_once_with(0)
def test_config_load(self):
self.assertRaises(RuntimeError, self.conf.load)
def test_config_load_vargs_len(self):
self.assertRaises(RuntimeError, self.conf.load,
'test.xml')
def test_config_load_len_with_format_set(self):
self.conf.rpc.load_config = \
MagicMock(return_value='rpc_contents')
self.assertEqual(self.conf.load('test.xml', format='set'),
'rpc_contents')
def test_config_load_len_with_format_xml(self):
self.conf.rpc.load_config = \
MagicMock(return_value='rpc_contents')
xmldata = """<snmp>
<community>
<name>iBGP</name>
</community>
</snmp>"""
self.assertEqual(self.conf.load(xmldata, format='xml'),
'rpc_contents')
@patch('__builtin__.open')
def test_config_load_lformat_byext_ValueError(self, mock_open):
self.conf.rpc.load_config = \
示例8: Config
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
cu = Config(dev)
data = """interfaces {
ge-1/0/1 {
description "MPLS interface";
unit 0 {
family mpls;
}
}
ge-1/0/2 {
description "MPLS interface";
unit 0 {
family mpls;
}
}
}
protocols {
mpls {
interface ge-1/0/1;
interface ge-1/0/2;
}
}
"""
cu.load(data, format='text')
cu.pdiff()
if cu.commit_check():
cu.commit()
else:
cu.rollback()
dev.close()
示例9:
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
#-----------------------------------------------------------------------------------------------------------------------
# DESCRIPTION:
# Use PyEZ to enable LLDP on a subset of interfaces for a specific device.
#
# CONTACT: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
#
# CREATED: 2015-11-11
#
# VERSION: 1
#
# USAGE: enable_lldp.py
# -----------------------------------------------------------------------------------------------------------------------
from jnpr.junos import Device # import the class Device
from jnpr.junos.utils.config import Config # import the class Config
import yaml
s=open('configuration_management/interfaces.yml').read() #s is a string
my_variables=yaml.load(s) # my_variables is a dictionary
a_device=Device (host="172.30.179.113", user="pytraining", password="Poclab123")
a_device.open()
cfg=Config(a_device) # cfg is the candidate configuration
cfg.rollback()
cfg.load(template_path='configuration_management/template_lldp.j2', template_vars=my_variables, format='set')
cfg.pdiff()
cfg.commit()
示例10: str
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
#pprint( device.facts )
device.bind(cfg=Config)
try:
device.rpc.open_configuration(private=True)
except jnpr.junos.exception.RpcError as e:
if 'severity: warning' in str(e):
print str(e)
pass
else:
raise
for line in open("fila"):
elhay = Config(device)
set_cmd = line
elhay.load(set_cmd, format='set')
elhay.pdiff()
elhay.commit()
#for line in open("fila"):
# print device.cli(line)
#device.cfg.load(template_path=template, template_vars=customer)
#device.bind(cu=Config)
#device.rpc.open_configuration(private=True,normalize=True)
#device.cu.load("set system host-name r0")
#device.cu.commit()
#device.rpc.close_configuration()
示例11: TestConfig
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
#.........这里部分代码省略.........
self.conf.rpc.get_configuration = MagicMock()
self.conf.diff()
self.conf.rpc.get_configuration.\
assert_called_with(
{'compare': 'rollback', 'rollback': '0', 'format': 'text'})
def test_config_diff_exception_severity_warning(self):
rpc_xml = '''
<rpc-error>
<error-severity>warning</error-severity>
<error-info><bad-element>bgp</bad-element></error-info>
<error-message>mgd: statement must contain additional statements</error-message>
</rpc-error>
'''
rsp = etree.XML(rpc_xml)
self.conf.rpc.get_configuration = MagicMock(
side_effect=RpcError(rsp=rsp))
self.assertEqual(self.conf.diff(),
"Unable to parse diff from response!")
def test_config_diff_exception_severity_warning_still_raise(self):
rpc_xml = '''
<rpc-error>
<error-severity>warning</error-severity>
<error-info><bad-element>bgp</bad-element></error-info>
<error-message>statement not found</error-message>
</rpc-error>
'''
rsp = etree.XML(rpc_xml)
self.conf.rpc.get_configuration = MagicMock(
side_effect=RpcError(rsp=rsp))
self.assertRaises(RpcError, self.conf.diff)
def test_config_pdiff(self):
self.conf.diff = MagicMock(return_value='Stuff')
self.conf.pdiff()
self.conf.diff.assert_called_once_with(0)
def test_config_load(self):
self.assertRaises(RuntimeError, self.conf.load)
def test_config_load_vargs_len(self):
self.assertRaises(RuntimeError, self.conf.load,
'test.xml')
def test_config_load_len_with_format_set(self):
self.conf.rpc.load_config = \
MagicMock(return_value='rpc_contents')
self.assertEqual(self.conf.load('test.xml', format='set'),
'rpc_contents')
def test_config_load_len_with_format_xml(self):
self.conf.rpc.load_config = \
MagicMock(return_value='rpc_contents')
xmldata = """<snmp>
<community>
<name>iBGP</name>
</community>
</snmp>"""
self.assertEqual(self.conf.load(xmldata, format='xml'),
'rpc_contents')
def test_config_load_len_with_format_text(self):
self.conf.rpc.load_config = \
MagicMock(return_value='rpc_contents')
示例12: TestConfig
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
class TestConfig(unittest.TestCase):
def setUp(self):
self.dev = Device(host='1.1.1.1')
self.conf = Config(self.dev)
def test_config_constructor(self):
self.assertTrue(isinstance(self.conf._dev, Device))
def test_config_confirm(self):
self.conf.rpc.commit_configuration = MagicMock()
self.assertTrue(self.conf.commit(confirm=True))
def test_config_commit_confirm_timeout(self):
self.conf.rpc.commit_configuration = MagicMock()
self.conf.commit(confirm=10)
self.conf.rpc.commit_configuration\
.assert_called_with(**{'confirm-timeout': '10', 'confirmed': True})
def test_config_commit_comment(self):
self.conf.rpc.commit_configuration = MagicMock()
self.conf.commit(comment='Test')
self.conf.rpc.commit_configuration.assert_called_with(log='Test')
@patch('jnpr.junos.utils.config.JXML.remove_namespaces')
def test_config_commit_exception(self, mock_jxml):
class MyException(Exception):
xml = 'test'
self.conf.rpc.commit_configuration = \
MagicMock(side_effect=MyException)
self.assertRaises(AttributeError, self.conf.commit)
def test_config_commit_exception_RpcError(self):
ex = RpcError(rsp='ok')
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex)
self.assertTrue(self.conf.commit())
import xml.etree.ElementTree as ET
xmldata = """<data><company name="Juniper">
<code>pyez</code>
<year>2013</year>
</company></data>"""
root = ET.fromstring(xmldata)
el = root.find('company')
ex = RpcError(rsp=el)
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex)
self.assertRaises(CommitError, self.conf.commit)
def test_commit_check(self):
self.conf.rpc.commit_configuration = MagicMock()
self.assertTrue(self.conf.commit_check())
@patch('jnpr.junos.utils.config.JXML.rpc_error')
def test_commit_check_exception(self, mock_jxml):
class MyException(Exception):
xml = 'test'
self.conf.rpc.commit_configuration = MagicMock(side_effect=MyException)
# with self.assertRaises(AttributeError):
self.conf.commit_check()
def test_config_commit_check_exception_RpcError(self):
ex = RpcError(rsp='ok')
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex)
self.assertTrue(self.conf.commit_check())
import xml.etree.ElementTree as ET
xmldata = """<data><company name="Juniper">
<code>pyez</code>
<year>2013</year>
</company></data>"""
root = ET.fromstring(xmldata)
el = root.find('company')
ex = RpcError(rsp=el)
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex)
self.assertRaises(CommitError, self.conf.commit_check)
def test_config_diff(self):
self.conf.rpc.get_configuration = MagicMock()
self.conf.diff()
self.conf.rpc.get_configuration.\
assert_called_with({'compare': 'rollback', 'rollback': '0', 'format': 'text'})
def test_config_pdiff(self):
self.conf.diff = MagicMock(return_value='Stuff')
self.conf.pdiff()
print self.conf.diff.call_args
self.conf.diff.assert_called_once_with(0)
def test_config_load(self):
self.assertRaises(RuntimeError, self.conf.load)
def test_config_load_vargs_len(self):
self.assertRaises(RuntimeError, self.conf.load,
'test.xml')
def test_config_load_len_with_format(self):
self.conf.rpc.load_config = \
MagicMock(return_value='rpc_contents')
self.assertEqual(self.conf.load('test.xml', format='set'),
'rpc_contents')
@patch('__builtin__.open')
def test_config_load_lformat_byext_ValueError(self, mock_open):
#.........这里部分代码省略.........
示例13: Device
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
#! /usr/bin/env python
from jnpr.junos.device import Device
from jnpr.junos.utils.config import Config
dev = Device(host='20.0.2.31',user='admin',password='ktcloud00')
dev.open()
cu = Config(dev)
print cu.load(path="/home/hans/git/juniper/juniper/manual_file.set", format='set')
print cu.pdiff()
dev.close()
示例14: load
# 需要导入模块: from jnpr.junos.utils.config import Config [as 别名]
# 或者: from jnpr.junos.utils.config.Config import pdiff [as 别名]
After each load(), display the differences between the running config and the candidate config.
Additionally, perform at least one commit and one rollback(0) in this program.
The committed hostname at the end of the program should be: pynet-jnpr-srx1
'''
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
a_device = Device(host='184.105.247.76', user='pyclass', password='88newclass')
a_device.open()
cfg = Config(a_device)
cfg.load("set system host-name foo", format="set", merge=True)
print "Hostname Change using 'set' format:"
print cfg.pdiff()
print "*" * 80
print "Rollback candidate config"
cfg.rollback(0)
print cfg.pdiff()
print "*" * 80
cfg.load(path="exercise4.conf", format="text", merge=True)
print "Hostname Change using 'text' format:"
print cfg.pdiff()
print "*" * 80
print "Committing hostname change."
cfg.commit(comment="Changing hostname to bar")
cfg.load(path="exercise4.xml", format="xml", merge=True)
print "Hostname Change using 'xml' format:"