【问题标题】:Formatting text using either linux or python使用 linux 或 python 格式化文本
【发布时间】:2018-12-17 10:14:27
【问题描述】:

我是脚本新手,所以任何帮助都会大有帮助,

它更多与Cisco路由器配置文件有关,第一种格式是当你运行命令“show running-config”时,第二部分是当你使用命令“show running-config正式”时

所以,我想将我的 config txt 文件转换为以后的格式。

我的 linux 操作系统中有一个文件,其中包含以下数据,

control-plane     
 management-plane   
  inband    
   interface all   
    allow xy   
     address  x.x.x.x   
     address  y.y.y.y   

    !   
    allow b   
     address  b.b.b.b   
    !  
   !  
  !  

我想要的是删除它的格式,所以基本上像这样将子部分附加到父级,或者使用 linux 命令或使用 python 编写脚本。你认为最好的方法是什么?

control-plane management-plane inband interface all allow xy   
control-plane management-plane inband interface all allow xy address  x.x.x.x   
control-plane management-plane inband interface all allow xy address  y.y.y.y   
control-plane management-plane inband interface all allow b   
control-plane management-plane inband interface all allow b address b.b.b.b

使用相同的概念,我想在同一个文件中进一步重新格式化,

service sesh instance1
 service-location preferred-active 0/3/CPU0
 service-type nps nps-1
  forced-placement npu 0
  tunnel type gre
   name gre10
   tunnel-destination ipv4 address 209.165.200.225
   ipv4 address 192.0.2.6/24
   remote ipv4 address 192.0.2.5/24
   tunnel-source ipv4 address 209.165.200.226
  !
  package nps-mips64-r2.rpm
  interface ServiceApp1
   remote ipv4 address 209.165.200.227/24
  !
 !
!

template pre-pos
 interface preconfigure POS0/1/0/0
  ipv4 address 10.3.32.154 255.0.0.0 
!

我还附上了an image,只是为了给一个更清晰的图片。

谢谢

萨尔

【问题讨论】:

  • 我不知道你是想在“允许”和“地址”行上输出,还是在树的最低两层,或者什么。我采用的方法是将文件转换为 Python 结构(在本例中为 list-of-lists-of-lists ...),将输入表示为树。然后,您可以将该树转换为您喜欢的任何序列化输出格式。
  • @MikeHousky- 嗨 Mike,所以我不确定你是否熟悉路由器,但第一个是当你运行命令“show running-config”时,第二部分是当你使用命令“显示运行配置正式”我想将我的配置文件转换为以后的格式。
  • 不...无法识别该格式。如果这是一次性的,看起来你有一个答案。我的方法会比你真正需要的要重得多。
  • 为什么需要或需要部分命令control-plane management-plane inband interface all allow xy… b

标签: python linux scripting


【解决方案1】:

希望这会有所帮助:

file = open('text.txt','r')
allow_indices=[]
exl_indices=[]
content=[]
i = 0
for line in file:
    if 'allow' in line:
        allow_indices.append(i)
    if '!' in line:
        exl_indices.append(i)
    content.append(line)
    i += 1
file = open('text.txt','r')

base=''
formatted=''

for i in range(0,allow_indices[0]):
    base+= content[i]

close_index=0
for i in allow_indices:
    new_base = base+content[i]
    formatted+=new_base.replace('\n','')+'\n'
    for j in range(i+1,exl_indices[close_index]-1):
        formatted+=new_base.replace('\n','')+content[j].replace('\n','')+'\n'
        close_index+=1

print formatted

我使用以下内容作为 text.txt 文件的内容:

control-plane
 management-plane
  inband
   interface all
    allow xy
     address x.x.x.x
     address y.y.y.y

    !
    allow b
     address  b.b.b.b
    !
   !
  !

结果如下:

control-plane      management-plane     inband       interface all       allow xy   
control-plane      management-plane     inband       interface all       allow xy        address x.x.x.x   
control-plane      management-plane     inband       interface all       allow xy        address y.y.y.y   
control-plane      management-plane     inband       interface all       allow b   
control-plane      management-plane     inband       interface all       allow b        address  b.b.b.b 

【讨论】:

  • 你认为会有更有效的方法来重新格式化它??我在上面的实际帖子中更新了更多文本数据,这将无法通过这些检查。我在想也许会打破!标记但尚未成功。
  • 如果您将“隧道类型”和“包”索引添加到 allow_indices 表中,它应该可以工作,可以通过在行中添加此“如果 '隧道类型':allow_indices.append(i)”来做到这一点在第 6 行之后,对于包也是如此
  • 如果你不介意的话,你能带我看一下例子吗,这将帮助我快速上手。
猜你喜欢
  • 2013-08-16
  • 2023-03-03
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
相关资源
最近更新 更多