我有一系列路由器的以下輸出:
hostname CPE-ABCDEFGHI-55553-03
Interface IP-Address OK? Method Status Protocol
FastEthernet0 unassigned YES unset up up
FastEthernet1 unassigned YES unset up down
FastEthernet2 unassigned YES unset up down
FastEthernet3 unassigned YES unset up up
FastEthernet4 unassigned YES NVRAM up up
FastEthernet4.100 93.62.88.112 YES DHCP up up
FastEthernet4.106 182.21.200.233 YES DHCP up up
Vlan1 12.230.1.34 YES NVRAM up up
###############################################
hostname CPE-XXXXXXXXX-33333-01
Interface IP-Address OK? Method Status Protocol
Async1 unassigned YES unset down down
FastEthernet0 unassigned YES unset up up
FastEthernet1 unassigned YES unset down down
FastEthernet2 unassigned YES unset down down
FastEthernet3 unassigned YES unset down down
FastEthernet4 unassigned YES unset down down
FastEthernet5 unassigned YES unset down down
FastEthernet6 unassigned YES unset down down
FastEthernet7 unassigned YES unset down down
FastEthernet8 unassigned YES NVRAM administratively down down
GigabitEthernet0 unassigned YES DHCP up up
GigabitEthernet0.100 1.1.1.1 YES DHCP up up
GigabitEthernet0.106 152.21.133.171 YES DHCP up up
NVI0 unassigned YES unset administratively down down
Vlan1 10.241.0.162 YES NVRAM up up
Vlan10 10.240.0.194 YES NVRAM up up
###############################################
hostname CPE-YYYYYYYYY-28886-03
Interface IP-Address OK? Method Status Protocol
Cellular0 unassigned YES NVRAM up up
Dialer1 192.168.255.3 YES IPCP up up
FastEthernet0 unassigned YES unset up up
FastEthernet1 unassigned YES unset up down
FastEthernet2 unassigned YES unset up down
FastEthernet3 unassigned YES unset up down
FastEthernet4 unassigned YES NVRAM administratively down down
Loopback0 173.17.18.193 YES NVRAM up up
NVI0 18.10.10.1 YES unset up up
Vlan1 10.241.0.163 YES NVRAM up up
Vlan10 10.240.0.195 YES NVRAM up up
###############################################
hostname CPE-AAAAAAAAA-22222-03
Interface IP-Address OK? Method Status Protocol
FastEthernet0 unassigned YES unset up up
FastEthernet1 unassigned YES unset up down
FastEthernet2 unassigned YES unset down down
FastEthernet3 unassigned YES unset up down
FastEthernet4 unassigned YES NVRAM up up
FastEthernet4.100 46.189.234.214 YES DHCP up up
FastEthernet4.106 172.21.128.240 YES DHCP up up
Vlan1 10.241.7.253 YES NVRAM up up
Vlan10 10.270.1.35 YES NVRAM up up
###############################################
我需要將其轉換為每個介面帶有一列的 Excel 檔案。為此,我需要制作一個 python 2.7.5(ssh 服務器的限制)腳本來將文本決議為 Excel。
例如(想象這些是細胞)
| 主機名 | 界面 | IP地址 | 好? | 方法 | 地位 | 協議 |
|---|---|---|---|---|---|---|
| CPE-ABCDEFGHI-55553-03 | 快速以太網0 | 未分配 | (...) | ... | ... | ... |
| CPE-ABCDEFGHI-55553-03 | 快速以太網1 | 未分配 | (...) | ... | ... | ... |
你能幫我做這個嗎?
uj5u.com熱心網友回復:
也許是這樣的?
output = []
with open('Output.txt','r') as fil:
for line in fil:
output.append(line.strip())
mydata = []
host_curr = ''
for element in output:
if element.startswith('####') or element.startswith('Interface'):
continue
if element.startswith('hostname'):
host_curr = element.split()[1]
continue
myline = host_curr '\t' element
mydata.append(myline)
with open('Output.csv','w') as out:
for elem in mydata:
out.write('{}\n'.format(elem))
如果輸出是您的輸出(這里是文本行串列)。csv 應該是這樣的:
CPE-ABCDEFGHI-55553-03 FastEthernet0 unassigned YES unset up up
CPE-ABCDEFGHI-55553-03 FastEthernet1 unassigned YES unset up down
CPE-ABCDEFGHI-55553-03 FastEthernet2 unassigned YES unset up down
CPE-ABCDEFGHI-55553-03 FastEthernet3 unassigned YES unset up up
CPE-ABCDEFGHI-55553-03 FastEthernet4 unassigned YES NVRAM up up
CPE-ABCDEFGHI-55553-03 FastEthernet4.100 93.62.88.112 YES DHCP up up
CPE-ABCDEFGHI-55553-03 FastEthernet4.106 182.21.200.233 YES DHCP up up
CPE-ABCDEFGHI-55553-03 Vlan1 12.230.1.34 YES NVRAM up up
CPE-XXXXXXXXX-33333-01 Async1 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet0 unassigned YES unset up up
CPE-XXXXXXXXX-33333-01 FastEthernet1 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet2 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet3 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet4 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet5 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet6 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet7 unassigned YES unset down down
CPE-XXXXXXXXX-33333-01 FastEthernet8 unassigned YES NVRAM administratively down down
CPE-XXXXXXXXX-33333-01 GigabitEthernet0 unassigned YES DHCP up up
CPE-XXXXXXXXX-33333-01 GigabitEthernet0.100 1.1.1.1 YES DHCP up up
CPE-XXXXXXXXX-33333-01 GigabitEthernet0.106 152.21.133.171 YES DHCP up up
CPE-XXXXXXXXX-33333-01 NVI0 unassigned YES unset administratively down down
CPE-XXXXXXXXX-33333-01 Vlan1 10.241.0.162 YES NVRAM up up
CPE-XXXXXXXXX-33333-01 Vlan10 10.240.0.194 YES NVRAM up up
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/357678.html
標籤:Python python-2.7 解析
下一篇:子行程解壓縮源檔案洗掉?
