hostリストとコマンドリストの組み合わせ表を読み込み順次実行する。
python、netmikoでCisco機器から収集し結果テキストを保存する。

get-log.py
import re
import os
from netmiko import ConnectHandler
currentdir = os.path.dirname(os.path.abspath(__file__))
#組み合わせ表の読み込み
get_log_list_path = os.path.join(currentdir,'Get_log_list.txt')
with open(get_log_list_path) as f:
#1行ずつ読み込む
list_cmds = f.read().splitlines()
for list_cmd in list_cmds:
#テキストの#行は読まない
if not re.match('#', list_cmd):
hlbuf = list_cmd.split(',')
host_list = hlbuf[0]
cmd_list = hlbuf[1]
#hostリストの読み込み
filepath = os.path.join(currentdir,host_list)
with open(filepath) as f:
#1行ずつ読み込む
input_data = f.read().splitlines()
#hostリストの順次実行
for rows in input_data:
#テキストの#行は読まない
if not re.match('#', rows):
row = rows.split(',')
HOST = row[0]
USERNAME = row[1]
PASSWORD = row[2]
DEVICETYPE = row[3]
# print(host, username,password)
# 接続パラメータを設定
remote_device = {'device_type': DEVICETYPE,
'host': HOST,
'username': USERNAME,
'password': PASSWORD,
'secret': PASSWORD,
}
print('Connecting to ' + HOST)
remote_host = ConnectHandler(**remote_device)
print('Entering enable mode ...')
remote_host.enable()
#コマンドリストの読み込み
cmd_txt_path = os.path.join(currentdir,cmd_list)
with open(cmd_txt_path) as f:
#1行ずつ読み込む
cmd_datas = f.read().splitlines()
output = ''
#コマンドリスト繰り返し実行
for cmd_data in cmd_datas:
#テキストの#行は読まない
if not re.match('#', cmd_data):
cmd_buf = cmd_data.split(',')
cmd = cmd_buf[0]
output += '#' + cmd + '\n'
output = output + remote_host.send_command(cmd)
#プロンプト名の読み出し
prompt = remote_host.find_prompt()
hostname = prompt[:-1]
#ファイル出力用に禁止文字削除
hostname = re.sub(r'[\\/:*?"<>|]+','',hostname)
#テキストへ書き出し
config = output
#時間取得とファイル名組み立て
import datetime
now = datetime.datetime.now()
today = now.strftime('%Y%m%d-%H%M-%S')
output_file = hostname + '_' + today + '.log'
output_file = os.path.join(currentdir,'log',output_file)
#テキストファイル出力
with open(output_file , 'w') as backup:
backup.write(config)
print('Backup of ' + hostname + ' completed successfully')
print('#' * 30)
remote_host.disconnect()
#---コマンドリスト繰り返し
#---hostリストの順次実行
#---組み合わせ表の読み込み 繰り返し
Get_log_list.txt
#Host list,cmd list
cisco_list.txt,Cisco_cmd1.txt
cisco_ASA_list.txt,Cisco_ASA_cmd1.txt
cisco_list.txt
#Cisco cmd
show version
show run
show ip int b
show ip route
cisco_ASA_list.txt
#host or IP,username,password,device type
10.10.20.171,cisco,cisco,cisco_ios_telnet
Cisco_ASA_cmd1.txt
#CiscoASA cmd
terminal pager 0
show version
show run
show resource usage
show failover state
show route
実行結果
Connecting to 10.10.20.182
Entering enable mode ...
Backup of cisco@internet-host01~ completed successfully
##############################
Connecting to 10.10.20.181
Entering enable mode ...
Backup of internet-rtr01 completed successfully
##############################
Connecting to 10.10.20.172
Entering enable mode ...
Backup of edge-sw01 completed successfully
##############################
Connecting to 10.10.20.173
Entering enable mode ...
Backup of RP00CPU0core-rtr01 completed successfully
##############################
Connecting to 10.10.20.174
Entering enable mode ...
Backup of RP00CPU0core-rtr02 completed successfully
##############################
Connecting to 10.10.20.175
Entering enable mode ...
Backup of dist-rtr01 completed successfully
##############################
Connecting to 10.10.20.176
Entering enable mode ...
Backup of dist-rtr02 completed successfully
##############################
Connecting to 10.10.20.178
Entering enable mode ...
Backup of dist-sw02 completed successfully
##############################
Connecting to 10.10.20.177
Entering enable mode ...
Backup of dist-sw01 completed successfully
##############################
Connecting to 10.10.20.171
Entering enable mode ...
Backup of edge-firewall01 completed successfully
##############################
log
logフォルダへ保存されたテキスト
internet-rtr01_20230224-0055-48.log
edge-sw01_20230224-0055-54.log
RP00CPU0core-rtr01_20230224-0056-05.log
RP00CPU0core-rtr02_20230224-0056-15.log
dist-rtr01_20230224-0056-19.log
dist-rtr02_20230224-0056-23.log
dist-sw02_20230224-0056-30.log
dist-sw01_20230224-0056-37.log
edge-firewall01_20230224-0056-48.log
internet-rtr01_20230224-0055-48.log
#show version
Cisco IOS XE Software, Version 17.03.02
Cisco IOS Software [Amsterdam], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.3.2, RELEASE SOFTWARE (fc3)
~~~ 省略 ~~~
Configuration register is 0x2102
#show run
Building configuration...
Current configuration : 6728 bytes
!
! Last configuration change at 12:08:52 UTC Thu Feb 23 2023
!
version 17.3
service timestamps debug datetime msec
service timestamps log datetime msec
~~~ 省略 ~~~
!
end
#show ip int b
Interface IP-Address OK? Method Status Protocol
GigabitEthernet1 10.10.20.181 YES TFTP up up
GigabitEthernet2 172.31.252.1 YES TFTP up up
GigabitEthernet3 172.31.0.1 YES TFTP up up
GigabitEthernet4 unassigned YES unset administratively down down
Loopback0 unassigned YES unset administratively down down
#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, m - OMP
n - NAT, Ni - NAT inside, No - NAT outside, Nd - NAT DIA
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
H - NHRP, G - NHRP registered, g - NHRP registration summary
o - ODR, P - periodic downloaded static route, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
& - replicated local route overrides by connected
Gateway of last resort is not set
S 172.16.0.0/16 [1/0] via 172.31.252.2
172.31.0.0/16 is variably subnetted, 4 subnets, 2 masks
C 172.31.0.0/24 is directly connected, GigabitEthernet3
L 172.31.0.1/32 is directly connected, GigabitEthernet3
C 172.31.252.0/24 is directly connected, GigabitEthernet2
L 172.31.252.1/32 is directly connected, GigabitEthernet2
他は長いので省略。
他
エラー処理ができていないので対象が存在しないと止まる。
デバイスによっては、’secret’パラメータを書かないとエラーになりログインができなかった。
表示量が多いコマンドの場合、取得待ちtimeoutの調整が必要。show log,show tec はおそらく取れない。
コメント