JANOG | JApan Network Operators' Group...file2.write("Good!") file2.close() $ python3 sample_file.py...
Transcript of JANOG | JApan Network Operators' Group...file2.write("Good!") file2.close() $ python3 sample_file.py...
-
-01,1/ 2 1 8 0 C A ,
. / A 51 : 1AC 8 H
-
• e ae rOtx p
s p b i• – Tlg jn y Ou
J 02A: :C 99 : A: CA:C A C9
• Oweb S h U ob i
• 979H c e ib mU i
• cg n U mi UWn c
-A IC : / 1-.4 98 A 2A A , 9C 8 / 8
https://www.janog.gr.jp/meeting/janog41/program/autoprep
-
• BCB ABP• . J J
• HE LBHG 0G@BG J . . , JB ) 8J GL • y U T cspSa w• i kS St hegvS wlr• ms Sa wclg mugd m
• LOHJD 0G@BG J - 1 7-0 ( ) • /. je nSw 8 JBG@• hegvS a o / a o
• .H GBLP , LBNBLB• 4, 71 mu ro• L7I .H BG@
.HIPJB@AL 7.2 AGH HE LBHG , JB G
-
()
• SH i ea C• S O T p C
• T SII rt C• l i T C
• norS S nmH C• nosS nm u C• ca n I T S
S h gT y C• h gT
U norS S C
: A ,/- 0 /32 : .: : 83 21 , 2
-
8 42 - . -10 8 ,8 48 1 40 0
. . .
. . .
. . .
. . .
-
( )••••
- 2• mp• U S r• e h I• s U t
- 2• mp C
• mp• Cl• s
• e h uT• ianmph• U
( )•• ~
•
( )••••
- 2• Cl O H• Co
c gl• T S
T yT• Ac gl
I53 ., .21 8 - 5 8 2 510 81
-
• u o cn s l SN hO k gi gc k• a p pwo uo• b a011 m• r y
• b i e c y ,: bAC 8: 6 : : : / : 2 :
• c kI c b TIc gc k T
• c ca N ON ON OS Ti Uj a
• N c kON c ON S ON U hOT
• t r TiU aAG :8 H .20 - 2 6 1 : C :6 . 6
https://github.com/taijiji/NetworkAutomationTutorial
-
• L S S y• l e L 0 - A E A H 1 0 0
• nr R OU S y y• aLi S y y• h L S y ,G y• L L SsMUy y• m S L TS glctuxo• L S• L S y
• 0/ / A E PSp R R g
ABHC E I / 2 .7 A A E A D C 8 /
-
& vh C O I
4 A 9 iloT ( T O GU) OT s E gE gtET GU
nrle5 8 1 9 88 9 T O GU U T T O GU
, N h E I- O. gE & E
2 A l E 7 A 68 A 03 E
-NANOG71:Network Automation: past, present, and future ( Slide, Movie)
?
https://pc.nanog.org/static/published/meetings/NANOG71/1441/20171002_Ulinic_Network_Automation_Past__v1.pdfhttps://youtu.be/aQFbSovedIE
-
89 2 - . -10 8 ,8 8 1 0 0
https://speakerdeck.com/opelab/20171212-automation
https://speakerdeck.com/opelab/20171212-automation
-
G ir n t / 4 6 S J• / 4 6
• 6• o ••• g p•• n
• / 4 6• n hno• 0.• ti t t•• e
• f s J .00n hno p• O NPAf s
• ir n B p Jr t :4 t S• ir n B p J tg
• tip( / /11 63 wS
) .
/
y tn t
-
( )
• > C >• r U pmocH2 4lnaesU u U• hH cH ) u U U
u OW OTI U• rjHo t bna y gpiH
STy OTI O
> 8 ( -0. ,1 0 8 > > C > A -
https://github.com/mnagaku/janog41-auto-taiji
-
• b O PhU ehUp p O ShU
•2 m r rn h ub OI
b cS • y o lcT
b n h oOt i l O
H Sg.CA8 l K ca
p p ©OI W i g-, US b n b a cs a U b
) C 8A 0-) 1 0 8 A (
-
• 2 C > ( )
SH aH• 2 C >
p IssI emgt H• r nrs 2 C > SPy S hI aH
u r nrs y • r nrs 2 C > ioIl
P U emgt aH
2 C > , cP T c 2 C > OUU UH
. A C 041. 487 > 3 C > - 8A 76 0>7
-
m CA8 ic so OH
(, C 8A .2 ,-3 26 8 1 A 6 4 .
$ python –-version
Python 2.7.14
$ python2 --version
Python 2.7.14
$ python3 --version
Python 3.6.3
ae h pt0CA8 I gi
1T P S
1 uT PCA8 CA8 CA8 )
U icae h rn l H
-
• 57 c efUi• : H IPM lk
• C M/ PPP CIH I IPH I M• 4 57 57 lk
• C M/ I MI M )).- . (A( . ) ,
• 1 H 57 , lk• C M/ I M I M . )
-A . ..,, (•gUdh XW qawb lk Sutsp6082 oms XrnT
1I C - 385129 8 CHI 7I O IHM 0 3H
https://www.python.org/downloads/https://qiita.com/ms-rock/items/6e4498a5963f3d9c4a67https://qiita.com/estaro/items/5a9e4eb8f0902b9977e3
-
•io ml• u C g n H• r u T p pe I ch S yH• io ml S r u }O U
H
(- C 8A ) 20-. 26 8 1 : A ,;6 4
$ vi sample.py
num = 1if num == 1:
print(“Hello JANOG!!!”)
$ python3 sample.py
Hello JANOG!!!
•io ml• s CA8 a p S CA8 g n {t O H
I
-
: A ., .21 : -:7 : 82 10 1
•• S
• S• a• H• U
• T c• : C•I O• S•
-
8 2 , - ,10 8 8 8 1 0 0
$ python3 sample_print.py
Hello JANOG!
A C
A C HI
print("Hello JANOG!")1.
A C HI
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_print.py
-
= C 9 8A ., . 8 = -= A9=9
a = 1 # a: intb = 1.1 # b: floatprint(a)print(b)print(‘-----’)c = a + b # c: floatprint(c)
$ python3 sample_number.py
11.1-----2.1
I H I H O
0 S1 T U
(C int a; float b; )
( )
2 C
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_number.py
-
A= C - . - , 8 = 2
c = “text” # ‘ “d = " "##Python3 Unicode#Python2 ASCII u" "
print(c)print(d)
$ python3 sample_string.py
text
2 8 1 = A
O 0 S TUH I
H
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_string.py
-
A= - . - , 8 = 2
router_list = ["router_A", "router_B", "router_C"]
# print(router_list)
# 0 1print(router_list[1])
# (len)print(len(router_list))
$ python3 sample_list.py
['router_A', 'router_B', 'router_C']router_B3
2 8 18 A
H I O
0C 0
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_list.py
-
= C 8A ., . 8 = -=: A =
$ python3 sample_dictionary.py
router_A15.1
O S
0 1TU I I H
# key valuerouter_info = {
"hostname" : "router_A","os" : "junos","version" : "15.1",
}
# print(router_info["hostname"]) # print(router_info["version"])
: 2 A = C C
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_dictionary.py
-
A . .32 - 8 3 21 2
$ python3 sample_if.py
num is two
num = 2
if num == 1:print("num is one")
elif num == 2:print("num is two")
else:print("num is the others")
1 830 A
U T c
,A PCmU T Tai 21 3 PleH_
f O gI Ch Sfn
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_if.py
-
34 ., .2148 - 8 2 10 81
H IA C O U
# router_list = [”router_A”, ”router_B”]
# router_list router_namefor router_name in router_list:
print(router_name)
$ python3 sample_for_2.py
router_Arouter_B
# # range(3) = 0,1,2for num in range(3):
print(num)
$ python3 sample_for_1.py
012
H IA TS O Usample_for_1.py
sample_for_2.py
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_for_1.pyhttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_for_2.py
-
C HA A
C A
# :"r"file1 = open("sample_read.txt", "r")text = file1.read() # print(text)file1.close()
# :"w", :"a"file2 = open("sample_write.txt", "w") file2.write("Good!")file2.close()
$ python3 sample_file.py
Hello JANOG! from sample_read.txtHow are you feeling?
sample_file.py
[ : sample_read.txt]
Hello JANOG! from sample_read.txtHow are you feeling?
[ : sample_write.txt]
Good!
C HA A
C A
8 2 - . -10 8 ,85 8 1 0 0
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_file.pyhttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_read.txthttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_write.txt
-
(- CI = ) 0-.4 :8=A 2 A , : 8 A8
:
: A
# ()
6
• t y ] ] U [• 1H chnf[ Tat• m g02 SUe ph Tat
• iodl [ r v t sTu[iodl T O Pa
-
A C A C HI# # # OS show bgp summarydef get_show_bgp_summary(router_info):
if router_info["os"] == "junos":command = "show bgp summary"
elif router_info['os'] == 'ios':command = "show ip bgp summary"
else:command = “N/A”
return command
# router_info = {
"hostname" : "router_A","os" : "junos"
}# router_infocommand = get_show_bgp_summary(router_info)print(command)
$ python3 sample_function.py
show bgp summary
sample_function.py
8 2 - . -10 78 ,8 87 1 0 70
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_function.py
-
• ni di coO s =• a a s O l S[ =
co f O =•
• ni ) : 2 (P• di efI rhf ]a e f
,
8
= () # . # . () #
-
89 2 - . -10 8 ,8 8 1 0 0
class Router:# : def __init__(self, hostname, os, version):
# : self.hostname = hostnameself.os = osself.version = version
# show bgp summarydef get_show_bgp_summary(self):
if self.os == "junos":command = "show bgp summary"
elif self.os == 'ios':command = "show ip bgp summary"
else:command = 'N/A’
return command
# Routerrouter_A = Router(hostname="router_A", os="junos", version="15.1")router_B = Router(hostname="router_B", os="ios", version="15.7(3)M")
# Routerprint(router_A.hostname)command = router_A.get_show_bgp_summary()print(command)
print(router_B.hostname)command = router_B.get_show_bgp_summary()print(command)
$ python3.6 sample_class.py
router_Ashow bgp summaryrouter_Bshow ip bgp summary
A C A C HIsample_class.py
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_class.py
-
A - 0 32 . 83 21 2
• N C• .-, SC•JUOT U UC• C• I JH C
-
• m hmn•cg odO d oI Um hmn• 20 3 3 022 3 :
• tm hmn• jle T I m hmn• am hmn d oH• : 0 2 0: :0 08
A ., .31 : - 8 : 3 10 :1
yr T usI ip mU S C
-
A ( , - 0 32 . 8 ) 3 21 , 2
$ pip3 list
Jinja2 (2.9.6). . .
$ pip3 install jinja2
S H HOI C1 H HOI C
HOI C T H U
( ) venv virtualenvpip
( )https://docs.python.jp/3/library/venv.html
https://docs.python.jp/3/library/venv.html
-
$ pip --version
pip 9.0.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)
$ pip2 --version
pip 9.0.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)
$ pip3 --version
pip 9.0.1 from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (python 3.6)
l Uac
n, 8 ac ph
-r S PCA A A AO c Hsm gI Ue T
( ) : pip Python setuptools pip
http://www.lifewithpython.com/2012/11/Python-package-setuptools-pip.html
8 cito
3 . .21 8 - 8 2 10 81
http://www.lifewithpython.com/2012/11/Python-package-setuptools-pip.html
-
4 A ., .31 - 83 10 1
# datetimeimport datetime
# datetime datetime nownow = datetime.datetime.now()print(now)
$ python3 sample_library.py
2017-11-15 14:25:56.360603
sample_library.py20 3 83H HI C O
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_library.py
-
A M )I L 5 )
5 3( L I P )
: JSONhttps://thinkit.co.jp/article/70/1
https://thinkit.co.jp/article/70/1
-
(8 ) -30 , 3 6 2 C A 6 - 6
[{"router_name": "Router_A","ip": "192.168.0.1","os": "junos"
},{"router_name": "Router_B","ip": "192.168.0.2","os": "ios"
},{"router_name": "Router_C","ip": "192.168.0.3","os": "iosxr"
}]
$ python3 sample_json.py
[{'router_name': 'Router_A', 'ip': '192.168.0.1', 'os': 'junos'}, {'router_name': 'Router_B', 'ip': '192.168.0.2', 'os': 'ios'}, {'router_name': 'Router_C', 'ip': '192.168.0.3', 'os': 'iosxr'}]-----192.168.0.1
.20 c SHN OJ N TU
.A S P P 1 hHNe O ag iIJ U g h
sample_json.json import json
file = open("sample_json.json", "r")
# file jsonrouters_list = json.load(file)
print(routers_list)print(”-----")print(routers_list[0]["ip"])
sample_json.py
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_json.jsonhttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_json.py
-
J J a 7J P) J 37 )( 2
{{ title }}
{{ body }}
title = “Web page 1”
body = “Hello! World!”
{{ }} :
{% if %} : if{% endif %}
{% for in %} : for{% endfor %}
{# #} :
[(Jinja2)]
HTML
-
(C ( ,0. 1 0 3 A ) 32 , 3
interface {{ if_name }}description {{ if_description }}ip address {{ ip4 }} {{ ip4_subnet }}duplex autospeed autono shutdown
!
H O IJ - 82 UT import jinja2
# file = open("sample_template.jinja2", "r")template_txt = file.read()
# template = \jinja2.Environment().from_string(template_txt)
# config_txt = template.render(
if_description="To_RouterA",if_name="fastethernet 1/1",ip4=“192.168.0.1”,ip4_subnet="255.255.255.0")
print(config_txt)
$ python3 sample_template_1.py
interface fastethernet 1/1description To_RouterAip address 192.168.0.1 255.255.255.0duplex autospeed autono shutdown
!
sample_template.jinja2 sample_template_1.py
S
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_template.jinja2https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_template_1.py
-
( ( ( )interface {{ if_name }}description {{ if_description }}ip address {{ ip4 }} {{ ip4_subnet }}duplex autospeed autono shutdown
!
3( ) 2
interfaces = [{
"if_description" : "To_RouterA","if_name" : "fastethernet 1/1","ip4” : "192.168.0.1","ip4_subnet” : "255.255.255.0"
},{
"if_description" : "To_RouterB","if_name" : "fastethernet 1/2","ip4" : "192.168.1.1","ip4_subnet" : "255.255.255.0"
},{
"if_description" : "To_RouterC","if_name" : "fastethernet 1/3”,"ip4" : "192.168.2.1","ip4_subnet" : "255.255.255.0"
}]
for interface in interfaces:config_txt = template.render(interface)print(config_txt)
$ python3 sample_template_2.py
interface fastethernet 1/1description To_RouterAip address 192.168.0.1 255.255.255.0duplex autospeed autono shutdown
!interface fastethernet 1/2description To_RouterBip address 192.168.1.1 255.255.255.0duplex autospeed autono shutdown
!interface fastethernet 1/3description To_RouterCip address 192.168.2.1 255.255.255.0duplex autospeed autono shutdown
!
9
sample_template.jinja2 sample_template_2.py
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_template.jinja2https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_template_2.py
-
40
text = “Today is Sunday”
pettern = ”Today is (.+)”
. :
1
+ :
() :
match = re.search(pettern, text )# match.group(0) # => Today is Sunday# match.group(1) # => Sunday
-
import re
# # show versionshow_version_txt = """Hostname: vsrxModel: firefly-perimeterJUNOS Software Release 12.1X47-D15.4"""
# # () : # . : 1# + : regex = "JUNOS Software Release (.+)"
# show_version_txt# match = re.search(regex, show_version_txt )
# print(match.group(0))print(”--------------------")
# print(match.group(1))
sample_regex.py$ python3 sample_regex.py
JUNOS Software Release 12.1X47-D15.4--------------------12.1X47-D15.4
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_regex.py
-
• t l h s i h r C• I T H h Hi h gi hC• s p si u
• o C• Oh o i S h
s s i h C• h i h y hC
e y i T hC• s p si u
• s p si a ym hC• s p si c O a
rln © U hC
: 4 > A , - 0 2 : .: > : 8 21 , 2
-
def hello(num):if num == 1:
message = “Hello”elif num == 2:
message = “Good bye”else:
message = “No message”
return message
# hello(1)
# ”Hello”
assertEqual(hello(1), “Hello”)
# hello(2)
# “Good bye”
assertEqual(hello(2), “Good bye”)# hello(-1)
# ”No message”
assertEqual(hello(-1), “No message”)# hello(“abc”)
# ”No message”
assertEqual(hello(“abc”), “No message”)
HI CA
34 ., .2148 - 8 2 10 81
-
8 42 - . -10 8 ,8 48 1 40 0
def add(x, y):sum = x + yreturn sum
sample_unittest.py
S OC H OC
import unittestimport sample_unittest
class TestSample(unittest.TestCase):def test_add(self):
actual = sample_unittest.add(1,2)expected = 3self.assertEqual(actual, expected)
if __name__ == "__main__":unittest.main()
tests/test_sample_unittest.py
├── sample_unittest.py└── tests
└── test_sample_unittest.py
IA
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_unittest.pyhttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/tests/test_sample_unittest.py
-
python3 -m unittest tests.test_sample_unittest.----------------------------------------------------------------------Ran 1 test in 0.000s
OK
4 5
python3 -m unittest tests.test_sample_unittestF======================================================================FAIL: test_add (tests.test_sample_unittest.TestSample)----------------------------------------------------------------------Traceback (most recent call last):File ”xxx/tests/test_sample_unittest.py", line 12, in test_addself.assertEqual(expected, actual)
AssertionError: 3 != 4
----------------------------------------------------------------------Ran 1 test in 0.000s
4 5
()
( ) python3 –m unittesttest __init__.py
-
• TroOS LI ED e• 2F D L81
• Uae ciTljOSP LI ED bh W• D E , HA
• agX b cipm b• - I - I.J - I1
• nk i X ci b• 0 DA DH G
EFLG I 2 .6 DE 4E JI EDH C G D
http://openpyxl.readthedocs.io/en/default/https://docs.djangoproject.com/en/2.0/http://flask.pocoo.org/docs/0.12/https://git-scm.com/https://github.com/https://about.gitlab.com/https://jenkins-ci.org/https://circleci.com/
-
/
4 ., .2148 - 8 72 10 81
-
• x sb rg a f EBn up
• ERUNPJ N-NDE M .ELAMD 4ABQ
• l a0 2ar ty o a f vt kh
• -IQCN - 4 2:4 -IQCN b ri oc • 0 ri oc
• l a-42ar a f vt kh
• AGPAMR SMIOEP T : • 0IRHSB AGPAMR SMNQ 0SEQR• w m AGPAMRa IPE Wg e
• AGPAMR -IQCN 2 :T • w m : RNN BNV 8APR 2 : AGPAMR SICJ RAPR• w m 2 :T AGPAMRg d
• AGPAMR +PIQRA T • w m QIMG T UIRH AGPAMR AMD IPRSA ,NV
()-NOWPIGHR X ) 2 -1 ECHMN N SRINMQ +LEPICA 2MC
https://labs.networktocode.com/https://www.cisco.com/c/en/us/products/cloud-systems-management/modeling-labs/index.htmlhttps://learningnetworkstore.cisco.com/virtual-internet-routing-lab-virl/cisco-personal-edition-pe-20-nodes-virl-20http://www.eve-ng.com/https://github.com/JNPRAutomate/vagrant-junoshttps://qiita.com/taijijiji/items/501a4d671106240fbd2chttps://xrdocs.github.io/application-hosting/tutorials/iosxr-vagrant-quickstarthttps://qiita.com/Mabuchin/items/22992f157f301d63b715https://eos.arista.com/using-veos-with-vagrant-and-virtualbox/
-
• e g u
• // 048 4 g -, ia
• h ci o g u
• h ci o t h u
A ,0. 1 04 / 8 94 2 ,
l sL LO u THo o g pn y u yS L m I r U C
-
• 5S 5( (
• ) 5 O 0 (• : (
CiscoRouter
JuniperRouter
AristaRouter
Cisco
Juniper
Arista
-
Manufacturer OS Library Language OS version
Cisco
IOS / IOS-XEOne Platform Kit (OnePK) C, Java, Python ASR1000: 3.12 -3.17(EoL)
YANG Development Kit(YDK) Python, C++ 16.5.1 later
IOS-XROne Platform Kit (OnePK) C, Java, Python ASR900: 5.1.2 - 5.3.3(EoL)ASR9000: 5.1.1 - 5.3.3(EoL)
YANG Development Kit(YDK) Python, C++ 6.0.0 later
NX-OS
One Platform Kit (OnePK) C, Java, Python Nexus9000: all versionNexus6000: - 7.2(EoL)
NX-API Web GUI,HTTP/HTTPS
Nexus9000: all versionNexus7000: 7.2(0)D1(1) laterNesus6000: 7.2(0)N1(1) later
Juniper JUNOSPyEZ Python 11.4 later
Juniper Extension Toolkit(JET) Python 16.2 later
Brocade Network OS PyNOS Python 5.0.1 later
Arista EOSpyeapi, rbeapi, goeapi Python, Ruby, Go 4.1.2 later
EOS SDK C++, Python all version
O , 5 S , 1
https://blogs.cisco.com/security/ciscos-onepk-part-1-introductionhttps://developer.cisco.com/site/ydk/https://blogs.cisco.com/security/ciscos-onepk-part-1-introductionhttps://developer.cisco.com/site/ydk/https://blogs.cisco.com/security/ciscos-onepk-part-1-introductionhttps://supportforums.cisco.com/t5/%E3%83%87%E3%83%BC%E3%82%BF%E3%82%BB%E3%83%B3%E3%82%BF%E3%83%BC-%E3%83%89%E3%82%AD%E3%83%A5%E3%83%A1%E3%83%B3%E3%83%88/nx-os-programmability-nx-api-%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6/ta-p/3159295https://www.juniper.net/documentation/en_US/release-independent/junos-pyez/information-products/pathway-pages/index.htmlhttps://www.juniper.net/documentation/en_US/release-independent/jet/information-products/pathway-pages/index.htmlhttps://pythonhosted.org/pynos/https://eos.arista.com/introducing-the-python-client-for-eapi-pyeapi/https://github.com/arista-eosplus/rbeapihttps://eos.arista.com/an-introduction-to-the-golang-eapi/https://github.com/aristanetworks/EosSdk
-
:/
• L X 33 47 :7 er l• -21 pI I x t• x pX S s c I i T X nH • , / 6 7 76
)
Cisco
Router
SSH/Telnet
show version
(Cisco Command)
Juniper
Router
SSH/Telnet
show version
(Juniper Command)
Hostname: vsrx
Model: firefly-perimeter
JUNOS Software Release [12.1X47-D15.4]
. . .
E s C D E s C 5( . ) (D
Thu Jan 18 02:03:05.771 UTC
Cisco IOS XR Software, Version 6.2.1.23I
Copyright (c) 2013-2016 by Cisco Systems, Inc.
. . .
-
import Exscript # SSH ( :pip3 install exscript)import re #
username = "user1"password = "password1"ip4 = "192.168.33.3"
# SSHsession = Exscript.protocols.SSH2()session.connect(ip4)
# account = Exscript.Account(name=username, password=password)session.login(account)
# session.execute("show version")result = session.responseprint(result)print("----------")
# pattern = "JUNOS Software Release \[(.+)\]"match = re.search(pattern, result)version = match.group(1)print(version)
# SSHsession.send("exit")session.close()
sample_exscript_show.py
$ python3 sample_exscript_show.py
show versionHostname: vsrxModel: firefly-perimeterJUNOS Software Release [12.1X47-D15.4]----------12.1X47-D15.4
3 ()) ,
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_exscript_show.py
-
) (
print(”===== Step 1. run show command =====")session.execute("show configuration interfaces ge-0/0/1")print(Fore.YELLOW + session.response) # Fore.YELLOW :
print("===== Step 2. configure =====")session.execute("configure")
config_txt = "set interfaces ge-0/0/1 disable"session.execute(config_txt)print(Fore.YELLOW + session.response) #
print("===== Step 3. commit check =====")session.execute("show | compare")print(Fore.YELLOW + session.response)session.execute("commit check")print(Fore.YELLOW + session.response)
print("===== Step 4. commit =====")print("Do you commit? y/n") # y or nchoice = input()if choice == "y":
session.execute("commit")print(session.response)
else:session.execute("rollback")print(session.response)
session.execute("exit")print(session.response)
print("===== Step 5. run show command(again) =====")session.execute("show configuration interfaces ge-0/0/1")print(Fore.YELLOW + session.response)
$ python3 sample_exscript_set.py
===== Step 1. run show command =====show configuration interfaces ge-0/0/1unit 0 {
family inet {address 10.0.1.1/24;
}}===== Step 2. configure =====set interfaces ge-0/0/1 disable===== Step 3. commit check =====show | compare[edit interfaces ge-0/0/1]+ disable;commit checkconfiguration check succeeds===== Step 4. commit =====Do you commit? y/nycommitcommit completeexitExiting configuration mode===== Step 5. run show command(again) =====show configuration interfaces ge-0/0/1disable;unit 0 {
family inet {address 10.0.1.1/24;
}}
sample_exscript_set.py( )
: sample_exscript_set.py
, (4 )
5
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_exscript_set.pyhttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_exscript_set.py
-
•5 HE. 1 25, 5 8E•p e• c a l xSmV T nsO • -H , u S MN A CAFE S h
MUrwO o yti gO
)),A IC E ( 05 ,/ 5 A A FE A + C 8 0
https://github.com/google/textfsmhttps://github.com/networktocode/ntc-templateshttps://www.slideshare.net/techblogyahoo/netopscoding-npstudyhttps://www.janog.gr.jp/meeting/janog41/program/lt3
-
• C wsP n L u • i X O n eno
• OmC tP r MN v• v i X A N Ccha SPS• ( ) ) , 6 :
• kC Pn eno • ( 6 3: : 63 . 35 2 : 2 2 : 1
NAPALM
CiscoRouter
JuniperRouter
Cisco
Juniper
get_network_driver(”iosxr")get_facts()[‘os_version’]
12.1X47-D15.4
get_network_driver("junos")get_facts()[‘os_version’]
-
• r y n b laU u8P IH o• J . H J F A AI EI H F EHA O F• J . CE N I H J F # N I EIH HJ F
• ig la RTt m d d Xc S
r n R e r n m• .
• / E 17: ) 2 F 5NHEJ 5 67: ( F0E I 47:# F 6 7: F 47: ( ) ( F
• Tp o Uhe RsTx T n o c a d S• di aR S• . J. H J F A AI EI H F NJJI EHA O F #
H JI # CN H
,0IJP EC ( - 4 70 HI#:IFN EIH / E 4H
https://napalm.readthedocs.io/en/latest/index.htmlhttps://github.com/napalm-automation/napalmhttp://napalm.readthedocs.io/en/latest/support/index.html
-
(5 A ( , - 0 25 . )8 21 , 2
# NAPALM # : pip3 install napalm)import napalmfrom pprint import pprint
# JUNOSdriver = napalm.get_network_driver("junos")device = driver(
hostname="192.168.33.3",username="user1",password="password1" )
# device.open()
# fact = device.get_facts()pprint(fact) # pprint:
print("----------")
# ( )print(fact["os_version"])device.close()
sample_napalm_show.py
$ python3 sample_napalm_show.py
{'fqdn': 'vsrx','hostname': 'vsrx','interface_list': [ 'ge-0/0/0',
'ge-0/0/1','ge-0/0/2',. . . ,'vlan'],
'model': 'FIREFLY-PERIMETER','os_version': '12.1X47-D15.4','serial_number': '83f144ddd4f7','uptime': 8309,'vendor': 'Juniper'}----------12.1X47-D15.4
ICCH
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_napalm_show.py
-
) (. . .
print("===== Step 1. run show command =====")print("Interface Status: ", end="") if device.get_interfaces()["ge-0/0/1"]["is_up"] == True:
status_before = ”UP"elif device.get_interfaces()["ge-0/0/1"]["is_up"] == False:
status_before = ”DOWN"print(Fore.YELLOW + status_before)
print("===== Step 2. configure =====")device.load_merge_candidate(filename="./sample_config_junos.txt")print("OK")
print("===== Step 3. compare configuration =====")print(Fore.YELLOW + device.compare_config())
print("===== Step 4. commit =====")print("Do you commit? y/n")choice = input()if choice == "y":
print("Commit config: ", end="")device.commit_config()print("OK")
else:print("Discard config: ", end="")device.discard_config()print("OK")
print("===== Step 5. run show command(again) =====")print("Interface Status: ", end="") if device.get_interfaces()["ge-0/0/1"]["is_up"] == True:
status_after = ”UP"elif device.get_interfaces()["ge-0/0/1"]["is_up"] == False:
status_after = ”DOWN"print(Fore.YELLOW + status_after)
interfaces {ge-0/0/1 {
disable;}
}
)(
sample_napalm_set.py( )
$ python3 sample_napalm_set.py
===== Step 1. run show command =====Interface Status: UP===== Step 2. configure =====OK===== Step 3. compare configuration =====[edit interfaces ge-0/0/1]+ disable;===== Step 4. commit =====Do you commit? y/nyCommit config: OK===== Step 5. run show command(again) =====Interface Status: DOWN
sample_config_junos.txt
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_napalm_set.pyhttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_config_junos.txt
-
• sc N N O
• k c l S l S a in
• s c O W be• l : , S t A O A• ( ) 62 1 ,. , .03
Ansible
(playbook)
CiscoRouter
JuniperRouter
Ansibleiosxr Module
Ansiblejunos Module
iosxr-factsdebug: var=ansible_net_version
junos-factsdebug: var=ansible_net_version
12.1X47-D15.4
-
• lepl dh b cuY6ERUNPJ 5N S E _ i y olHRRO NC AM I E CNL AM I E ARE R I R NF MERUNPJ LN S E HRL
• O A NNJ wx 5 kh v• y ol• :
• 2 : 2 : 6 : 6 : . :/( /NPRI : 2 SLN EMNTN 6ERTI NPOEM UIRCH P MAMCE T 8AMN :PN N
•• krfh knsga t
• ,M I E imCNPE LN S E 0A AV LN S E k
• im,MI E v
)-NO PIGHR X 2 -1 ECHMN :N SRINM ,LEPICA 2MC
http://docs.ansible.com/ansible/latest/list_of_network_modules.html
-
() )
? ., .21 8 - 6 8 2 10 81
HI C Apip3 install ansiblepip3 install ncclient # junos
- hosts: vsrx1connection: localgather_facts: notasks:
- name: Get basic informationjunos_facts:
- name: show model namedebug: var=ansible_net_model
- name: show serial numberdebug: var=ansible_net_serialnum
- name: show versiondebug: var=ansible_net_version
[vsrx1]192.168.33.3[vsrx1:vars]ansible_ssh_user=user1ansible_ssh_pass=password1
hosts
playbookjunos_show_1.yaml
$ ansible-playbook -i hosts junos_show_1.yaml
PLAY [vsrx1] *************************************
TASK [Get basic information] *********************ok: [192.168.33.3]
TASK [show model name] ***************************ok: [192.168.33.3] => {
"ansible_net_model": "firefly-perimeter"}
TASK [show serial number] ************************ok: [192.168.33.3] => {
"ansible_net_serialnum": "83f144ddd4f7"}
TASK [show version] ******************************ok: [192.168.33.3] => {
"ansible_net_version": null}
PLAY RECAP ***************************************192.168.33.3 : ok=4 changed=0 unreachable=0 failed=0
≈
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_ansible/hostshttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_ansible/junos_show_1.yaml
-
()
3 ., .21 8 - 6 8 2 10 81
[vsrx1]192.168.33.3[vsrx1:vars]ansible_ssh_user=user1ansible_ssh_pass=password1
hosts
- hosts: vsrx1connection: localgather_facts: notasks:
- name: send command "show version"junos_command:
commands: show versionregister: result# "result"
- name: show resultdebug: var=result.stdout_lines
playbookjunos_show_2.yaml
$ ansible-playbook -i hosts junos_show_2.yaml
PLAY [vsrx1] ***************************************TASK [send command "show version"] *****************ok: [192.168.33.3]
TASK [show result of "show version"] ***************ok: [192.168.33.3] => {
"result.stdout_lines": [[
"Hostname: vsrx","Model: firefly-perimeter","JUNOS Software Release [12.1X47-D15.4]"
]]
}
PLAY RECAP *****************************************192.168.33.3 : ok=2 changed=0 unreachable=0 failed=0
AC
https://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_ansible/hostshttps://github.com/taijiji/NetworkAutomationTutorial/blob/master/samplecode/sample_ansible/junos_show_2.yaml
-
Ir o• © y• IrH12 e O i• SU y22. 3 o -0/t• -0/ Tia T• t e Ti h tI i
( o• I a s T • t n I p m i
t n I• Ir o Ti
) I• tI Ti U S lo• t n Ir i• p I o u I Ti• cg U I L h ry o• IrH12 O Ir
)- 8 /31-.4 3 6 2 C A , 6 / 6
-
BA
LH J M .6 - 6 E 5 C J EI H .E
• JJ I J : I E C I E H• H T JJ I J : J A A E C I E H
• u• 2 01U iehoObaknOclps Z
• r fOm tyu• /52 L L,8U iehoObaknOclp
• 2 01 STP P Nr g jO S
https://github.com/taijiji/napalm-scenariohttps://github.com/taijiji/napalm-scenariohttps://www.slideshare.net/ssuser6a8d29/napalmhttps://www.slideshare.net/taijitsuchiya5/jsnapypyez
-
• rI Ph p t P u P• , 8 A• h p t P u
• l m imn• -- m imn• oc m imn• oc e o
• t u O U A UHC P IS O
• T s t C y P I• T t P I m imn• T a g I• T I uO I
. .21 8 - 6 8 2 10 81
-
• Ic• e
• Ic• ru y U s t u
S nhp OIC• sU t m ol i ST ea
gU HICU HU HIC
6 A , - 0 2 . 7 6 8 621 , 2
-
The corporate logo mark embodies our burning vision “to target more than swift perception of
global changes and proper response to market shifts -- aspiring to be a part of inducing those transitions.”
Beneath the logo, this desire is expressed in the phrase “Challenging Tomorrow's Changes.”
8 2 - . -10 8 ,8 8 61 0 0