【问题标题】:Python Won't write to text filePython不会写入文本文件
【发布时间】:2016-06-28 13:33:07
【问题描述】:

我正在尝试使用以下代码将 IP 地址从一个文件写入另一个文件,如果它们出现超过 30 次:

#!/usr/bin/python

#open the auth.log file
myFile = open('auth.log','r') 

myTxtFile = open('blacklist2.txt','w') #open the Security_Test.txt for writing later

myTxtFile.write('The IP Addresses with more than 30 Attacks are:\n') #prints out a line of text ready for the outcome

ip_attacks = {}
count_attacks = 0

#go through each line of the file and return it to the variable line
for line in myFile.readlines():

    #get the IP address
    #we are working backwards to avoid the difference of the length of the NT logs
    attack_ip = list_of_line[-4]
    attack_ip_list= attack_ip.split('port')
    attack_address = attack_ip_list[0]
    if 'Failed password for' in line:
        #print '\'',attack_address,'\''
        if ip_attacks.has_key(attack_address):
            count_attacks = ip_attacks[attack_address]
            count_attacks = count_attacks +1
            ip_attacks[attack_address] = count_attacks
            #zero out the temporary counter as a precaution
            count_attacks =0 
        else:
            ip_attacks[attack_address] = 1
        if count_attacks > 30:
            myTxtFile.write(ip_attacks)

但它不会写入文本文件,写入文本文件的唯一内容是第一行“具有 30 次以上攻击的 IP 地址是:”我在这里做错了什么吗?允许我将文件中的 ip_address 写入另一个文件??

日志文件中的示例行:

Feb  5 08:25:47 j4-be02 sshd[2130]: Failed password for root from 5.199.133.223 port 50259 ssh2
Feb  5 08:25:55 j4-be02 sshd[2133]: Failed password for root from 5.199.133.223 port 57329 ssh2

【问题讨论】:

  • 似乎你的代码永远不会通过if count_attacks > 30:分支

标签: python apache python-2.7 logging


【解决方案1】:

您的代码错误,因为您将count_attacks 重置为零。我相信你希望你的 if 语句是:

if ip_attacks[attack_address] > 30:
        myTxtFile.write(ip_attacks)

代替:

if count_attacks > 30:
        myTxtFile.write(ip_attacks)

编辑: 顺便提一句。我相信这 3 行:

count_attacks = ip_attacks[attack_address]
count_attacks = count_attacks +1
ip_attacks[attack_address] = count_attacks

可以替换为:

ip_attacks[attack_address] += 1

编辑:问题的完整解决方案:

#!/usr/bin/python

from collections import defaultdict
#open the auth.log file
myFile = open('auth.log','r') 

myTxtFile = open('blacklist2.txt','w') #open the Security_Test.txt for writing later

myTxtFile.write('The IP Addresses with more than 30 Attacks are:\n') #prints out a line of text ready for the outcome

ip_attacks = defaultdict(int)
count_attacks = 0

#go through each line of the file and return it to the variable line
for line in myFile.readlines():

#get the IP address
#we are working backwards to avoid the difference of the length of the NT logs
attack_ip = list_of_line[-4]
attack_ip_list= attack_ip.split('port')
attack_address = attack_ip_list[0]
if 'Failed password for' in line:
    #print '\'',attack_address,'\''
    ip_attacks[attack_address] += 1


for key, value in ip_attacks.iteritems():
    if value > 30:
        myTxtFile.write(key)

【讨论】:

  • 我在这一行不断收到错误 myTxtFile.write(ip_attacks) TypeError: expected a character buffer object
  • ip_attacks 是一本字典。您需要将字符串或字符缓冲区对象传递给 write() 函数。
  • 我现在可以使用它了,但它仍然没有将我想要的内容写入文本文件,它正在将每个 IP 写入文件,它应该只声明 2-3 个 IP 地址跨度>
  • 是的,我将 count_attacks = 0 替换为新的 if 语句,并将 3 行替换为新行
  • 不,您不应将 count_attacks = 0 替换为 if 语句。您应该用新的 if 语句替换旧的 if 语句: if count_attacks > 30: myTxtFile.write(ip_attacks) 。抱歉,如果不清楚。我编辑了我的答案以更清楚。
猜你喜欢
  • 2013-05-18
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
相关资源
最近更新 更多