首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python不会写入文本文件

Python不会写入文本文件
EN

Stack Overflow用户
提问于 2016-03-13 15:05:50
回答 1查看 429关注 0票数 0

如果ip地址发生超过30次,我将尝试使用以下代码将ip地址从一个文件写入另一个文件:

代码语言:javascript
复制
#!/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)

但是它不会写到文本文件,唯一写到文本文件的是第一行‘IP地址有30多个攻击:’这里有什么不允许我将ip_address从文件写到另一个文件的错误吗?

日志文件中的示例行:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-03-13 15:12:58

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

代码语言:javascript
复制
if ip_attacks[attack_address] > 30:
        myTxtFile.write(ip_attacks)

而不是:

代码语言:javascript
复制
if count_attacks > 30:
        myTxtFile.write(ip_attacks)

编辑:.我相信这3行:

代码语言:javascript
复制
count_attacks = ip_attacks[attack_address]
count_attacks = count_attacks +1
ip_attacks[attack_address] = count_attacks

可替换为:

代码语言:javascript
复制
ip_attacks[attack_address] += 1

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

代码语言:javascript
复制
#!/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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35971839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档