Upgrade to Pro — share decks privately, control downloads, hide ads and more …

明日からはじめるネットワーク運用自動化 / Let's get started Network Automation from Tomorrow!

明日からはじめるネットワーク運用自動化 / Let's get started Network Automation from Tomorrow!

Taiji Tsuchiya

January 25, 2018
Tweet

More Decks by Taiji Tsuchiya

Other Decks in Technology

Transcript

  1.    -01 ,1/ 2 1 8 0 C

    A , . / A 5 1 : 1AC 8 H
  2.  • e a e rOtx p s p b

    i • – Tlg jn y Ou J 02 A: :C 99 : A: CA:C A C9 • Oweb S h U ob i • 979H c e i b mU i • cg n U mi U Wn c -A IC : / 1-.4 98 A 2A A , 9C 8 / 8
  3.  • 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
  4. () • 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
  5.     8 42 - . -10 8

    ,8 48 1 40 0 " !%(4:6 .) . . . *3&(4 2+$7 . . . *3&1 /-9'#!0 . . . *3&58 ,/  . . .
  6.       5)($) • "&% #

    ! • +! • !3! • 04$ - 2 • mp • U S r • e h I • s U t - 2 • mp C • mp • Cl • s • e h uT • ianmph • U 5)(*) • % *,. ! • !$~*,. "/2 ! • 04*,.  5)(() • % (! • !(! • 04( • '- 016  - 2 • Cl O H • Co c gl • T S T yT • Ac gl I 53 ., .21 8 - 5 8 2 510 81
  7.  • u o cn s l S N hO

    k gi gc k • a p pwo uo • b a011 m • r y • b i e c y ,: b AC 8: 6 : : : / : 2 : • c kI c b TI c gc k T • c ca N ON ON O S Ti Uj a • N c kON c ON S ON U hO T • t r TiU a AG :8 H .20 - 2 6 1 : C :6 . 6
  8.   • 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 PS p R R g ABHC E I / 2 .7 A A E A D C 8 /
  9. & vh C O I 4 A 9 iloT (

    T O GU ) OT s E gE gtET GU nrle 5 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) .!3+ %,3+  *)-&   ! "#$4 105'/  ( 2 '/ 2 ?
  10.     89 2 - . -10 8

    ,8 8 1 0 0         https://speakerdeck.com/opelab/20171212-automation
  11.  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
  12. ( ) • > C > • r U pmocH

    2 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 -
  13.  • b O PhU ehU p p O ShU

    • 2 m r rn h u b OI b cS • y o lcT b n h oO t i l O H Sg.CA8 l K c a p p ©OI W i g-, U S b n b a c s a U b ) C 8A 0-) 1 0 8 A (
  14. • 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 > , c P T c 2 C > OUU UH . A C 041. 487 > 3 C > - 8A 76 0>7
  15. 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 pt 0CA8 I gi 1T P S 1 uT P CA8 CA8 CA8 ) U ic ae h rn l H
  16. • 57 c efUi • : H IPM lk •

    C M/ PPP CIH I IPH I M • 4 57 57 lk • C M/ I M I M )).- . (A( . ) , • 1 H 57 , lk • C M/ I M I M . ) -A . ..,, ( • gUdh XW qawb lk S utsp6082 oms XrnT 1I C - 385129 8 CHI 7I O IHM 0 3H
  17.   • 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 = 1 if num == 1: print(“Hello JANOG!!!”) $ python3 sample.py Hello JANOG!!! • io ml • s CA8 a p S CA8 g n {t O H I
  18. : A ., .21 : -:7 : 82 10 1

    • • S • S • a • H • U • T c • : C • I O • S •
  19. 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
  20.   = C 9 8A ., . 8 =

    -= A9= 9 a = 1 # a: int  b = 1.1 # b: float  print(a) print(b) print(‘-----’) c = a + b # c: float  print(c) $ python3 sample_number.py 1 1.1 ----- 2.1 I H I H O 0 S1 T U (Cint a;float b;)"  !  ( # ) 2 C
  21.  A= C - . - , 8 = 2

    c = “text” # ‘“  $(,# d = "" #)-%+&0  #Python3  ! Unicode+&0  " #Python2  !ASCII u"" .'*/ print(c) print(d) $ python3 sample_string.py text  2 8 1 = A O 0 S TU H I H
  22.  A= - . - , 8 = 2 router_list

    = ["router_A", "router_B", "router_C"] #  print(router_list) # 01 print(router_list[1]) #  (len) print(len(router_list)) $ python3 sample_list.py ['router_A', 'router_B', 'router_C'] router_B 3 2 8 18 A H I O 0 C 0
  23.  = C 8A ., . 8 = -=: A

    = $ python3 sample_dictionary.py router_A 15.1 O S 0 1 TU I I H # keyvalue  router_info = { "hostname" : "router_A", "os" : "junos", "version" : "15.1", } #   print(router_info["hostname"]) #   print(router_info["version"]) : 2 A = C C
  24. 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
  25. 34 ., .2148 - 8 2 10 81 H IA

    C O U #    router_list = [”router_A”, ”router_B”] # router_list router_name  for router_name in router_list: print(router_name) $ python3 sample_for_2.py router_A router_B #    # range(3) = 0,1,2 for num in range(3): print(num) $ python3 sample_for_1.py 0 1 2 H IA TS O U sample_for_1.py sample_for_2.py
  26.  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.txt How are you feeling? sample_file.py   [   : sample_read.txt] Hello JANOG! from sample_read.txt How are you feeling? [   : sample_write.txt] Good! C HA A C A 8 2 - . -10 8 ,85 8 1 0 0
  27.  ( - 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 sT u[iodl T O Pa
  28.   A C A C HI #  !

    # $  # OS show bgp summary# def 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_info    command = 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
  29.  • ni di coO s = • a a

    s O l S[ = co f O = • • ni ) : 2 (P • di efI rhf ]a e f ,        8   =  () #    .  #   . () #  
  30.   89 2 - . -10 8 ,8 8

    1 0 0 class Router: # &+$: '#% " +$ def __init__(self, hostname, os, version): #  $:  ( $ self.hostname = hostname self.os = os self.version = version # show bgp summary* def 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 # Router '# router_A = Router(hostname="router_A", os="junos", version="15.1") router_B = Router(hostname="router_B", os="ios", version="15.7(3)M") #   Router !)+$  print(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_A show bgp summary router_B show ip bgp summary A C A C HI sample_class.py
  31. A - 0 32 . 83 21 2 • N

    C • .-, SC • JUOT U UC • C • I JH C
  32.   • m hmn • cg odO d oI

    Um hmn • 20 3 3 022 3 : • tm hmn • jle T I m hmn • a m hmn d oH • : 0 2 0: :0 08 A ., .31 : - 8 : 3 10 :1 yr T usI ip m U S C
  33. A ( , - 0 32 . 8 ) 3

    21 , 2 $ pip3 list Jinja2 (2.9.6) . . . $ pip3 install jinja2 S H HOI C 1 H HOI C HOI C T H U () *&  venvvirtualenv",* #  pip %'( ( $+)! ) https://docs.python.jp/3/library/venv.html
  34. $ 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 U ac n, 8 ac ph -r S PC A A A AO c H sm gI Ue T () : pip  Python setuptools  pip http://www.lifewithpython.com/2012/11/Python-package-setuptools-pip.html 8 c ito 3 . .21 8 - 8 2 10 81
  35.  4 A ., .31 - 83 10 1 #

    datetime    import datetime # datetime  datetime now  now = datetime.datetime.now() print(now) $ python3 sample_library.py 2017-11-15 14:25:56.360603 sample_library.py    20 3 83H HI C O
  36.  A M ) I L 5 ) 5 3(

    L I P ) : JSON https://thinkit.co.jp/article/70/1
  37.  ( 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 S HN OJ N TU .A S P P 1 h HNe O ag iIJ U g h sample_json.json   import json file = open("sample_json.json", "r") # file  json routers_list = json.load(file) print(routers_list) print(”-----") print(routers_list[0]["ip"]) sample_json.py
  38.      J J a 7 J

    P ) J 37 )( 2 <html> <body> <h1> {{ title }} </h1> <p> {{ body }} </p> </body> </html> title = “Web page 1” body = “Hello! World!” {{  }} :  {% if  %} : if {% endif %} {% for  in  %} : for {% endfor %} {# #} :    [  (Jinja2)] HTML     
  39.     ( C ( ,0. 1 0

    3 A ) 32 , 3 interface {{ if_name }} description {{ if_description }} ip address {{ ip4 }} {{ ip4_subnet }} duplex auto speed auto no 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/1 description To_RouterA ip address 192.168.0.1 255.255.255.0 duplex auto speed auto no shutdown ! sample_template.jinja2 sample_template_1.py S  
  40. ( ( ( ) interface {{ if_name }} description {{

    if_description }} ip address {{ ip4 }} {{ ip4_subnet }} duplex auto speed auto no 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/1 description To_RouterA ip address 192.168.0.1 255.255.255.0 duplex auto speed auto no shutdown ! interface fastethernet 1/2 description To_RouterB ip address 192.168.1.1 255.255.255.0 duplex auto speed auto no shutdown ! interface fastethernet 1/3 description To_RouterC ip address 192.168.2.1 255.255.255.0 duplex auto speed auto no shutdown ! 9 sample_template.jinja2 sample_template_2.py 
  41.  4 0 text = “Today is Sunday” pettern =

    ”Today is (.+)” .:  & (*1( +: $ '#   ) ():  match = re.search(pettern, text ) #  !" % match.group(0) # =>Today is Sunday #    % match.group(1) # =>Sunday
  42.   import re # :-' <(> # show version*'%16

    show_version_txt = """ Hostname: vsrx Model: firefly-perimeter JUNOS Software Release 12.1X47-D15.4 """ # .#8& # ():  # . : !'" 7 <(>1<( # + : 5/ .#8& 3  , $= regex = "JUNOS Software Release (.+)" # show_version_txt <(> 3  # .#8& <(> 4+*' match = re.search(regex, show_version_txt ) #  <(<(>028) print(match.group(0)) print(”--------------------") #  <(>   9;4+ print(match.group(1)) sample_regex.py $ python3 sample_regex.py JUNOS Software Release 12.1X47-D15.4 -------------------- 12.1X47-D15.4  
  43.  • t l h s i h r C

    • I T H h H i 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
  44.   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
  45.   8 42 - . -10 8 ,8 48

    1 40 0 def add(x, y): sum = x + y return sum sample_unittest.py S OC H OC import unittest import sample_unittest class TestSample(unittest.TestCase): def test_add(self): actual = sample_unittest.add(1,2) expected = 3 self.assertEqual(actual, expected) if __name__ == "__main__": unittest.main() tests/test_sample_unittest.py ├── sample_unittest.py └── tests └── test_sample_unittest.py IA
  46.   python3 -m unittest tests.test_sample_unittest . ---------------------------------------------------------------------- Ran 1

    test in 0.000s OK 4 5 python3 -m unittest tests.test_sample_unittest F ====================================================================== FAIL: test_add (tests.test_sample_unittest.TestSample) ---------------------------------------------------------------------- Traceback (most recent call last): File ”xxx/tests/test_sample_unittest.py", line 12, in test_add self.assertEqual(expected, actual) AssertionError: 3 != 4 ---------------------------------------------------------------------- Ran 1 test in 0.000s 4 5 () () python3 –m unittest +/ (  %# !,  &$test * "__init__.py')-.
  47.     • 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
  48. • x sb rg a f EBn up • ERUNPJ

    N-NDE M .ELAMD 4ABQ • l a0 2a r ty o a f vt kh • -IQCN - 4 2:4 -IQCN b ri oc • 0 ri oc • l a-42a r 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
  49.     • 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 TH o o g pn y u y S L m I r U C
  50.      • 5S 5 ( (

    • ) 5 O 0 ( • : ( Cisco Router Juniper Router Arista Router Cisco  Juniper  Arista 
  51.    Manufacturer OS Library Language OS version Cisco

    IOS / IOS-XE One Platform Kit (OnePK) C, Java, Python ASR1000: 3.12 -3.17(EoL) YANG Development Kit(YDK) Python, C++ 16.5.1 later IOS-XR One 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 version Nexus6000: - 7.2(EoL) NX-API Web GUI, HTTP/HTTPS Nexus9000: all version Nexus7000: 7.2(0)D1(1) later Nesus6000: 7.2(0)N1(1) later Juniper JUNOS PyEZ Python 11.4 later Juniper Extension Toolkit(JET) Python 16.2 later Extreme (ex-Brocade) Network OS PyNOS Python 5.0.1 later Arista EOS pyeapi, rbeapi, goeapi Python, Ruby, Go 4.1.2 later EOS SDK C++, Python all version O , 5 S , 1
  52. : / • 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. . . .
  53.       import Exscript # SSH(*:pip3

    install exscript) import re # "( username = "user1" password = "password1" ip4 = "192.168.33.3" # SSH + session = Exscript.protocols.SSH2() session.connect(ip4) #   account = Exscript.Account(name=username, password=password) session.login(account) #   $!,' session.execute("show version") result = session.response print(result) print("----------") # "( )& pattern = "JUNOS Software Release \[(.+)\]" match = re.search(pattern, result) version = match.group(1) print(version) # SSH #% session.send("exit") session.close() sample_exscript_show.py $ python3 sample_exscript_show.py show version Hostname: vsrx Model: firefly-perimeter JUNOS Software Release [12.1X47-D15.4] ---------- 12.1X47-D15.4  3 () ) ,
  54. ) ( 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 n choice = 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/1 unit 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 check configuration check succeeds ===== Step 4. commit ===== Do you commit? y/n y commit commit complete exit Exiting configuration mode ===== Step 5. run show command(again) ===== show configuration interfaces ge-0/0/1 disable; unit 0 { family inet { address 10.0.1.1/24; } } sample_exscript_set.py()   : sample_exscript_set.py  , ( 4 ) 5
  55. •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
  56.       • 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  Cisco Router Juniper Router Cisco  Juniper  get_network_driver(”iosxr") get_facts()[‘os_version’] 12.1X47-D15.4 get_network_driver("junos") get_facts()[‘os_version’]
  57.  • r y n b laU u 8P 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 R Tt m d d Xc S r n R e r n m • . • / E 17: ) 2 F 5NHEJ 5 67: ( F 0E 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
  58.   ( 5 A ( , - 0 25

    . )8 21 , 2 # NAPALM  # : pip3 install napalm) import napalm from pprint import pprint # JUNOS    driver = napalm.get_network_driver("junos") device = driver( hostname="192.168.33.3", username="user1", password="password1" ) #   ! device.open() # x 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  IC CH
  59. ) ( . . . 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/n y Commit config: OK ===== Step 5. run show command(again) ===== Interface Status: DOWN  sample_config_junos.txt
  60.       • s c 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) Cisco Router Juniper Router Ansible iosxr Module Ansible junos Module iosxr-facts debug: var=ansible_net_version junos-facts debug: var=ansible_net_version 12.1X47-D15.4
  61.  • lepl dh b cuY 6ERUNPJ 5N S E

    _ i y ol HRRO 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 NP OEM UIRCH P MAMCE T 8AMN :PN N • • krfh knsga t • ,M I E im CNPE LN S E 0A AV LN S E k • im,MI E v ) -NO PIGHR X 2 -1 ECHMN :N SRINM ,LEPICA 2MC
  62. () ) ? ., .21 8 - 6 8 2

    10 81 HI C A pip3 install ansible pip3 install ncclient #junos - hosts: vsrx1 connection: local gather_facts: no tasks: - name: Get basic information junos_facts: - name: show model name debug: var=ansible_net_model - name: show serial number debug: var=ansible_net_serialnum - name: show version debug: var=ansible_net_version [vsrx1] 192.168.33.3 [vsrx1:vars] ansible_ssh_user=user1 ansible_ssh_pass=password1    hosts playbook junos_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  ≈
  63. () 3 ., .21 8 - 6 8 2 10

    81 [vsrx1] 192.168.33.3 [vsrx1:vars] ansible_ssh_user=user1 ansible_ssh_pass=password1  hosts - hosts: vsrx1 connection: local gather_facts: no tasks: - name: send command "show version" junos_command: commands: show version register: result #  "result" - name: show result debug: var=result.stdout_lines playbook junos_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
  64.      Ir o • © y

    • IrH12 e O i • SU y 22. 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
  65. B A EJ H .6 - 6 C 5 AIH

    C .C • HHE H I: C E A C • O OP HHE H I: H E IE • s • 2 01S h gnMa jmMb o t • p eMl r s • /52 J T J,8S yuh gnMa jmMb o • 2 01 NUNZ Lp iMcTU PZ
  66.  • rI P h 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 UH C 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
  67. • Ic • e • Ic • ru y U

    s t u S nhp OIC • sU t m ol i ST ea gU HIC U HU HIC 6 A , - 0 2 . 7 6 8 621 , 2