adding lines to a file in it doesnt exist in python
Question: so what i am doing is first reading the /var/log/secure file for ipaddresses
that i want to block and saving that list to a file. then i am trying read
that file and determine if the address is outside of the US, and if it is,
then i want to block the address in the iptables.

i also want to predetermine if that address has already been added to the
iptables before i write to it:

    
    
    #!/usr/bin/env python
    
    import GeoIP
    import os
    
    os.system("cat /var/log/secure | grep 'Failed password for invalid user' | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' >> /home/user/ipaddresses.txt")
    
    iptables_file = open('/etc/sysconfig/iptables', 'r')
    loglist = iptables_file.readlines()
    iptables_file.close()
    found = False
    
    gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
    
    for line in file("/home/user/ipaddresses.txt", "r"):
        line = line[:-1] # strip the last from the line
        #print line, "-", gi.country_code_by_addr(line)
    
        if str(gi.country_code_by_addr(line)) != "US":
                for ip in loglist:
                        if str(line) in line:
                                print str(line) + " found in iptables, skipping ip address"
                                break
                else:
                        print "adding " + str(line) + " to iptables"
                        #os.system("iptables -A INPUT -s " + str(line) + " -j DROP")
    
    #os.system("service iptables save")
    

obviously i dont want to write to my iptables until i know that its working so
thats why i have it commented out. what am i doing wrong? this is the error:

    
    
    grep: write error
    cat: write error: Broken pipe
    


Answer: The second grep command:

grep -oE '((1?[0-9]...[0-5])' /var/log/secure

has /var/log/secure as an argument. That should not be there. It is reading
the file and not reading standard input, and that is causing the error.

