Python:一个用来批量处理文件违规内容并记录的脚本

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错
Python:一个用来批量处理文件违规内容并记录的脚本

初稿
#!/usr/bin/env python
from glob import glob
if __name__ == “__main__”:
            ret = {}
            for f in glob(“/home/xinchang.jl/admin_auth/*”):
                    for line in open(f).readlines():
                            if not line or len(line.split()) < 3:
                                    continue
                            user = line.split()[-1]
                            file = f.split(“/”)[-1]
                            if user.split(“@”)[0] != file.split(“@”)[0]:
                                    ret[line.split()[-1]] = file
                            else:
                                    pass
            print ret
第二次修改:
#!/usr/bin/env python
from glob import glob
if __name__ == “__main__”:
            ret = {}
            a = 0
            def add_key(username,hostname):
                    global ret
                    if ret.has_key(username):
                            ret[username] += “,” + hostname
                    else:
                            ret[username] = hostname
            for f in glob(“/home/xinchang.jl/admin_auth/*”):
                    for line in open(f).readlines():
                            if not line or len(line.split()) < 3:
                                    continue
                            elif line.split()[0] == ‘ssh-rsa’ or line.split()[0] == ‘ssh-dss’:
                                    user = line.split()[-1]
                                    file = f.split(“/”)[-1]
                                    if user.split(“@”)[0] != file.split(“@”)[0]:
                                            add_key(user,file)
                            else:
                                    pass
            for host_name in ret.keys():
                           print host_name,”:”
                           h = ret[host_name].split(“,”)
                           for host in h:
                                    print ”  “,host
第三次修改:
#!/usr/bin/env python
from glob import glob
from optparse import OptionParser
parser = OptionParser(usage=”%prog [-f] [-q]”,version=”%prog 1.0″)
parser.add_option(“-f”,”–file”,dest=”filename”,default=”filename”,type=”string”)
(options,args) = parser.parse_args()
if __name__ == “__main__”:
            ret = {}
            a = 0
            def add_key(username,hostname):
                    global ret
                    if ret.has_key(username):
                            ret[username] += “,” + hostname
                    else:
                            ret[username] = hostname
            for f in glob(options.filename):
                    for line in open(f).readlines():
                            if not line or len(line.split()) < 3:
                                    continue
                            elif line.split()[0] == ‘ssh-rsa’ or line.split()[0] == ‘ssh-dss’:
                                    user = line.split()[-1]
                                    file = f.split(“/”)[-1]
                                    if user.split(“@”)[0] != file.split(“@”)[0]:
                                            add_key(user,file)
                            else:
                                    pass
            for host_name in ret.keys():
                           print host_name,”:”
                           h = ret[host_name].split(“,”)
                           for host in h:
                                    print ”  “,host
最终修订版:
#!/usr/bin/env python
from glob import glob
from optparse import OptionParser
parser = OptionParser(usage=”%prog [-f] [-q]”,version=”%prog 1.1″)
parser.add_option(“-f”,”–file”,dest=”filename”,default=”filename”,type=”string”)
(options,args) = parser.parse_args()
if __name__ == “__main__”:
            ret = {}
            a = 0
            def add_key(username,hostname):
                    global ret
                    if ret.has_key(username):
                            ret[username] += “,” + hostname
                    else:
                            ret[username] = hostname
            for f in glob(options.filename + “/*”):
                    for line in open(f).readlines():
                            if not line or len(line.split()) < 3:
                                    continue
                            elif line.split()[0] == ‘ssh-rsa’ or line.split()[0] == ‘ssh-dss’:
                                    user = line.split()[-1]
                                    file = f.split(“/”)[-1]
                                    if user.split(“@”)[0] != file.split(“@”)[0]:
                                            add_key(user,file)
                            else:
                                    pass
            for host_name in ret.keys():
                           print host_name,”:”
                           h = ret[host_name].split(“,”)<br

未经允许不得转载:演道网 » Python:一个用来批量处理文件违规内容并记录的脚本

赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册