brunch

You can make anything
by writing

C.S.Lewis

파이썬 네트워크 프로그래밍 (#10:netmiko)

파이썬 네트워크 자동화

"netmiko"를 이용한 파이썬 네트워크 자동화

앞선 글에서 netmiko를 이용해서 각 장비에 인터페이스 설정을 진행했습니다. 설정 시에 각 장비 별로 접속하여 명령어를 넣는 방법이었습니다. 이번 글에서는 각 장비를 장비 밴더, Role로 나누어 그룹 별로 설정 하도록 하겠습니다. 

설정 시에 사용할 것은 파이썬의 리스트, for 문을 쓰겠습니다.  


8대 장비의 ntp server, clock 설정 및 확인  

cisco router

cisco switch

arista router & switch


스크립트 내용.


from netmiko import ConnectHandler


cisco_routers = [

        {

    "device_type": "cisco_ios",

    "host": "192.168.37.201",

    "username": "devnet",

    "password": "dn1001",

    },

        {

    "device_type": "cisco_ios",

    "host": "192.168.37.202",

    "username": "devnet",

    "password": "dn1001",

    },

        {

    "device_type": "cisco_ios",

    "host": "192.168.37.203",

    "username": "devnet",

    "password": "dn1001",

    },

]


cisco_switches = [

        {

    "device_type": "cisco_ios",

    "host": "192.168.37.102",

    "username": "devnet",

    "password": "dn1001",

    },

        {

    "device_type": "cisco_ios",

    "host": "192.168.37.112",

    "username": "devnet",

    "password": "dn1001",

    },

]


arista_routers = [

        {

    "device_type": "arista_eos",

    "host": "192.168.37.101",

    "username": "devnet",

    "password": "dn1001",

    },

        {

    "device_type": "arista_eos",

    "host": "192.168.37.111",

    "username": "devnet",

    "password": "dn1001",

    },

        {

    "device_type": "arista_eos",

    "host": "192.168.37.204",

    "username": "devnet",

    "password": "dn1001",

    },

]


# cisco router configuration

for device in cisco_routers:

    net_connect = ConnectHandler(**device)

    print(f"* Configuring NTP server : {device['host']}")

    commands = ['ntp server 192.168.37.129','clock timezone utc -0']

    net_connect.send_config_set(commands)

    output = net_connect.send_command("sh ntp status")

    output += net_connect.send_command("show ntp associations")

    print('-'*100)

    print(output)

    print('-'*100)

    net_connect.disconnect()


# cisco switch configuration

for device in cisco_switches:

    net_connect = ConnectHandler(**device)

    print(f"* Configuring NTP server : {device['host']}")

    commands = ['ntp server 192.168.37.129','clock timezone utc -0']

    net_connect.send_config_set(commands)

    output = net_connect.send_command("sh ntp status")

    output += net_connect.send_command("show ntp associations")

    print('-'*100)

    print(output)

    print('-'*100)

    net_connect.disconnect()


# arista router configuration

for device in arista_routers:

    net_connect = ConnectHandler(**device)

    print(f"* Configuring NTP server : {device['host']}")

    commands = ['no ntp local-NTP','ntp server 192.168.37.129','clock timezone UTC']

    net_connect.enable()

    net_connect.config_mode()

    net_connect.send_config_set(commands,exit_config_mode=False)

    output = net_connect.send_command("show ntp associations")

    output += net_connect.send_command("show clock")

    print('-'*100)

    print(output)

    print('-'*100)

    net_connect.disconnect()




실행 결과


마무리

스크립트가 실행 될때, 각 그룹에 정의된 장비를 하나씩 호출하여 명령어를 실행합니다. 

따라서 여러 대의 장비를 동일하게 설정이 필요하다면 list와 for을 적절히 사용한다면 도움이 될 것이라고 생각됩니다. 

매거진의 이전글 파이썬 네트워크 프로그래밍 (#9 : netmiko)

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari