Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中主动扫描不同的文件日期_Python_While Loop_Scanning - Fatal编程技术网

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

在Python中主动扫描不同的文件日期,python,while-loop,scanning,Python,While Loop,Scanning,我正在尝试制作一个Python脚本,该脚本可以主动扫描日志以查找新凭据,并修复它们,例如RCAP输出等,但我没有得到想要的输出。我是while循环中的一个完全的noob,我甚至不认为这是一条路要走,但以下是我所拥有的: #! /usr/bin/env python #include <stdio.h> import subprocess import os import sys import time while True: username_list = list(subp

我正在尝试制作一个Python脚本,该脚本可以主动扫描日志以查找新凭据,并修复它们,例如RCAP输出等,但我没有得到想要的输出。我是while循环中的一个完全的noob,我甚至不认为这是一条路要走,但以下是我所拥有的:

#! /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

您没有设置任何条件来中断循环,因此它将无限期运行。如果您想将其构造为一个while循环,并且不希望它永远运行,则需要在某处添加一个break语句:

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

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

这里有一个版本的代码,每个循环只读取一次文件,而不是三次:

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)

包括?当你使用're.findall'的时候不是更容易吗?事实上,我在意外事件中截断了这个。最后我休息了一下。不,这不起作用/将起作用,因为在Ettercap用户:,PASS:,等等行之前将有多行。也许你应该发布一个数据示例;我不是一个读心术的人-不,我知道了。我只是调整了我的代码,添加了一点你的。。。一小时又一小时的实验。。。但现在它起作用了