He tuhinga mo te mahi tahi me Junos PyEZ - "Python microframework e taea ai e koe te whakahaere me te whakaaunoa i nga taputapu whakahaere Junos OS" aunoatanga me te whakahaere, nga mea katoa e arohaina ana e matou. He maha nga whainga o te tuhi i te tuhinga tuhi i roto i tenei tuhinga - te ako i te Python me te mahi aunoa mo te kohikohi korero me te whakarereke i nga whirihoranga mo nga taputapu e whakahaere ana i te Junos OS. Ko te whiriwhiringa o tenei huinga motuhake o Python + Junos PyEZ i mahia na te iti o te arai ki te uru atu ki te reo kaupapa Python me te ngawari o te whakamahi i te whare pukapuka Junos PyEZ, kaore e hiahiatia he mohiotanga tohunga mo Junos OS.
Whāinga
Te arotake o nga ipurangiroto ipv4 kore utu na te kamupene. Ko te paearu he kore utu he kupenga-roto ko te kore urunga e pa ana ki a ia i roto i nga huarahi i runga i te whakawhiti hei pouara e whakahaere ana i a Junos OS.
Реализация
Python + Junos PyEZ, ahakoa he whakamatautau ki te mahi ma te paramiko me te ssh.exec_command,
Ko te whakauru i te putanga o Junos PyEZ mai i te PyPI ka mahia me te whakahau e whai ake nei:
$ pip install junos-eznc
Ka taea hoki e koe te whakauru mai i te peka matua o te kaupapa i runga i GitHub me te whakahau e whai ake nei:
$ pip install git+https://github.com/Juniper/py-junos-eznc.git
Na tetahi atu kōwhiringa mā
$ pip install -r requirements.txt
Ma tenei whakahau ka whakauru i nga whare pukapuka e ngaro ana i te punaha, e tika ana mo te mahi. I taku putanga whakaritenga.txt E rua noa iho, ko nga putanga hou e tohuhia ana i te wa i tuhia ai te tuhinga:
junos-eznc
netaddr
Ma te taunoa, ka tangohia e te tuhinga te ingoa o te kaiwhakamahi o naianei kei roto i te punaha; ka taea e koe te takiuru i raro i te ingoa o tetahi atu kaiwhakamahi ma te whakamahi i te tohu matua show_route.py -u getpass.getpass ka whakaae ki te kupuhipa mai i stdin kia kore ai te kupuhipa noho tonu i roto i te punaha. Hei hono atu ki te taputapu, me whakauru ano koe ki tona ingoa kaihautu, IP IP ranei ina akiakihia. Ko nga raraunga katoa e tika ana mo te whakamana i runga i te taputapu kua riro mai.
Ka tautoko a Junos PyEZ i te hono ki nga taputapu e whakahaere ana i a Junos OS ma te whakamahi i te papatohu, telnet, te netconf ranei ma te ssh. Ko te tuhinga e korero ana mo te waahanga whakamutunga.
Hei hono atu ki nga taputapu, whakamahia te akomanga Pūrere o te kōwae jnpr.junos
with jnpr.junos.Device(host=router,
user=args.name,
passwd=password) as dev:
Ka tukuna he tono mo nga huarahi katoa e mohiotia ana e te pouara ma te waea tikanga mamao, ma te waea tikanga mamao ranei, ahakoa he pai ake.
data = dev.rpc.get_route_information()
He rite te whakahau mo Junos OS
user@router> show route | display xml
Ma te taapiri i te rpc ki te mutunga o te whakahau, ka whiwhi tatou i te tohu tono ka taea te whakarite ki te ingoa o te tikanga RPC, ma tenei ka kitea etahi atu ingoa e pa ana. Me mohio he rereke te wetereo mo te tuhi i te tohu tono mai i te ingoa tikanga, ara, me whakakapi e koe nga tohuhono ki nga tohu.
user@router> show route | display xml rpc
<rpc-reply >route_list = data.xpath("//rt-destination/text()")
Ko te toenga o te waahanga i takai ki roto i tetahi wa poto, kia kore ai e whakahoki ano te tono ki te pouara mena e tika ana ki te tirotiro i tetahi atu iraroto mai i nga mea kua mohiohia e te pouara. He mea tika kia whakahuahia ko te pouara e tono nei au e mohio ana ki nga huarahi ma te OSPF anake, no reira mo te pouara taha he pai ake te whakarereke i te tono kia iti ake te wa o te tuhi.
data = dev.rpc.get_ospf_route_information()
Inaianei ka titiro tatou ki nga mea o roto o te whiro
I te timatanga, ka tonohia te kaiwhakamahi ki te whakauru i tetahi kupenga-roto me te kanohi kanohi me te kore e neke atu i te toru octets mai i te whatunga o te kupenga-roto kotahi, he mea tika tenei ki te whakarite i te awhe rapu. Kaore au i te tino pai ki tenei whakatinanatanga o te tohu i nga paearu me te awhe rapu, engari i tenei wa kaore ano kia kitea he otinga pai ake. Panuku, mai i te rarangi hua o nga kupengaroto route_list, ma te whakamahi i tetahi taurangi kei roto kaua e neke ake i te toru octets, ka tohua e au nga kupengaroto e pai ana ki ahau.
tmp = re.search(r'^%sS*' % subnet_search, route_list[i])
Na roto i te IPNetwork, te kōwae netaddr, ka whiwhi au i nga kupengaroto i te ahua o te rarangi o nga wahitau ipv4
range_subnet = netaddr.IPNetwork(tmp.group(0))
Ma te whakamahi i te IPNetwork, ka whiwhi au i te whānuitanga o nga wahitau mai i te whatunga kua uru mai e te kaiwhakamahi me te kanohi kanohi me te whakaputa i te rarangi o nga wahitau katoa mai i tenei awhe hei whakataurite ki te rarangi o nga wahitau kua nohoia.
for i in set(net_list).difference(set(busyip)):
freeip.append(i)
Ka whakaatu ahau i te rarangi hua o nga wahitau kore utu i te ahua o nga kupengaroto
print(netaddr.IPSet(freeip))
Kei raro nei te tuhinga katoa, i whakamatauria ki nga huringa hei pouara, tauira ex4550, ex4600
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import getpass
import netaddr
import re
import sys
import jnpr.junos
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user',
action='store',
dest='name',
help='Enter login from tacacs if it differs from the '
'username in the system.')
args = parser.parse_args()
if not args.name:
args.name = getpass.getuser() # Return the “login name” of the user.
router = input("Full routers name: ")
password = getpass.getpass("Password: ")
try:
# Authenticates to a device running Junos, for get information about routs
# into xml format and selects by tag.
route_list = []
with jnpr.junos.Device(host=router,
user=args.name,
passwd=password) as dev:
data = dev.rpc.get_route_information()
route_list = data.xpath("//rt-destination/text()")
except (jnpr.junos.exception.ConnectRefusedError,
jnpr.junos.exception.ConnectUnknownHostError) as err:
print("Equipment name or password wrong.")
sys.exit(1)
while True:
subnet = input("Net with mask: ")
subnet_search = input("Input no more three octet: ")
# Gets a list of busy IP addresses from the received subnets.
busyip = []
for i in range(len(route_list)):
tmp = re.search(r'^%sS*' % subnet_search, route_list[i])
if tmp:
range_subnet = netaddr.IPNetwork(tmp.group(0))
for ip in range_subnet:
busyip.append("%s" % ip)
range_subnet = netaddr.IPNetwork(subnet)
# Gets list ip adresses from subnetworks lists.
net_list = []
for ip in range_subnet:
net_list.append("%s" % ip)
# Сomparing lists.
freeip = []
for i in set(net_list).difference(set(busyip)):
freeip.append(i)
print(netaddr.IPSet(freeip))
request = input("To run request again enter yes or y, "
"press 'enter', complete request: ")
if request in ("yes", "y"):
continue
else:
print('Bye')
break
Source: will.com