I am trying to use Python Regex to get the IP address from the last line of a log.
I can get an IP if I search through the entire log. For example:
with open("read.log", "r+") as log:
for line in log:
address = "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$"
match = re.match(address, line)
But when I try to just read the last line and get an IP address, I do not get any results. How do I fix this?
import re
def run():
try:
logfile = open('read.log', 'r')
# print ('First line in log: ',logfile.readline())
for line in logfile:
x = line
for ip in x:
address = "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$"
match = re.match(address, ip)
logfile.close
print ('Last Line: ', match)
except OSError as e:
print (e)
run()
My read.log looks like this...
10.1.177.198 Tue Jun 19 09:25:16 CDT 2018
10.1.160.198 Tue Jun 19 09:25:38 CDT 2018
10.1.177.198 Tue Jun 19 09:25:36 CDT 2018
10.1.177.198 Tue Jun 19 09:26:38 CDT 2018
10.1.177.198 Tue Jun 19 09:27:16 CDT 2018
10.1.177.198 Tue Jun 19 09:28:38 CDT 2018