0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Using IOS-Automation with Netmiko via OOP approach1

Posted at

NETMIKOを使ったIOS自動化のオブジェクト指向プログラムをつくってみる。

Screenshot_2024-11-25_00-55-12.png

構成は非常に簡単でIOSルーターに管理クラウドを直付けしているだけ。
そしてクラウドのPythonプログラムからR1のアクセスリストを確認する。

R1の設定は以下

R1
hostname R1
ip domain-name abc.com
crypto key generate rsa modulus 1024
ip ssh version 2
username admin privilege 15 password cisco

ip access-list standard ACL_MGMT
permit 10.255.1.51


line vty 0 4
login local
transport input all

interface gi 0/0
ip address 10.255.1.101 255.255.255.0
description Connect to MGMT
no shut 


Pythonプログラムはこちら

netmiko_demo.py
#router接続ライブラリのNetmiko
from netmiko import ConnectHandler
#フィルタリングと構造化データの表示ライブラリ
from ntc_templates.parse import parse_output

#デバイスに接続
ssh_connect = ConnectHandler(
    host="10.255.1.101",
    username="admin",
    password="cisco",
    device_type="cisco_ios",
    port = "22"
)

raw_output = ssh_connect.send_command("show ip access-list ACL_MGMT")
print(raw_output)

#フィルタリングして構造化する
structured_output = parse_output(
    platform = "cisco_ios",
    command = "show ip access-list ACL_MGMT",
    data = raw_output
)

print(structured_output)

実行したところ、アクセスリストとアクセスリストの構造データが取得された。

#python netmiko_demo.py 
Standard IP access list ACL_MGMT
    10 permit 10.255.1.51
[{'acl_type': 'Standard', 'acl_name': 'ACL_MGMT', 'line_num': '', 'action': '', 'protocol': '', 'src_host': '', 'src_any': '', 'src_network': '', 'src_wildcard': '', 'src_network_object_group_name': '', 'src_port_match': '', 'src_port': '', 'src_port_range_start': '', 'src_port_range_end': '', 'dst_host': '', 'dst_any': '', 'dst_network': '', 'dst_wildcard': '', 'dst_network_object_group_name': '', 'dst_port_match': '', 'dst_port': '', 'dst_port_range_start': '', 'dst_port_range_end': '', 'service_object_group_name': '', 'flags_match': '', 'tcp_flag': '', 'log': '', 'log_tag': '', 'icmp_type': '', 'time': '', 'state': '', 'matches': ''}, {'acl_type': 'Standard', 'acl_name': 'ACL_MGMT', 'line_num': '10', 'action': 'permit', 'protocol': '', 'src_host': '10.255.1.51', 'src_any': '', 'src_network': '', 'src_wildcard': '', 'src_network_object_group_name': '', 'src_port_match': '', 'src_port': '', 'src_port_range_start': '', 'src_port_range_end': '', 'dst_host': '', 'dst_any': '', 'dst_network': '', 'dst_wildcard': '', 'dst_network_object_group_name': '', 'dst_port_match': '', 'dst_port': '', 'dst_port_range_start': '', 'dst_port_range_end': '', 'service_object_group_name': '', 'flags_match': '', 'tcp_flag': '', 'log': '', 'log_tag': '', 'icmp_type': '', 'time': '', 'state': '', 'matches': ''}]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?