快速提問;所以基本上我試圖打印爲下面的結果,如果關鍵詞是發現如果找到字符串打印n較早的行
Keyword = ['Dn']
Output = ISIS Protocol Information for ISIS(523)
---------------------------------------
SystemId: 0101.7001.1125 System Level: L2
Area-Authentication-mode: NULL
Domain-Authentication-mode: NULL
Ipv6 is not enabled
ISIS is in restart-completed status
Level-2 Application Supported: MPLS Traffic Engineering
L2 MPLS TE is not enabled
ISIS is in protocol hot standby state: Real-Time Backup
Interface: 10.170.11.125(Loop0)
Cost: L1 0 L2 0 Ipv6 Cost: L1 0 L2 0
State: IPV4 Up IPV6 Down
Type: P2P MTU: 1500
Priority: L1 64 L2 64
Timers: Csnp: L12 10 , Retransmit: L12 5 , Hello: 10 ,
Hello Multiplier: 3 , LSP-Throttle Timer: L12 50
Interface: 10.164.179.218(GE0/5/0)
Cost: L1 10 L2 10 Ipv6 Cost: L1 10 L2 10
State: IPV4 Mtu:Up/Lnk:Dn/IP:Dn IPV6 Down
Type: BROADCAST MTU: 9497
Priority: L1 64 L2 64
Timers: Csnp: L1 10 L2 10 ,Retransmit: L12 5 , Hello: L1 10 L2
Hello Multiplier: L1 3 L2 3 , LSP-Throttle Timer: L12 50
Interface: 10.164.179.237(GE0/6/0)
Cost: L1 1000 L2 1000 Ipv6 Cost: L1 10 L2 10
State: IPV4 Up IPV6 Down
Type: BROADCAST MTU: 9497
Priority: L1 64 L2 64
Timers: Csnp: L1 10 L2 10 ,Retransmit: L12 5 , Hello: L1 10 L2 10 ,
Hello Multiplier: L1 3 L2 3 , LSP-Throttle Timer: L12 50
因此,如果「DN」輸出打印發現最後兩行,所以預期輸出應該
Interface: 10.164.179.218(GE0/5/0)
Cost: L1 10 L2 10 Ipv6 Cost: L1 10 L2 10
State: IPV4 Mtu:Up/Lnk:Dn/IP:Dn IPV6 Down
使用摘錄如下:
with open(host1 + ".txt","w") as f:
else:
if (">") in output:
output = net_connect.send_command("screen-length 0 temporary", delay_factor=1)
print (output)
output = net_connect.send_command("dis isis brief", delay_factor=1)
print (output)
f.write(output)
hosts = open((hostsfile) , "r")
keys = ['Dn']
hosts = [hosts for hosts in (hosts.strip() for hosts in open(hostsfile)) if hosts]
for host2 in hosts:
for line in f:
for keywords in keys:
if keywords in line:
print (line)
我希望這可以解釋,也親切地忽略,如文件操作等小問題,作爲主要關注的是輸出的N-2行,如果字符串中發現
謝謝你;快速的;我該如何從輸出中刪除雙轉換:(對不起,我只是開始學python – Saadi381
@ saadi381 - 對不起,錯字...它應該是'print(item)',而不是'print(dqu)'。 – mgilson