Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am very new to python and I need to read below file (Actually below output get it from command ) and print its output.

Below is the file:

NIC:
   Name: eth0
   ID: 0 1
   Dynamic MAC Addr: 00:00:00:00:00:01
   Preference: NONE

   Ethernet Interface:
      Name: prod
      Default Network: Yes
      VLAN ID: 2
      Operational VLAN: SWA
      Config Qual: N/A

      Name: dev
      Default Network: No
      VLAN ID: 3
      Operational VLAN: SWA
      Config Qual: N/A

   Name: eth1
   Fabric ID: 1 0
   Dynamic MAC Addr: 00:00:00:00:00:11
   Preference: NONE
   Ethernet Interface:
      Name: prod
      Default Network: Yes
      VLAN ID: 2
      Operational VLAN: SWB
      Config Qual: N/A

      Name: dev
      Default Network: No
      VLAN ID: 3
      Operational VLAN: SWB
      Config Qual: N/A
  1. What actually i what to achieve is i need to find how many interface i have. In above case eth0 and eth1.

  2. I want verify eth0 has ID 0 1

  3. For eth0, i need to verify whether Name prod exists and vlan ID 2 is for prod and Default Network is Yes

  4. For eth0, i need to verify whether Name dev exists and vlan ID 3 is for dev and Default Network is no.

similarly for all the interface. Usually i will get eth0 - 20 in output.

What i tried so far to achieve this and able to make it work using below method but below code is not at correct because it wont work if fields are interchanged.

What i actually doing is finding eth0 and redirecting below values to var 1, var2 , var3 and so on and then using these var1, var2, var3 to get my output.

but this wont work when fields interchange and when extra fields are added to output file.

(Note below code is example that i am trying)

I need your helps here to achieve this in proper way.

from itertools import islice
import string
import re
import os
import sys
a = "eth0"
def silentremove(filename): # remove file
   try:
       os.remove(filename)
   except OSError as e: # this would be "except OSError, e:" 
       if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
          raise # re-raise exception if a different error occurred
file = "inputfile.txt"
silentremove(file)
with open("vnic1.txt") as f: # Remove empty lines
   for line in f:
      if not line.isspace():
          input_file = open('inputfile.txt', 'a')
          sys.stdout = input_file
          sys.stdout.write(line)
          input_file.close()
          sys.stdout = sys.__stdout__
d = {}
count = 0
with open('inputfile.txt') as f:
   for line in f:
      if (re.search(a, line, re.IGNORECASE)):
         var1, var2, var3, var4, var5,var6,var7,var8,var9,var10,var11,var12,var13, var14, var15, var16 = islice(f, 16)
         key, value = var1.strip().split(":", 1)
         d[key] = value
         ideth0 = "0 1"
         if (re.search(ideth0, value, re.IGNORECASE)):
            count +=1
         key, value = var5.strip().split(":", 1)
         d[key] = value
         name = "prod"
         if (re.search(name, value, re.IGNORECASE)):
            count +=1
         if count == 2:
            print "Working"
            print "Status-{}:".format(a)
            print "  ID    Name"
            print "  {}    {}".format(ideth0, name)

         else:
          print "Failed"   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

the text looks la YAML, but it's not, but it could be

with open("vnic1.txt") as f:
   data = f.read()
# makeing it yaml-string
data = dat.replace("  Name","- Name") # the 2 spaces are intended
import yaml
data = yaml.load(data) # you now have the entire structure as a huge dict, profit

this way you'll have in data something like this

{
    'NIC': [
        {'Dynamic MAC Addr': '00:00:00:00:00:01',
         'Ethernet Interface': [{'Config Qual': 'N/A',
                                 'Default Network': True,
                                 'Name': 'prod',
                                 'Operational VLAN': 'SWA',
                                 'VLAN ID': 2},
                                {'Config Qual': 'N/A',
                                 'Default Network': False,
                                 'Name': 'dev',
                                 'Operational VLAN': 'SWA',
                                 'VLAN ID': 3}],
         'ID': '0 1',
         'Name': 'eth0',
         'Preference': 'NONE'},
        {'Dynamic MAC Addr': '00:00:00:00:00:11',
         'Ethernet Interface': [{'Config Qual': 'N/A',
                                 'Default Network': True,
                                 'Name': 'prod',
                                 'Operational VLAN': 'SWB',
                                 'VLAN ID': 2},
                                {'Config Qual': 'N/A',
                                 'Default Network': False,
                                 'Name': 'dev',
                                 'Operational VLAN': 'SWB',
                                 'VLAN ID': 3}],
         'Fabric ID': '1 0',
         'Name': 'eth1',
         'Preference': 'NONE'}
    ]
}

which might not be what you asked for, but will definitely be a lot more easy to navigate (at least you have the sections correctly separated)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...