保存文本文件的一部分
Save one part of a text file
我使用 Rancid 服务器保存有关网络上 Cisco 交换机的信息。我想写一个 Python 3 脚本来将数据的配置部分提取到文本文件中。我已经让它工作了,但我有一个文件,其中有两次配置,我只想要第一个配置。
这是我所做的:
import sys
flag = False
start = ('version', 'config-register')
end = ('@\n', 'monitor 6\n', 'end\n')
with open(sys.arbv[1], "r") as file:
for line in file:
if line.startswith(start):
file = True;
if flag:
sys.stdout.write(line)
if line.endswith(end):
flag = False
具有两次配置的文件使用 'version'
开始,'@\n'
结束。我尝试使用 break
但我仍然得到两个配置。
文件示例:
!VTP:VTP 域名:
!VTP:VTP 修剪模式:已禁用(操作上已禁用)
!VTP:VTP V2 模式:已禁用
!VTP:VTP 陷阱生成:已禁用
!VTP:MD5 摘要:0x05 0xBB 0x45 0x03 0x57 0xBE 0xBA 0x57
!VTP:VTP 版本 运行:1
!
!DEBUG:调试级别设置为 Minor(1)
!DEBUG:新会话的默认日志记录级别:3
!
!CORES:模块实例进程名PID日期(年月日时间)
!CORES: ------ ------ -------------- ---------- ---------- --------------
!
!PROC_LOGS: 进程 PID 正常退出堆栈核心日志创建时间
!PROC_LOGS: -------------- ------ ---------- ----- ----- - --------------
!
version 5.2(1)N1(4)
logging level feature-mgr 0
hostname
no feature telnet
feature tacacs+
cfs eth distribute
feature udld
feature interface-vlan
feature lacp
feature vpc
feature lldp
feature vtp
feature fex
username (removed)
**** content removed ****
Interface Section
clock timezone CTD -6 0
line console
exec-timeout 5
line vty
session-limit 5
session-limit 5
exec-timeout 5
access-class 3 in
boot kickstart
boot system
ip route
no ip source-route
@
1.75
log
@updates
@
text
版本和配置注册的顺序在这里很重要。所以你最好只遍历文件两次。通过这种方式,您可以将需要查找的文件部分拆分为组。一次查找版本的值,一次查找配置寄存器。
import sys
flag = False
VERSION_NAME = r'version'
CONFIG_NAME = r'config-register'
end = ('@\n', 'monitor 6\n', 'end\n')
FILE_NAME = sys.argv[1]
# find the config-register
with open(FILE_NAME, "r") as file:
start = CONFIG_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True # correction
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break # correction
if line.startswith(start):
config_found = True
# find the version
with open(FILE_NAME, "r") as file:
start = VERSION_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break
if line.startswith(start):
config_found = True
然后您将再次循环并只搜索配置版本和 "end"。这不是最注重性能的,但由于您没有处理整个文件,希望这能满足您的需求。
我使用 Rancid 服务器保存有关网络上 Cisco 交换机的信息。我想写一个 Python 3 脚本来将数据的配置部分提取到文本文件中。我已经让它工作了,但我有一个文件,其中有两次配置,我只想要第一个配置。
这是我所做的:
import sys
flag = False
start = ('version', 'config-register')
end = ('@\n', 'monitor 6\n', 'end\n')
with open(sys.arbv[1], "r") as file:
for line in file:
if line.startswith(start):
file = True;
if flag:
sys.stdout.write(line)
if line.endswith(end):
flag = False
具有两次配置的文件使用 'version'
开始,'@\n'
结束。我尝试使用 break
但我仍然得到两个配置。
文件示例: !VTP:VTP 域名: !VTP:VTP 修剪模式:已禁用(操作上已禁用) !VTP:VTP V2 模式:已禁用 !VTP:VTP 陷阱生成:已禁用 !VTP:MD5 摘要:0x05 0xBB 0x45 0x03 0x57 0xBE 0xBA 0x57 !VTP:VTP 版本 运行:1 ! !DEBUG:调试级别设置为 Minor(1) !DEBUG:新会话的默认日志记录级别:3 ! !CORES:模块实例进程名PID日期(年月日时间) !CORES: ------ ------ -------------- ---------- ---------- -------------- ! !PROC_LOGS: 进程 PID 正常退出堆栈核心日志创建时间 !PROC_LOGS: -------------- ------ ---------- ----- ----- - -------------- !
version 5.2(1)N1(4)
logging level feature-mgr 0
hostname
no feature telnet
feature tacacs+
cfs eth distribute
feature udld
feature interface-vlan
feature lacp
feature vpc
feature lldp
feature vtp
feature fex
username (removed)
**** content removed ****
Interface Section
clock timezone CTD -6 0
line console
exec-timeout 5
line vty
session-limit 5
session-limit 5
exec-timeout 5
access-class 3 in
boot kickstart
boot system
ip route
no ip source-route
@
1.75
log
@updates
@
text
版本和配置注册的顺序在这里很重要。所以你最好只遍历文件两次。通过这种方式,您可以将需要查找的文件部分拆分为组。一次查找版本的值,一次查找配置寄存器。
import sys
flag = False
VERSION_NAME = r'version'
CONFIG_NAME = r'config-register'
end = ('@\n', 'monitor 6\n', 'end\n')
FILE_NAME = sys.argv[1]
# find the config-register
with open(FILE_NAME, "r") as file:
start = CONFIG_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True # correction
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break # correction
if line.startswith(start):
config_found = True
# find the version
with open(FILE_NAME, "r") as file:
start = VERSION_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break
if line.startswith(start):
config_found = True
然后您将再次循环并只搜索配置版本和 "end"。这不是最注重性能的,但由于您没有处理整个文件,希望这能满足您的需求。