php从文件中找到字符串,以xxx字开头,直到字yyy

时间:2015-03-06 21:56:13

标签: php preg-match-all

它的日志文件包含这样的行

...
Mar  1 03:34:24 domain sshd[19178]: Failed password for root from 222.186.55.230 port 3005 ssh2
...

我想在用户失败的每一行和用户成功的其他数组行中放入数组行以xxx =失败或成功直到yyy = ssh2然后我可以从两个数组获取ip:

preg_match_all("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $securelogfile, $matches);

并在新数组中获取差异ip。我在正则表达式上很可怕我正在尝试的只是浪费我的时间。

1 个答案:

答案 0 :(得分:0)

回答@ThomasKilian的问题。我用的是失败的:

 preg_match_all("/^(\b(Failed)\b\s+)(\w+ +){4}((\d+\.){3}\d+)( +\w+){3}/", $securelogfile, $matches); 

对于失败的日志行和:

 preg_match_all("/^(\b(Success)\b\s+)(\w+ +){4}((\d+\.){3}\d+)( +\w+){3}/", $securelogfile, $matches);

请记住,首先{4}表示在“失败或成功”

之后计算4个字

这给阵列我应该得到干净的IP,然后得到差异。