Jan-29-2019, 02:50 PM
(This post was last modified: Jan-29-2019, 03:29 PM by gt76_noobster.)
Hello all,
I have a problem where I have two very long lists and I need to use variables (ip addresses) to match lines in another.
fileseen is the list of all IP addresses observed at a certain location, filemaybe is a list of hosts in subnets available in various locations.
I am hoping to use IPs in fileseen to get the SUBLOC location code in file two when a match between an actual and potential occured.
My code so far looks like this, it does the regex's fine, but it does not loop back the next line of the
first and ip match to extract the next set of variables:
cat file1:
(unique $1, not unique $2,$3)
10.0.1.x LO1 192.168.1.11
10.0.2.x LO2 192.168.1.11
cat file2:
(all potential ip's for a given LOC site, $1 unique)
10.0.1.x SUBLOC
Greetings - G
sorry I cleaned this up a bit.
Works for the first file. Now how can I used this to get the stuff I want from file two?
I had some help some years ago for Perl via Perlmonks, they used hash indexes, but I am obviously not a coder.
I have a problem where I have two very long lists and I need to use variables (ip addresses) to match lines in another.
fileseen is the list of all IP addresses observed at a certain location, filemaybe is a list of hosts in subnets available in various locations.
I am hoping to use IPs in fileseen to get the SUBLOC location code in file two when a match between an actual and potential occured.
My code so far looks like this, it does the regex's fine, but it does not loop back the next line of the
first and ip match to extract the next set of variables:
cat file1:
(unique $1, not unique $2,$3)
10.0.1.x LO1 192.168.1.11
10.0.2.x LO2 192.168.1.11
cat file2:
(all potential ip's for a given LOC site, $1 unique)
10.0.1.x SUBLOC
#!/usr/bin/python3 import sys import re def extract_ips(data): regex=re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") return (regex.findall(data)) def extract_proxy(data): regex=re.compile(r"\ [A-Z0-9]{1,5}\ ") return (regex.findall(data)) def extract_proxyip(data): regex=re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") return (regex.findall(data)) fileseen = open ('file1' ,'r', encoding='utf-8') filemaybe = open ('file2','r', encoding='utf-8') seen_hosts=[] for line in fileseen: for ip in extract_ips(line): if ip not in seen_hosts: seen_hosts.append(ip) for proxy in extract_proxy(line): for proxyip in extract_proxyip(line): # print ('Seen:', seenip, ' BEHIND Proxy:',boproxy, ' BO Proxy IP:',proxyip) for line in filemaybe: if re.search(ip, line): # print ('Matched actual:', ip, 'Sublocation: '). # help is needed here (Want SUBLOC, I only get 1 match #print ('Seen:', seenip, ' BEHIND Proxy:',proxy, ' BO Proxy IP:',proxyip)Thank you very much.
Greetings - G
sorry I cleaned this up a bit.
Works for the first file. Now how can I used this to get the stuff I want from file two?
I had some help some years ago for Perl via Perlmonks, they used hash indexes, but I am obviously not a coder.
#!/usr/bin/python3 import sys import re def extract_ips(data): regex=re.compile(r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") return (regex.findall(data)) def extract_bo(data): regex=re.compile(r"\ [A-Z0-9]{1,5}\ ") return (regex.findall(data)) def extract_proxyip(data): regex=re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$") return (regex.findall(data)) filemaybe = open ('all_pot_hosts.txt','r', encoding='utf-8') fileseen = open ('seen_hosts_by_proxy.txt' ,'r', encoding='utf-8') seen_hosts=[] for line in fileseen: for ip in extract_ips(line): if ip not in seen_hosts: for boproxy in extract_bo(line): for proxyip in extract_proxyip(line): print ('Seen:', ip, ' BEHIND Proxy:',boproxy, ' BO Proxy IP:',proxyip)