这是我当前的代码:

fd = open('/var/log/secure.log')
syslog = fd.readlines()

countTable = {} # initialization of a dictionary
for line in syslog:
        data = line.split()
        if len(data) >= 15:
                if data[14] in ['user=root']:
                        intrusions = []
                        ip = data[13].replace('rhost=', "")
                        intrusions.extend(ip)
                        print "intruder:", intrusions


这是我得到的输出:

intruder: ['218.92.0.201']
intruder: ['222.186.173.142']
intruder: ['222.186.173.142']
intruder: ['222.186.173.142']
intruder: ['218.92.0.201']
intruder: ['218.92.0.201']
intruder: ['222.186.30.218']
intruder: ['218.92.0.201']


我怎样才能将所有这些IP放入同一“入侵”列表?

谢谢!

最佳答案

您只需要在第一个循环之前将intrusions = []移动:

countTable = {} # initialization of a dictionary

intrusions = []
for line in syslog:
        data = line.split()
        if len(data) >= 15:
                if data[14] in ['user=root']:

                        ip = data[13].replace('rhost=', "")
                        intrusions.extend(ip)
                        print "intruder:", intrusions

10-04 12:59