在Python中主动扫描不同的文件日期

时间:2015-05-28 23:44:30

标签: python while-loop scanning

我正在尝试创建一个Python脚本,主动扫描日志以获取新凭据并修复它们(Ettercap输出等),但我没有得到我想要的输出。我是while循环中的一个完整的菜鸟,我甚至不认为它是要走的路,但这就是我所拥有的:

#! /usr/bin/env python
#include <stdio.h>
import subprocess
import os
import sys
import time
while True:
    username_list = list(subprocess.check_output("cat pooky.log | grep 'USER: ' | awk '{print $6}'", shell=True))
    password_list = list(subprocess.check_output("cat pooky.log | grep 'PASS: ' | awk '{print $8}'", shell=True))
    url = subprocess.check_output("cat pooky.log | grep 'INFO: ' | awk '{print $10}'", shell=True)
    if "+" in username_list:
        fix1 = username_list.index('+')
        username_list[fix1] = " "
    username = "".join(username_list)
    if "+" in password_list:
        fix2 = password_list.index('+')
        password_list[fix2] = " "
    old = os.stat("pooky.log").st_mtime
    if os.stat("pooky.log").st_mtime != old:
        username += username
        password += password
        url += url
        print "New credentials found."
    time.sleep(1)
    break

2 个答案:

答案 0 :(得分:0)

您没有设置任何条件来破坏循环,因此它将无限期地运行。如果你想把它构造成一个while循环而你不希望它永远运行,你需要在某处添加一个break语句:

while True:
    # do stuff
    if something == somethingElse:
        break

至于你的其余代码,如果没有一些示例输入和预期输出,很难说它有什么问题。

答案 1 :(得分:0)

这是你的代码版本,每个循环只读取一次文件(不是三次):

import os
import time

log_file = "pooky.log"
old_time = None
while True:
    username_list = []
    password_list = []
    url_list = []
    log_fh = open(log_file)
    for line in log_fh:
        words = line.split()
        if words[5] == 'USER:':
            username_list.append(words[6].replace('+', ' '))
        if words[7] == 'PASS:':
            password_list.append(words[8].replace('+', ' '))
        if words[9] == 'INFO:':
            url_list.append(words[10])
    close(log_fh)
    new_time = os.stat(log_file).st_mtime
    if old_time is not None and new_time != old_time:
        print "New credentials found."
        old_time = new_time
        break
    time.sleep(1)
相关问题