简体   繁体   中英

JavaScript or Python to pull IP address from block data

I am trying to filter through a bunch of jiberish text in a google spreadsheet, and pull just the IP address and store them so I can compare them later. IE user puts in

"Summary: unauthorized ms-rdp traffic
Notes:  SRC_IP: 211.238.202.137   91.212.144.2   92.66.145.194   121.229.128.42   81.162.195.34   81.88.125.86    213.42.28.188   85.21.42.240   94.56.89.117   177.55.40.14   219.69.14.40
SRC_Port:
SRC_Country: US KR IL CN CZ AE RU BR TW
DST_IP: MANY
DST_Port:
DST_Country: US
Campus_Agency:"

the script stores all the scr_ip address's and later if needs be the user can type in an IP address like 211.238.202.137 and it will return a statement that verifies the IP is in or not in, the list. I have tried if statements and had no luck, I have been trying different variations and I think this is just a little over my skill set. Closest I have come was it pulled the IP address but sorted them by value so they didn't match the originals

A quick regular expression that pulls out all ip-address-like text:

import re

ipaddress = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')

addresses = ipaddress.findall(inputtext)
if '211.238.202.137' in addresses:
    print 'We have a match!'

For your example text, the .findall() call returns:

>>> ipaddress.findall(inputtext)
['211.238.202.137', '91.212.144.2', '92.66.145.194', '121.229.128.42', '81.162.195.34', '81.88.125.86', '213.42.28.188', '85.21.42.240', '94.56.89.117', '177.55.40.14', '219.69.14.40']
import re

text = """Summary: unauthorized ms-rdp traffic
Notes:  SRC_IP: 211.238.202.137   91.212.144.2   92.66.145.194   121.229.128.42   81.162.195.34   81.88.125.86    213.42.28.188   85.21.42.240   94.56.89.117   177.55.40.14   219.69.14.40
SRC_Port:
SRC_Country: US KR IL CN CZ AE RU BR TW
DST_IP: MANY
DST_Port:
DST_Country: US
Campus_Agency:"""

"""This will store all the ips in the text variable in a list called ips"""
ips = re.findall('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', text, re.DOTALL)

ipEntered = raw_input('Please enter an IP: ')
if ipEntered in ips:
    print 'The IP you entered is in the list.'
else:
    print 'The IP you entered is not in the list.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM