匹配文件中的任何行

时间:2015-05-07 20:43:49

标签: php regex

我不确定如何读取文件并拉出符合PHP条件的3个IP。我尝试使用爆炸,但这会爆炸所有的空间....我可以得到的任何一种帮助非常感谢:

[May  7 16:30:08] NOTICE[5311] chan_sip.c: Registration from '"507" <sip:507@145.155.214.146:5060>' failed for '195.154.53.90:5068' - Wrong password
[May  7 16:30:48] NOTICE[5311] chan_sip.c: Registration from '"557" <sip:557@145.155.214.146:5060>' failed for '195.154.53.90:5087' - Wrong password
[May  7 16:31:26] NOTICE[5311] chan_sip.c: Registration from '"107" <sip:107@145.155.214.146:5060>' failed for '195.154.53.90:5107' - Wrong password
[May  7 16:31:43] NOTICE[5311] chan_sip.c: Registration from '"907" <sip:907@145.155.214.146:5060>' failed for '195.154.53.90:5084' - Wrong password
[May  7 16:31:58] NOTICE[5311] chan_sip.c: Registration from '"407" <sip:407@145.155.214.146:5060>' failed for '195.154.53.90:5101' - Wrong password
[May  7 16:32:17] NOTICE[5311] chan_sip.c: Registration from '"207" <sip:207@145.155.214.146:5060>' failed for '195.154.53.90:5083' - Wrong password
[May  7 16:32:31] NOTICE[5311] chan_sip.c: Registration from '"7" <sip:7@145.155.214.146:5060>' failed for '195.154.53.90:5113' - Wrong password
[May  7 16:32:57] NOTICE[5311] chan_sip.c: Registration from '"307" <sip:307@145.155.214.146:5060>' failed for '195.154.53.90:5117' - Wrong password

我尝试使用:

readfile(/var/log/asterisk/messages)

我只是想让$ip附加195.154.53.90

$ip=$search[]

2 个答案:

答案 0 :(得分:3)

我可能会后悔这一点,但在这里:

$ip = '195.154.53.90';
$result = preg_grep(preg_quote("/$ip/"), file('/var/log/asterisk/messages'));

print_r($result);

根据评论,找到找到195.154.53.90:5068的位置的任何IP(用单引号括起来):

preg_match_all("/'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d+'/",
               file_get_contents('/var/log/asterisk/messages'),
               $ip);

print_r($ip[1]);

答案 1 :(得分:2)

以下代码将提取$(function () { $('#container').highcharts({ "chart": { "type": "column", "borderWidth": 0, "backgroundColor": null, "spacing": [10, 10, 10, 10], "height": 100 }, "legend": { "enabled": false }, "credits": { "enabled": false }, "title": { "text": "" }, "xAxis": { "crosshair": true, "labels": { "enabled": true, "style": { "font": "Arial", "fontSize": "10px", "fontWeight": "normal", "zIndex": -100 }, "useHTML": true }, "tickWidth": 0, "tickLength": 0, "type": "category" }, "yAxis": { "min": 81, "max": 100, "borderWidth": 0, "gridLineWidth": 0, "minorGridLineWidth": 0, "dataLabels": { "enabled": false }, "labels": { "enabled": true, "format": "{value}%" }, "title": { "text": "" }, "plotBands": [{ "color": "#f1c40f", "from": 94.7, "to": 95.3, "label": null }, { "color": "#2ecc71", "from": 99.5, "to": 100.5, "label": null }] }, "plotOptions": { "column": { "pointPadding": 0.2, "borderWidth": 0 } }, "tooltip": { "enabled": true, "shared": true, "useHTML": true }, "series": [{ "data": [{ "name": "M", "y": 103, "color": "#2ecc71" }, { "name": "A", "y": 99, "color": "#f1c40f" }, { "name": "M", "y": 94, "color": "#e74c3c" }, { "name": "J", "y": 101, "color": "#2ecc71" }, { "name": "J", "y": 104, "color": "#2ecc71" }, { "name": "A", "y": 100, "color": "#2ecc71" }, { "name": "S", "y": 104, "color": "#2ecc71" }, { "name": "O", "y": 94, "color": "#e74c3c" }, { "name": "N", "y": 104, "color": "#2ecc71" }, { "name": "D", "y": 91, "color": "#e74c3c" }, { "name": "J", "y": 86, "color": "#e74c3c" }, { "name": "F", "y": 96, "color": "#f1c40f" }, { "name": "M", "y": 97, "color": "#f1c40f" }, { "name": "A", "y": 89, "color": "#e74c3c" }, { "name": "M", "y": 102, "color": "#2ecc71" }] }] }); // the button action $('#button').click(function () { var chart = $('#container').highcharts(), selectedPoints = chart.getSelectedPoints(); if (chart.lbl) { chart.lbl.destroy(); } chart.lbl = chart.renderer.label('You selected ' + selectedPoints.length + ' points', 100, 60) .attr({ padding: 10, r: 5, fill: Highcharts.getOptions().colors[1], zIndex: 5 }) .css({ color: 'white' }) .add(); }); }); 文本后面的IP:

failed for

上面的代码所做的是它首先将您的日志文件拆分为单独的行,将这些行映射到正则表达式匹配的结果,然后过滤掉不包含IP的日志行。

如果您需要以下$lines = explode("\n", file_get_contents("/var/log/asterisk/messages")); $ips = array_filter(array_map(function($item) { $matches = []; if(preg_match($item, "/'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d+'/")) { return $matches[1]; } return false; }, $lines), function($item) { return $item !== false; }); 的IP,则需要将正则表达式更改为以下内容:<sip: