Populate Python dictionaries from .ini in RHEL5

58 views Asked by At

I'm in the process of creating a script for dumpcap.exe that would add some more features to it. I am writing it to be multi-platform and most of the MSWindows version is complete.

Currently I am moving what I have to REHL5 in order to start debugging and change what's needed in order to make the transition a little more seem-less. The MSWindows version has been written in Python 2.7, while RHEL5 runs Python 2.5.

The program is around 1200 lines so I will cut out the unnecessary stuff. I will try to be as informative as possible since I do not know where I could have gone wrong

My current problem is that my Variables{} dict is not populating.

Variables = {}                     

choice = ""                        
MAX_CHOICE = 0                     

DUMPCAP = None                     

section = None                     

dc_envfile = "cfg.ini"             
EVENTBAT = "eventaction.bat"       
FINALBAT = "finalaction.bat"       
DISPLAY_IFACES = None              
DC_PID = None                      

dc_priority = None                 
dc_mode =  None                    

#Dumpcap settings:                 
dc_interface = None                
dc_capfile = None                  
dc_capfilter = None                
dc_snaplen = None                  
dc_promisc = None                  
dc_bufsize = None                  
dc_pcapng =  None                  

def readFile(filename):                                   
    global section                                        
    myfile = open(filename, 'r+')                         
    #with open(filename) as myfile:                       
    for line in myfile:                                   
        raw_data, _, comments = line.partition("#")       
        raw_data = raw_data.strip()                       

        if raw_data[0:1] == '[':                          
            section = line[0:-2]                          
            Variables.setdefault(section, {})             
        else:                                             
            var, _, val = raw_data.partition("=")         
            var = var.strip(' ')                          
            val = val.strip(' ')                          
            Variables.setdefault(section, {})[var] = [val]
    myfile.close()           

readFile(dc_envfile)

The commented out line in readFile() is Python2.7 code that will not work in RHEL5. Virtualenv's are not an option.

The .ini file that is being read looks like this

[DC]
dc_capfile = test
dc_interface = 2
dc_ringtime = 
dc_priority = NORMAL
dc_maxtime = 
dc_snaplen = 
dc_bufsize = 20
dc_promisc = Y
dc_capfilter = icmp
dc_maxfiles = 
dc_maxsize = 
dc_mode = Dumpcap+Event
dc_pcapng = N
dc_ringsize = 
dc_maxpackets = 
dc_ringfiles =       

When This is run, I get the error

File "PythonDumpCap25.py", line 992, in setVariables
    if ''.join(map(str, Variables['DC']["dc_priority"])) is '':
KeyError: 'DC'

The setVariables() function includes more than this but the error occurs in the first few lines of it

def setVariables():
    global dc_mode, dc_priority
            ####### Dumpcap settings: #######
    global dc_interface, dc_capfile, dc_capfilter, dc_snaplen
    global dc_promisc, dc_bufsize, dc_pcapng
            ####### Stop conditions: #######
    global dc_maxpackets, dc_maxtime, dc_maxsize, dc_maxfiles
            ####### Ringbuffer settings: #######
    global dc_ringtime, dc_ringsize, dc_ringfiles
            ####### Capture event settings: #######
    global dc_ev_interface, dc_ev_capfilter, dc_ev_count
    global dc_ev_kill, dc_ev_delay
            ####### Mailsend settings: #######
    global ms, ms_smtp_server, ms_smtp_port, ms_sendto
    global ms_cc, ms_bcc, ms_rrr, ms_from_name, ms_from
    global ms_replyto, ms_importance, ms_sensitivity
    global ms_security, ms_user, ms_attach, ms_maxattach
    global ms_subject, ms_pm

    global SMTP_USER_PASS

    if ''.join(map(str, Variables['DC']["dc_priority"])) is '':
        dc_priority = "NORMAL"
    else:
        dc_priority = ''.join(map(str, Variables['DC']["dc_priority"]))

And when i run my printVariables() function it prints an empty dictionary.

Why doesn't my Variables{} dict populate?

0

There are 0 answers