2

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?

  • Is there a reason you haven't set `Variables` as a global in your `readFile` function? – SuperBiasedMan Jun 24 '15 at 19:22
  • 3
    There's a module for parsing .ini (config) files in the standard library, I suggest you take a look: https://docs.python.org/2/library/configparser.html – pawroman Jun 24 '15 at 19:23
  • This seems wrong: `section = line[1:-2]` Slicing isn't inclusive, so you've likely only set the key as `'D'`. I also don't think that the newline character is being retained if that's why you used those numbers to slice. – SuperBiasedMan Jun 24 '15 at 19:32
  • Also have you tried just using a normal `print` on `Variables`? `printVariables()` could have problems in it. – SuperBiasedMan Jun 24 '15 at 19:34
  • @paw I have utilized the ConfigParser module in other places and it works wonderfully. – Googlesomething Jun 24 '15 at 19:35
  • @sup `printVariables()` is the name of the function as is `def printVariables();` – Googlesomething Jun 24 '15 at 19:36
  • @sup also `Variables` is the outermost layer of the script along with the actual variable declarations the follow. They are not inside a function. – Googlesomething Jun 24 '15 at 19:38
  • @sup There was no reason `Variables` wasn't global, but the change has been made with no effect. – Googlesomething Jun 24 '15 at 19:47
  • Hmm, I just ran a version of this in 2.7 and it seemed to work OK. I think you are going to have to dig in deeper to find where it is going wrong: What happens if you `print Variables` immediately after the call to `readFile`? What about if you print it after each line read from the ini file? – strubbly Jun 24 '15 at 22:16
  • @strub It works fine for me in 2.7 also, the problem is when I try to run it in 2.5, I'm not sure where the backporting error is – Googlesomething Jun 25 '15 at 13:08
  • @strub I printed `Variables` after each read line and it showed that `readFile()` was creating the section as `'DC']` leaving out the first `[`. I'll edit my question to fix that, but the same error is still given after the fix. – Googlesomething Jun 25 '15 at 13:15
  • @paw IS there a way to use `ConfigParser` to populate the dictionary and fill in default values if the `.ini` file option is `none`? – Googlesomething Jun 25 '15 at 13:20
  • @st changing the `section = line[]` to `section = line[1:-1] invoked the same error also – Googlesomething Jun 25 '15 at 13:26

0 Answers0