我如何确定IP地址已登录多少次?
我认为这是
192.168.1.254
192.168.1.254
192.168.1.254
192.168.1.254
192.168.1.254
对此
syslog_2019-05-15.txt看起来像这样
DROP IN = eth0 OUT = MAC = 38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC = 92.53.90.242 DST = 90.149.222.18 LEN = 40 TOS = 0x00 PREC = 0x00 TTL = 243 ID = 37773 PROTO = TCP SPT = 59155 DPT = 1027 SEQ = 1687374236 ACK = 0 WINDOW = 1024 RES = 0x00 SYN URGP = 0
192.168.1.1 May 14 00:01:44 kern警告内核DROP IN = eth0 OUT = MAC = 38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC = 185.216.140.6 DST = 90.149.222.18 LEN = 40 TOS = 0x00 PREC = 0x00 TTL = 248 ID = 54321 PROTO = TCP SPT = 5
代码:
var fs = require('fs');
fs.readFile('C:/Users/sondr/Desktop/koder/Ip_søk_syslog/syslog_2019-05-15.txt', 'utf8', function(err, data) {
if (err) throw err; {
//count
var count = 0;
//ReEX
const reg = /\bSRC=([\.0-9]+)\b/g;
while ((m = reg.exec(data))) {
console.log("SRC= " + m[1])
console.log(++count);
}
// DEBUG:
//console.log(data);
}
});
答案 0 :(得分:2)
遍历IP地址并将该地址另存为对象的键,如果您通过tracker
对象中已经存在的IP进行迭代,则可以获取其值并将其增加1
或如果它不存在,则将其默认设置为1
,因为这是您第一次遇到该IP。
const data = [
'192.168.1.254',
'192.168.1.254',
'192.168.1.254',
'192.168.1.254',
'192.168.1.254',
'10.40.89.79'
]
const tracker = {}
data.forEach(d => {
const count = d in tracker ? ++tracker[d] : 1
tracker[d] = count
})
Object.keys(tracker).forEach(k => console.log(`${k} (${tracker[k]})`))
我发现在线IP正则表达式可以对您的日志运行match
,然后对匹配项运行相同的功能。
const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/g
const str = `DROP IN=eth0 OUT= MAC=38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC=92.53.90.242 DST=90.149.222.18 LEN=40 TOS=0x00 PREC=0x00 TTL=243 ID=37773 PROTO=TCP SPT=59155 DPT=1027 SEQ=1687374236 ACK=0 WINDOW=1024 RES=0x00 SYN URGP=0
192.168.1.1 May 14 00:01:44 kern warning kernel DROP IN=eth0 OUT= MAC=38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC=185.216.140.6 DST=90.149.222.18 LEN=40 TOS=0x00 PREC=0x00 TTL=248 ID=54321 PROTO=TCP SPT=5`
const data = str.match(reg) || []
const tracker = {}
data.forEach(d => {
const count = d in tracker ? ++tracker[d] : 1
tracker[d] = count
})
Object.keys(tracker).forEach(k => console.log(`${k} (${tracker[k]})`))
可能是您可以在网上找到更好的Regex,如果可以,请告诉我! :-) UPDATE 我看到@Kunal的Regex看起来不错。
对此进行进一步说明,并构建一个接受正则表达式和字符串并返回针对正则表达式的字符串的函数。
const GET_OCCURRENCE = (r = /no args/g, str = 'no args') => {
const data = str.match(r) || []
const tracker = {}
let rtnStr = ''
data.forEach(d => {
const count = d in tracker ? ++tracker[d] : 1
tracker[d] = count
})
return [
Object.keys(tracker).reduce((rtn, k) => rtn+=` \n${k} (${tracker[k]})`, ''),
tracker
]
}
const [IPS, IPS_OBJ] = GET_OCCURRENCE(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/g, `DROP IN=eth0 OUT= MAC=38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC=92.53.90.242 DST=90.149.222.18 LEN=40 TOS=0x00 PREC=0x00 TTL=243 ID=37773 PROTO=TCP SPT=59155 DPT=1027 SEQ=1687374236 ACK=0 WINDOW=1024 RES=0x00 SYN URGP=0
192.168.1.1 May 14 00:01:44 kern warning kernel DROP IN=eth0 OUT= MAC=38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC=185.216.140.6 DST=90.149.222.18 LEN=40 TOS=0x00 PREC=0x00 TTL=248 ID=54321 PROTO=TCP SPT=5`)
console.log(IPS, IPS_OBJ)
const [LETTERS, LETTERS_OBJ] = GET_OCCURRENCE(/[azi]/g, 'bhfgdakdfjsihjkzzjkdldfaajjii')
console.log(LETTERS, LETTERS_OBJ)
答案 1 :(得分:0)
如果我理解正确,则希望从字符串中获取所有ip地址,然后将它们堆叠(删除了重复项)。 所以这个正则表达式:
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
可以检查ip地址。现在使用replace方法,我们可以获得所有的正则表达式匹配项。然后,我们可以遍历它们并删除重复项。看这段代码:
var str = something;
var IPs = [];
str.replace(
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi,
function(match) {
IPs[IPs.length] = match;
return match;
}
);
var stackedIPs = {};
for (i = 0; i < IPs.length; i++) {
if (stackedIPs[IPs[i]] == null) {
stackedIPs[IPs[i]] = 1;
} else {
stackedIPs[IPs[i]] = stackedIPs[IPs[i]] + 1;
}
}
StackedIP将为您提供一个如下所示的对象:
{
192.168.1.1: 3,
192.168.1.5: 2,
...
}
分配给每个IP地址的数字是找到它的次数。
答案 2 :(得分:0)
对于简单的IP地址,您可以使用this regex:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
正则表达式的分解:
\d
与数字0-9
匹配,IP八位字节的长度最小为1,最大为3。.
被\
转义。
const pattern = /(\d{1,}\.\d{1,}\.\d{1,}\.\d{1,})/gm;
const str = `192.168.1.1 May 14 00:01:44 kern warning kernel DROP IN=eth0 OUT= MAC=38:2c:4a:cb:e2:40:10:e8:78:aa:89:ba:08:00 SRC=185.216.140.6 DST=90.149.222.18 LEN=40 TOS=0x00 PREC=0x00 TTL=248 ID=54321 PROTO=TCP SPT=5`;
let matchMap = new Map();
let match;
while ((match = pattern.exec(str))) {
// Get the first captured group
let group = match[1];
// If the map doesn't contain the group set its count to 1
if (!matchMap.has(group)) {
matchMap.set(group, 1);
}
// Else increment the count
else {
matchMap.set(group, matchMap.get(group) + 1);
}
}
// Iterate over the match map now
for (const [key, val] of matchMap) {
console.log(key + ' -> ' + val);
}