Jan-06-2024, 11:18 AM
Hello,
The following code checks both the username and the MAC address:
Thank you.
The following code checks both the username and the MAC address:
#!/usr/bin/python3
import re
import sys
db_file = '/etc/openvpn/db.txt'
log_file = '/var/log/openvpn/openvpn.log'
regex_mac = 'IV_HWADDR=(.*)'
regex_username = 'depth=0, CN=(.*)'
login = False
logs_username_mac_list = []
with open(log_file, 'r') as log:
lines = log.readlines()
# read only latest 100 lines
last_50_lines = lines[-50:]
# iterate latest 50 lines
for line in last_50_lines:
match_mac = re.search(regex_mac, line)
if match_mac:
log_mac = match_mac.group(1)
print(log_mac)
# on match, add it to list
logs_username_mac_list.append(log_mac)
match_username = re.search(regex_username, line)
if match_username:
log_username = match_username.group(1)
print(log_username)
logs_username_mac_list.append(log_username)
# fetch username and mac address from database
with open(db_file, 'r') as db:
for line in db.readlines():
splitter = line.split('-', 1)
# 0 index is username, removing newline
db_username = (splitter[0]).rstrip("\n")
print(db_username)
# 1 index is mac, removing newline
db_mac = (splitter[1]).rstrip("\n")
print(db_mac)
if db_username in logs_username_mac_list:
user_index = logs_username_mac_list.index(db_username) - 1
print(user_index)
mac_index = logs_username_mac_list[user_index]
print(user_index)
# if log mac matches db_mac
if mac_index == db_mac:
print("true")
login = True
print(login)
if login:
sys.exit(0)
else:
sys.exit(1)I just want the MAC address to be checked. I changed the code as follows:#!/usr/bin/python3
import re
import sys
db_file = '/etc/openvpn/db.txt'
log_file = '/var/log/openvpn/openvpn.log'
regex_mac = 'IV_HWADDR=(.*)'
login = False
logs_mac_list = []
with open(log_file, 'r') as log:
lines = log.readlines()
# read only latest 100 lines
last_50_lines = lines[-50:]
# iterate latest 50 lines
for line in last_50_lines:
match_mac = re.search(regex_mac, line)
if match_mac:
log_mac = match_mac.group(1)
print(log_mac)
# on match, add it to list
logs_mac_list.append(log_mac)
# fetch username and mac address from database
with open(db_file, 'r') as db:
for line in db.readlines():
splitter = line.split('-', 1)
# 1 index is mac, removing newline
db_mac = (splitter[1]).rstrip("\n")
print(db_mac)
mac_index = logs_mac_list[user_index]
print(user_index)
# if log mac matches db_mac
if mac_index == db_mac:
print("true")
login = True
print(login)
if login:
sys.exit(0)
else:
sys.exit(1)Is it OK?Thank you.
