我正在为此创建一个脚本,dumpcap.exe
这将为其添加更多功能。我正在编写它是多平台的,并且大多数 MSWindows 版本都是完整的。
目前,我正在移动我必须做REHL5
的事情,以便开始调试并更改所需的内容,以使过渡看起来更简洁。MSWindows 版本已写入Python 2.7
,同时RHEL5
运行Python 2.5
。
该程序大约有 1200 行,因此我将删除不必要的内容。我会尽量提供信息,因为我不知道我可能哪里出错了
我目前的问题是我的Variables{}
字典没有填充。
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)
注释掉的行readFile()
是Python2.7
在 RHEL5 中不起作用的代码。Virtualenv
的不是一种选择。
正在读取的 .ini 文件如下所示
[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 =
运行时,我得到error
File "PythonDumpCap25.py", line 992, in setVariables
if ''.join(map(str, Variables['DC']["dc_priority"])) is '':
KeyError: 'DC'
该setVariables()
函数包含的内容不止于此,但错误发生在它的前几行
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"]))
当我运行我的printVariables()
函数时,它会打印一个空字典。