2013-06-11 68 views
-4

提取IP和用戶ID我有一個字符串如何從未成形串

Jun 11 02:47:04 webwork-tlv tcp: 2013-06-11 02:47:04 - ive - [84.11.11.11] hacker(Secure ID)[Manage System] - Host Checker policy 'Machine center' passed on host 84.11.11.11 for user 'hacker' 

一些字符串看起來像

Jun 11 00:13:26 webwork-tlv tcp: 2013-06-11 00:13:25 - ive - [10.11.12.19] hacker(Secure ID)[Manage System] - Sensor tlv-entid-001 - timestamp=[Tue Jun 11 02:23:42 2013 ] severity=[4] policyStr=[IDP 20110132] category=[attack] protocol=[tcp] attackStr=[HTTP:XSS:HTML-SCRIPT-IN-URL-VA] rulebaseStr=[IDS] rulebaseType=[Main Rule Base] srcAddr=[10.11.12.19] srcPort=[3333] dstAddr=[66.11.12.13] dstPort=[80] action=[drop] policyVersion=[41] ruleNumber=[3] 

我想在開始的時候,提取日期,[]但是如果之間的IP它是內部IP(以10或192開頭),無需提取和ID黑客(SecureID)

因此結果應該是ip:84.11.11.11,id:黑客

預先感謝您

+0

是否所有的預期字符串以這種格式? –

回答

2
>>> regex = re.compile("(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]) ([a-zA-Z0-9]+)") 
>>> r = regex.search(string) 

# List the groups found 
>>> r.groups() 
(u'[84.11.11.11]', u'hacker') 
0

排序的乏味,但:

s = "Jun 11 02:47:04 webwork-tlv tcp: 2013-06-11 02:47:04 - ive - [84.11.11.11] hacker(Secure ID)[Manage System] - Host Checker policy 'Machine center' passed on host 84.11.11.11 for user 'hacker'." 
parts = s.split('[')[1].split(']') 
{'ip': parts[0], 'id': parts[1].split('(Secure ID)')[0]}