Find Out Who Is Logged in on Linux Using Python

Find out who is logged in on linux using python

The best thing I found online is psutil.
See the psutil documentation

First install psutil :

pip install psutil

After that everything is easy as an example run python console from terminal:

import psutil 

psutil.users()

Output:

[user(name='root', terminal='pts/0', host='your-local-host-from-isp.net',
started=1358152704.0)]

How can i check which user is currently logged in on the computer with python?

Check this site

the answer with your snippet would be:

import getpass
checkuser = getpass.getuser()

print("The user currently logged in is: " + checkuser)

Also if you need the name just because of the home directory path you can use os.environ['HOMEPATH'] (on Windows) to get the path directly.

Is there a portable way to get the current username in Python?

Look at getpass module

import getpass
getpass.getuser()
'kostya'

Availability: Unix, Windows


p.s. Per comment below "this function looks at the values of various environment variables to determine the user name. Therefore, this function should not be relied on for access control purposes (or possibly any other purpose, since it allows any user to impersonate any other)."

authenticate a user from local linux host using python script

You can use pexpect. Unless you are running your script as root you will need to supply a password to sudo (as you will have to for any answer that uses sudo). To make the script more portable also supply a sudo user name but you could hard code this if using root. This code was written for Ubuntu 21.10 and may need the strings updating for other distributions. I think the code is self-explanatory, you spawn a process, interact with it and expect certain responses during exection.

import pexpect

sudo_user = 'whatever your sudo user name is'
sudo_password = "whatever your sudo user password is"
user_name = "whatever local user name is"
password = "whatever local user password is"

child = pexpect.spawn(f'/usr/bin/sudo /usr/bin/login {user_name}', encoding='utf-8')
child.expect_exact(f'[sudo] password for {sudo_user}: ')
child.sendline(sudo_password)
return_code = child.expect(['Sorry, try again', 'Password: '])
if return_code == 0:
print('Can\'t sudo')
print(child.after) # debug
child.kill(0)
else:
child.sendline(password)
return_code = child.expect(['Login incorrect', '[#\\$] '])
if return_code == 0:
print('Can\'t login')
print(child.after) # debug
child.kill(0)
elif return_code == 1:
print('Login OK.')
print('Shell command prompt', child.after)

For more detail see the docs https://pexpect.readthedocs.io/en/stable/overview.html

How can I find out if someone is actively using a Linux computer in Python or Java?

I assume that the "actively used" part is the tricky part.

If it is sufficient to check whether or not another user is logged in, you can use the commands w and who and perhaps last and lastlog. All these commands several parameter which you can lookup in the manuals.

From Java / Python you can execute these commands and parse their output.

On the other hand: The tools w and who use the file utmp to get their information. A quick Google turned up nothing for Java but for Python I've found the library pyutmp which you can use to read the utmp file directly without parsing the command output.

Whether the user logged in and went to lunch (possibly locking the screen) is a completely other story.

Python script to list users and groups

For *nix, you have the pwd and grp modules. You iterate through pwd.getpwall() to get all users. You look up their group names with grp.getgrgid(gid).

import pwd, grp
for p in pwd.getpwall():
print p[0], grp.getgrgid(p[3])[0]


Related Topics



Leave a reply



Submit