本文整理匯總了Python中jnpr.junos.Device方法的典型用法代碼示例。如果您正苦於以下問題:Python junos.Device方法的具體用法?Python junos.Device怎麽用?Python junos.Device使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jnpr.junos
的用法示例。
在下文中一共展示了junos.Device方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
"""Connect to Juniper device using PyEZ. Display device facts."""
pwd = getpass()
try:
ip_addr = raw_input("Enter Juniper SRX IP: ")
except NameError:
ip_addr = input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print("\n\nConnecting to Juniper SRX...\n")
a_device = Device(**juniper_srx)
a_device.open()
pprint(a_device.facts)
print()
示例2: configure_device
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def configure_device(connection_params, variables):
device_connection = Device(**connection_params)
device_connection.open()
# device_connection.facts_refresh()
facts = device_connection.facts
hostname = facts['hostname']
config_variables = variables['devices'][hostname]
with Config(device_connection, mode='private') as config:
config.load(template_path='templates/candidate.conf', template_vars=config_variables, merge=True)
print("Config diff:")
config.pdiff()
config.commit()
print(f'Configuration was updated successfully on {hostname}')
device_connection.close()
示例3: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
'''
Connect to Juniper device using PyEZ. Display device facts.
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()
pprint(a_device.facts)
print
示例4: __init__
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def __init__(self,firewall_config):
self.firewall_config = firewall_config
try:
assert self.firewall_config['privatekey']
assert self.firewall_config['privatekeypass']
except:
#User password connection
logger.info("Juniper User/Password connection.")
self.dev = Device(host=self.firewall_config['primary'], password=self.firewall_config['pass'],\
user=self.firewall_config['user'], port=self.firewall_config['port'], gather_facts=False)
else:
#RSA SSH connection
logger.info("Juniper RSA SSH connection.")
self.dev = Device(host=self.firewall_config['primary'], passwd=self.firewall_config['privatekeypass'],\
ssh_private_key_file=self.firewall_config['privatekey'],user=self.firewall_config['user'],\
port=self.firewall_config['port'], gather_facts=False)
self.dev.open(normalize=True)
try:
self.dev.timeout = int(self.firewall_config['timeout']) if self.firewall_config['timeout'] else 15
except (ValueError, KeyError):
logger.warning("Firewall timeout is not an int, setting default value.")
self.dev.timeout = 15
self.primary = self.firewall_config['primary']
示例5: myfunc
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def myfunc(*args):
devices = args[0]["devices"]
username = args[0]["username"][0]
secret = args[0]["password"][0]
hosts = args[0]["hosts"]
arpinfo = {}
for device in devices:
dev, port = device.split(":")
handle = Device(host=dev, port=port, user=username, passwd=secret)
handle.open()
devname = handle.rpc.get_system_information().find('host-name').text
for host in hosts:
arptable = handle.rpc.get_arp_table_information(hostname=str(host))
for intname in arptable.findall("./arp-table-entry/interface-name"):
if devname in arpinfo:
arpinfo[devname].append(intname.text.strip())
else:
arpinfo[devname] = [intname.text.strip()]
out(arpinfo)
示例6: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
"""Use Juniper PyEZ and direct RPC to retrieve the XML for 'show version' from the SRX."""
pwd = getpass()
try:
ip_addr = raw_input("Enter Juniper SRX IP: ")
except NameError:
ip_addr = input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print("\n\nConnecting to Juniper SRX...\n")
a_device = Device(**juniper_srx)
a_device.open()
# show version | display xml rpc
# get-software-information
show_version = a_device.rpc.get_software_information()
print()
print("Print show version XML out as a string (retrieved via PyEZ RPC):")
print("-" * 20)
print(etree.tostring(show_version, pretty_print=True).decode())
model = show_version.xpath("product-model")[0].text
print()
print("-" * 20)
print("SRX Model: {}".format(model))
print()
示例7: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
'''
Connect to Juniper device using PyEZ. Display the routing table.
'''
pwd = getpass()
try:
ip_addr = raw_input("Enter Juniper SRX IP: ")
except NameError:
ip_addr = input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print("\n\nConnecting to Juniper SRX...\n")
a_device = Device(**juniper_srx)
a_device.open()
routes = RouteTable(a_device)
routes.get()
print("\nJuniper SRX Routing Table: ")
for a_route, route_attr in routes.items():
print("\n" + a_route)
for attr_desc, attr_value in route_attr:
print(" {} {}".format(attr_desc, attr_value))
print("\n")
示例8: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
'''
Connect to Juniper device using PyEZ. Display operational state and pkts_in, pkts_out for all
of the interfaces.
'''
pwd = getpass()
try:
ip_addr = raw_input("Enter Juniper SRX IP: ")
except NameError:
ip_addr = input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print("\n\nConnecting to Juniper SRX...\n")
a_device = Device(**juniper_srx)
a_device.open()
eth_ports = EthPortTable(a_device)
eth_ports.get()
print("{:>15} {:>12} {:>12} {:>12}".format("INTF", "OPER STATE", "IN PACKETS", "OUT PACKETS"))
for intf, eth_stats in eth_ports.items():
eth_stats = dict(eth_stats)
oper_state = eth_stats['oper']
pkts_in = eth_stats['rx_packets']
pkts_out = eth_stats['tx_packets']
print("{:>15} {:>12} {:>12} {:>12}".format(intf, oper_state, pkts_in, pkts_out))
print()
示例9: do_something
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def do_something(params):
device_connection = Device(**params)
device_connection.open()
# device_connection.facts_refresh()
facts = device_connection.facts
# response_xml_element = device_connection.rpc.get_software_information(normalize=True)
# print(etree.tostring(response_xml_element))
# pprint(facts)
print("{0} {1} {0}".format('=' * 37, facts['hostname']))
routing_table = RouteTable(device_connection)
routing_table.get()
print("RIB:")
for prefix in routing_table:
print(f"Destination : {prefix.key}\n"
f"Via: {prefix.via}\n"
f"Nexthop: {prefix.nexthop}\n"
f"Protocol: {prefix.protocol}\n\n")
print("{}".format('=' * 80))
arp_table = ArpTable(device_connection)
arp_table.get()
print("ARP table:")
for entry in arp_table:
print(f"IP : {entry.ip_address}\n"
f"MAC: {entry.mac_address}\n"
f"Interface: {entry.interface_name}\n\n")
device_connection.close()
# return facts
示例10: enable_interface
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def enable_interface(int, **kwargs):
junos_details = get_junos_details(kwargs['device_id'])
junos_host = junos_details['host']
junos_user = junos_details['authentication']['password']['username']
# junos_password = junos_details['authentication']['password']['password']
junos_password = 'Juniper!1'
device=Device(host=junos_host, user=junos_user, password=junos_password)
device.open()
cfg=Config(device)
my_template = Template('delete interfaces {{ interface }} disable')
cfg.load(my_template.render(interface = int), format='set')
cfg.commit()
device.close()
示例11: check_connected
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def check_connected(device):
print("\n\n")
if device.connected:
print(f"Device {device.hostname} is connected!")
else:
print(f"Device {device.hostname} failed to connect :(.")
# If device is *not* connected; exit script
sys.exit(1)
示例12: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
'''
Connect to Juniper device using PyEZ. Display the routing table.
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()
routes = RouteTable(a_device)
routes.get()
print "\nJuniper SRX Routing Table: "
for a_route, route_attr in routes.items():
print "\n" + a_route
for attr_desc, attr_value in route_attr:
print " {} {}".format(attr_desc, attr_value)
print "\n"
示例13: main
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def main():
'''
Connect to Juniper device using PyEZ. Display operational state and pkts_in, pkts_out for all
of the interfaces.
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()
juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}
print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()
eth_ports = EthPortTable(a_device)
eth_ports.get()
print "{:>15} {:>12} {:>12} {:>12}".format("INTF", "OPER STATE", "IN PACKETS", "OUT PACKETS")
for intf, eth_stats in eth_ports.items():
eth_stats = dict(eth_stats)
oper_state = eth_stats['oper']
pkts_in = eth_stats['rx_packets']
pkts_out = eth_stats['tx_packets']
print "{:>15} {:>12} {:>12} {:>12}".format(intf, oper_state, pkts_in, pkts_out)
print
示例14: setUp
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def setUp(self):
self.dev = Device(
host='127.0.0.1', user='root', password='Juniper', port='2222'
)
self.dev.open()
示例15: mocked_device
# 需要導入模塊: from jnpr import junos [as 別名]
# 或者: from jnpr.junos import Device [as 別名]
def mocked_device(rpc_reply_dict, mock_connect):
"""Juniper PyEZ Device Fixture"""
def mock_manager(*args, **kwargs):
if 'device_params' in kwargs:
# open connection
device_params = kwargs['device_params']
device_handler = make_device_handler(device_params)
session = SSHSession(device_handler)
return Manager(session, device_handler)
elif args:
# rpc request
rpc_request = args[0].tag
rpc_command = str(args[0].text)
rpc_command = rpc_command.strip()
rpc_command = rpc_command.replace(" ", "_")
if rpc_request in rpc_reply_dict:
xml = rpc_reply_dict[rpc_request]
elif 'dir' in rpc_reply_dict:
fname = os.path.join(rpc_reply_dict['dir'], 'rpc-reply', rpc_command, rpc_request + '.xml')
with open(fname, 'r') as f:
xml = f.read()
else:
_rpc_reply_dict['dir']
fname = os.path.join(os.path.dirname(__file__), 'rpc-reply', rpc_command, rpc_request + '.xml')
with open(fname, 'r') as f:
xml = f.read()
rpc_reply = NCElement(xml, dev._conn._device_handler.transform_reply())
return rpc_reply
mock_connect.side_effect = mock_manager
dev = Device(host='1.1.1.1', user='juniper', gather_facts=False)
dev.open()
dev._conn.rpc = MagicMock(side_effect=mock_manager)
dev.close = MagicMock()
return dev