Added support for local sessions

Local sessions (not over SSH) are identified by 'localhost'
This commit is contained in:
Florian Rupp 2023-04-18 22:39:21 +02:00
parent 03c25129c9
commit edbc83cb42

View file

@ -38,14 +38,17 @@ def get_utmp_data() -> list[Session]:
Returns a list of User Objects Returns a list of User Objects
The function uses the utmp library. The utmp file contains information about ALL currently logged in users, The function uses the utmp library. The utmp file contains information about ALL currently logged in users,
including local users (not SSH sessions). We filter out the local users by checking if the remote IP address including local users (not SSH sessions). We filter out the local users by checking if the remote IP address
is empty. is empty and set the hostname for the local sessions to "localhost".
""" """
users : list[Session] = [] users : list[Session] = []
with open('/var/run/utmp', 'rb') as fd: with open('/var/run/utmp', 'rb') as fd:
buffer = fd.read() buffer = fd.read()
for record in utmp.read(buffer): for record in utmp.read(buffer):
if record.type == utmp.UTmpRecordType.user_process and record.host != '': if record.type == utmp.UTmpRecordType.user_process:
if record.host != '':
users.append(Session(record.user, record.line, record.host, record.sec)) users.append(Session(record.user, record.line, record.host, record.sec))
else:
users.append(Session(record.user, record.line, 'localhost', record.sec))
return users return users