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

明日からはじめるネットワーク運用自動化 実践編

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

明日からはじめるネットワーク運用自動化 実践編

第9回NCC情報交換会(電力系ISPの集い)で発表した資料です。

Avatar for Taiji Tsuchiya

Taiji Tsuchiya

April 18, 2018
Tweet

More Decks by Taiji Tsuchiya

Other Decks in Technology

Transcript

  1.     1--IH O 034 /42-. 4879 3

    C : A , 8 :7 / 7 4 : : 4AC79: NJ
  2.  • 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
  3.  t n Si h I e g c sut

    T U • ) c sut -1 1 : 8 T p T U • G o o 1 B O e : A t n Uh c ly T U • a S e h cS O U mr P S H , :8 B ( 0,. 2 CB: A :
  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.  8 2 - . -10 8 ,8 8 61

    0 0  /      /
  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 %*$ " $ / %*#0" $ ! -  /-)'$ (.   - & , +,
  11.     • U SV es nglp •

    og prh Hnog • nru ln nru ln / C . • , • Is i R I O e Tc • I e Tc R mHte T • IL a a XaXPc mHt R • e Xa XP N e c • y a X N 8 1- 2 1 0 C A
  12.     1-6 #.  &9  78:

    &) 1 +%/,4" 1%  !<31( *'  =0 1$5 C t C t 1;>,2 m C 11 / 1 / 1 o i 1 C 1 /1 /1 1 / /1 C C1- / 1 C
  13.  • l h l g l i • 1,

    -, • yGu Gs • ./ 2 ?8 • O V • oG tGs n G l O G l l © • H O Il g Vi • • g mG plegLi l Vi • l T • G S • e a h n G e i • r G l h g h V g c • U l Vi G l O i 8 C ,0. 1 0 / A 8 ? 8 ,
  14.    • bH e • hp g UI

    • gt c ebH e G S G • e us S GbH e • GT g L OebH oi prn g U e • 8 a 8 A3 8 -23 bH m l o g y e 8 C ,0. 1 0 / A 8 8 2 ,
  15.   • • • • 4 4 • •

    1 $"    (!#, NetOpsCoding#3) https://www.slideshare.net/taijitsuchiya5/ss-63478739
  16.  8 2 - . -10 8 ,85 8 1

    0 0 : “”   ( @BIGLOBE, JANOG US Regianl Meeting) https://www.slideshare.net/taijitsuchiya5/ss-55698922
  17. ) ( 8 2 - . -10 8 ,8 8

    61 0 0 : “”   ( @BIGLOBE, JANOG US Regianl Meeting) https://www.slideshare.net/taijitsuchiya5/ss-55698922
  18. / • U t • yU C • U ocs

    C • S g p e C • u Ta H C • g p O • t i r I n lmh A ( , - 0 2 . 7 )8 21 , 2
  19.   • S a • I iArT a C

    s goA a • Tm A lh a H CS • cpnt S • OU I u T • y • I A eA • S T 8 12 , - ,0 2 0 .
  20.  ) -ABHC : I ( 0 1-/ 98 A

    A A 9C 8 0 8 • e U ,.2 299C :i 2C G 9 299C jS c Ti l O • hi P U. S b O B : 8A B99C B • higa
  21. / 1-6 #.  &9  78: &) 1 +%/,4"

    1%  !<31( *'  =0 1$5 / : 0 / : 1;>,2 0 / C 2
  22. T u Ge iS GxH C 8 ,1- 2 1

    0 A 8 : 8 , Psp RI h Rr cR Pr ,. UaP . 0 . ,. UaP R c Ry t cR Py . y Oc o cR Plg Ry t cR Py Oc o . lg Ry t . y . nmy Oc o A . 8 C 8: lg Ry t . nmy Oc o A . 8 C B lg Ry t . my
  23. HNI A O ) -62 , 6 A L J

    I - v e Rpsd nR • mp Rf U Xa b P -nR Y • mp Rf l kR b P .L H I M 8b • r R P 0 tce Y • hio U P I C P U • v g y g 3N A M IJ ( • mp Rf P3N A eu Sa 1 3 0 b t if ml T
  24. 3 . .21 8 - 8 2 10 81 a

    C r Pgon CcU uHTO I A, 8P m c C U u I H , 8P y ilS. t a a CcP u ehpP s u H
  25.  • rH c P a i i K g

    P Ti K • n sH so iyH a c P c K TaK • Hp m Ug c o i H HpP Sl K u O rH m P K I WTh m a H H b K l P le h.- T ©c o c b K t H b c 8 ( ,1. 2 1 0 C A ) 4 ,
  26. • 3 ( TI I • 3 r tt l

    c nhulI • sgost H3 T T i l I , H sgost • sgost H3 p me , a c nhul I 3 - S O c a ye U c 3 P I ) A , 152 06 5 8 4 C .> 87 1 8
  27.   • 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 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 4; :6 C
  28.   • m hmn • cg odO d oI

    Um hmn • 20 022 7 : • tm hmn • jle T I m hmn • a m hmn d oH • 7 :70 270: :0 08 A ., . 1 : - 8 : 10 :1 yr T usI ip m U S C
  29. ) A .,) .21 - (82 10 1 $ pip

    list Jinja2 (2.9.6) . . . $ pip install jinja2 HI 0 HI C U HI S O T () *&  venvvirtualenv",* #  pip %'( ( $+)! ) https://docs.python.jp/3/library/venv.html
  30.  9 - . -20 89 ,9 98 2 0

    80 # 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    1 2 2 HA C I
  31.  • 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 1 : 2 F 5NHEJ 5 6 : ( F 0E I 4 :# F 6 : , F 4 : ( ( 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 03 HI#:IFN EIH / E 4H
  32.       • P vrOeo mi

    t A • hkca NS mX mnA • N sO c LM uA • u hkca w Mo e C O CA • ,(.( ,3 6 • OmX mn • ) 2 ,3 :6 3: 23 :2 3 :1 : 1 1 3 :6 ,) 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’]
  33. 8A -2. ,3 2 8 1 :BA - U eH

    l T aHyI R t S gi a Ss S s - r 1 r- G a S cO a S u S P Opg S mh a S u S P Opg mh a S u on P Opg 0 BA : C Amhx a S u on P Opg 0 BA : Amhx a S u n
  34. 8 23 - . -103 8 ,8 8 1 0

    0 import napalm # JUNOS   driver = napalm.get_network_driver("junos") device = driver( hostname="192.168.33.3", username="user1", password="password1" ) #     device.open() #    device.close()  OS junos / eos / ios / iosxr/ nxos   • IP  • ,     
  35. • S • I 8=Clsyp h S h T e

    mnot U O T r t • i wh ah mnot U T S1 cuH T g g e ( -=> 8A ) 20-.3 2 8 = 1= A = , 4 Hostname: vsrx Model: firefly-perimeter JUNOS Software Release [12.1X47-D15.4] 12.1X47-D15.4  
  36. • I G HP O • P O B 8

    C -1. ,2 15 0 A 8 5 8 3 - device.get_intefaces()[“ge-0/0/0”][“is_up”] # ge-0/0/0  UP ”True”, Down”False“ device.get_bgp_neighbors()[“global”] [“peers”] [“192.168.35.2”][“is_up”] # AS65001  UP ”True”, Down”False“
  37. • g g d ip e ml a SU g

    t ( ST g o a H .H L ) s ry RP g u s ry RP.H L H ( .HINJ AL O , 0 5./ A H 8H ML H - J 0 d ip e m hmn 1 C kocj b o a m hmn 4-6-23
  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. OS RI , OS U T H, 0 ( ,

    A 9 ) .2/,-3 28 1 C 8 . device.load_merge_candidate(filename="config_junos.txt") device.compare_config() print("Do you commit? y/n") choice = input() if choice == "y": device.commit_config() else: device.discard_config()
  41. A B G ,41 4 8 C B 8 ,

    8 • u Je eis Y H e isM L T Jg I    • Jg e • tJc • oeyJl • ,2 l e • 2m nJ,2 l e • 2m nJ • • paJrh UPSH - 10U .M N O I
  42.  A M ) I L 4 ) ( L

    I 1 P ) : JSON https://thinkit.co.jp/article/70/1
  43.  C 8A ( ,2 2 8 1 A )

    4 , [ { "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 -1 .e NIJ ST U - PiO O 0CA8 c cg N c PahHI T c cg 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
  44.  ( A C ) -30 ,4 38 2 C

    8A - host_info: mgnt_ipaddress: 192.168.33.3 hostname: vsrx1 os : junos username: user1 password: password1 if_info: if_name: ge-0/0/2 if_address_ipv4: 192.168.35.1 if_subnet_ipv4: 30 if_description: AS65002_peer .g ST IO MP LO U YH . ST a 1 C e ei IO MP l he c he ei import yaml # YAML   file = open("sample_yaml.yml", "r") # file YAML  router_info = yaml.load(file) print(router_info['host_info']['hostname’]) print(router_info[‘if_info'][‘if_name’]) sample_yaml.yml sample_yaml.py $ python3 sample_yaml.py vsrx1 ge-0/0/2  
  45. Scenario file (YAML) Router (Target) NAPALM Config Template (Jinja2) JUNOS

    IOSXR IOS EOS 4 Router • • • • check_interface • check_bgp_neighbor • set_interface • set_bgp_neighbor • set_route_policy_advertised/recieved interfaces { {{ if_name }} { unit 0 { family inet { address {{ if_addr }}/{{ if_subnet }} } } } } scenario: - check_interface: if_name: ge-0/0/2 if_status: up - set_interface: if_name: ge-0/0/2 if_addr: 192.168.35.1 if_subnet: 30
  46. : 64 A , - 0 2 : .: 6:

    8 621 , 2 C O H I
  47.    yu o T i h c t

    u S Ua G • t u ,.2 288A U r Ua G • H p p 2 C: P 4 B yu o i m Ua G • TaOg S S i T P G ens l S T I () - A :C ) 041- 48 : C B 8A 0
  48.  i fO hO (4 • (4 • / •

    y y • • • J n p • oy • N • (4 t • • ) • h h h • w • • N s )) • N s • fO 9 S fO h 3P h r • fO 9 S Ph O n
  49.  • rH c P a i i K g

    P Ti K • 5n sH so iyH a c P c K TaK • Hp m Ug c o i H HpP Sl K u O rH m P K I WTh m a H H b K l P le h.- T ©c o c b K t H b c 8 ( ,1. 2 1 0 C A ) ,
  50. • 3 ( TI I • 3 r tt l

    c nhulI • sgost H3 T T i l I , H sgost • sgost H3 p me , a c nhul I 3 - S O c a ye U c 3 P I ) A , 152 06 5 8 4 C .> 87 1 8
  51. m CA8 ic so OH ( , C 8A .2

    ,-3 2658 1 A 6 5 . 5 $ 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
  52. • 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
  53.   • 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-. 2 58 1 : A ,; 54 5 $ 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
  54. : 5 A ., .215 : -: : 82 10

    1 • • S • S • a • H • U • T c • : C • I O • S •
  55. 6 5 A ., . 25 - 6 8 621

    2 $ python3 sample_print.py Hello JANOG! C C IH print("Hello JANOG!") 18 0 6 C IH
  56.   8 , - 0 7 = . C

    =A 7 ,= 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 IH S IH 1 T2 U O (Cint a;float b;)"  !  ( # ) A 7 =C 57
  57.  = 8 ., .5 - A 8 5 8

    c = “text” # ‘“  $(,# d = "" #)-%+&0  #Python3  ! Unicode+&0  " #Python2  !ASCII u"" .'*/ print(c) print(d) $ python3 sample_string.py text  = 52 8 = 0 S1 T C U C I H I H O
  58.  = C 9 8A , - 0 58 =

    .= A9= 95 , 5 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 9 A C IH O 1 2 S 1 S S 2
  59.  :8 , - 0 = . C : =A

    : ,= $ python3 sample_dictionary.py router_A 15.1 TS 1 2 [U H OH H OH I # keyvalue  router_info = { "hostname" : "router_A", "os" : "junos", "version" : "15.1", } #   print(router_info["hostname"]) #   print(router_info["version"]) A 6: : =
  60. A 6 . . 26 - 8 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 8 0 A U T c ,A 6 PCmU T Tai 21 PleH _ f O gI Ch Sfn
  61. 3 ., .21 8 - 6 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
  62.  C I C HA #  :"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 I C HA 3 ., .21 8 - 6 8 2 10 81
  63.  ( - 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
  64.   A C A CH I #  !

    # $  # 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 5 ., .21 8 - 6 5 8 2 510 81
  65.  • S] h ch n I I r •

    P r f S S] n el • o = S] • h [ i (i 6 i O • ch = de el 6 : dael , 66          =  () #    .  #   . () #  )
  66.   ., .21 8 - 6 8 72 10

    81 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 CH I sample_class.py
  67. 6 A - 0 2 . 6 8 621 2

    • N C • .-, SC • JUOT U UC • C • I JH C
  68.   • n ino • dh pe rae p

    OH n ino • 1 : 1 8 • un ino • m c U O n ino • g acl n ino rae pI y • 8 81 81 1 19: 6 C , - 0 26 . 9A : 21 , 2 s U O t j nl T S
  69. A ( , - 0 2 . 8 ) 21

    , 2 $ pip3 list Jinja2 (2.9.6) . . . $ pip3 install jinja2 S H HOI C 7 71 H HOI C HOI C T H U () *&  venvvirtualenv",* #  pip %'( ( $+)! ) https://docs.python.jp/3/library/venv.html
  70. $ 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) c m H go- O i ,.srOT S P a I tnlh H e U () : pip  Python setuptools  pip http://www.lifewithpython.com/2012/11/Python-package-setuptools-pip.html A8 a up C , 0 32 . 7 83 21 2
  71.  - . -20 8 , 8 72 0 80

    # 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    1 2 72 HA C I
  72.  A M 7 ) I L7 ) 7 3(

    L I P ) : JSON https://thinkit.co.jp/article/70/1
  73.  ( 8 ) - 0 ,4 7 2 C

    A 7 - [ { "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
  74.      J J a 57 J

    P ) J 7 )( 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     
  75.     ( C 867A ) -1 ,2

    1 7 0 A8 8 - interface {{ if_name }} description {{ if_description }} ip address {{ ip4 }} {{ ip4_subnet }} duplex auto speed auto no shutdown ! I J O .8 U 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 H ST  
  76. ( ( ( ) interface {{ if_name }} description {{

    if_description }} ip address {{ ip4 }} {{ ip4_subnet }} duplex auto speed auto no shutdown ! 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 ! 7 sample_template.jinja2 sample_template_2.py 
  77.  7 8 text = “Today is Sunday” pettern =

    ”Today is (.+)” .:  & (*1( +: $ '#   ) ():  match = re.search(pettern, text ) #  !" % match.group(0) # =>Today is Sunday #    % match.group(1) # =>Sunday
  78.   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  
  79.  • s i g ur h Ug c •

    H S Ug h Ug A h Ug • ur o rh O t • yn • g cA n h g OA ur ur h O Ug • g h Ug Ap C g p ©h SeUO g • ur o rh O t • ur o rh O A l Ig • ur o rh Ua A m e T Ug : ., .21 8 - > 8 2 10 81
  80.   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”) H C A H 8 12 , - ,0 2 0 .
  81.   8 12 , - ,0 2 0 .

    def add(x, y): sum = x + y return sum sample_unittest.py C O I C I 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 H A
  82.   python3 -m unittest tests.test_sample_unittest . ---------------------------------------------------------------------- Ran 1

    test in 0.000s OK 3 8 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 3 8 () () python3 –m unittest +/ (  %# !,  &$test * "__init__.py')-.
  83.     • J AS n 2 H

    DCcU c • 1E C2 0 • T X bhSki O P2 H DC e ag • 8C D A8G • WcaP bhol a • , H , H-I , H08 • pmjUh We bh a • C CG F A . DE F H L .41 - 4 CD DAIH DCG F 8 .C
  84. • x sb rg a f EBn up • 6ERUNPJ

    N-NDE M .ELAMD ABQ • l a0 2a r ty o a f vt kh • -IQCN - 2: -IQCN b ri oc • 60 ri oc • l a- 2a 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
  85.     • C e ae t •

    .. / 7 e ae O , ch • cg h n e ae t • cg h n y s Cg t A /- 0 / 2 . 7 8 21 2 ur U t S n n e ae omp tp iOi l H L I TL
  86.      • ) : S •

    ) ( • O 8 : Cisco Router Juniper Router Arista Router Cisco  Juniper  Arista 
  87.    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 8 , 8 9 S 8 ,
  88. : / • l c X 552 69 9 t

    nC • /43X r ST L l C • r i Xe s H C D xIp • . 1 : 9 9 - 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] . . . i E ( X i E 7), 0 ) X 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. . . .
  89.       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  1() ) ,
  90. ) ( 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  , ( 2)
  91. • IF/ 2 3 - C 9F E • l

    • egm SwyTn rU a ot • .I - 8 cTMNO E H C F T i NVsx p u h +) - C F ( 1 -0 A F AE , 9 1A
  92.       • hra S t

    okM v C • mec P o o C • Pn u r eX s NO L wC • w mec O i C • ) . , . 9 • o o • ) 4 . A 1 4 46 3 3: 3 : A . 2 ( 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’]
  93.  • 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 1 : ) 2 F 5NHEJ 5 6 : ( F 0E I 4 :# 9 F 6 : F 4 : ( ) ( 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 0 HI#:IFN EIH / E 4H
  94.   ) A 6 ( -0. ,1 0 6

    8 9 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() #  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  H C H I
  95. ) ( . . . 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
  96.       • 9t W A

    : O i O : • ln e N . N W As 9 A N : b a S : • t 9 i kc N A W: • . , N A : N : • ) 6 3 2 8 8 1 ( 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
  97.  • lepl dh b cuY ERUNPJ 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 : 9 : : . : /( /NPRI : 2 SLN EMNTN ERTI 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
  98. () ) 8 2 ? - . -10 8 ,8

    8 1 0 0 HA C 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 I $ 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  ≈
  99. () 8 12 , - ,0 2 0 . [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 A
  100.      H n • • H

    01 e O h • T Ii 11- 2 A n ,/.s © • ,/. Sh aS • s Sh Ig sH O h ( n • H a r LS • s m Hu o l eh s m O H c c • H n c Sh ) H • sH Sh TU n • s m H eh • o H nyt H c Sh • iT pH Ig ny • H 01 e H , C 8A .20,-3 2 8 1 A 4 .
  101.  • 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 3 . .21 8 - 8 2 10 81