I'm writing a python code where you need to input username and password. Using Keyring I'm able to save passwords, but the username is still required every time.
I've thought about storing the username to some file on the computer, but then the file will be accessible for whoever wants to use the program next time (it's on a shared computer). I think storing the username for just one session (similar to how the password is saved for just one session for sudo) would be fine, but I can't see any way to do this. So I'm wondering: is there any way to distinguish different terminal sessions by Python?
Asked
Active
Viewed 61 times
-1

Masayuki Fujita
- 5
- 3
-
This seems like an [XY Problem](https://en.wikipedia.org/wiki/XY_problem). How does being able to distinguish terminal sessions have anything to do with securing passwords? Apparent duplicate of https://stackoverflow.com/questions/7014953/i-need-to-securely-store-a-username-and-password-in-python-what-are-my-options – tripleee Apr 21 '22 at 10:42
1 Answers
0
Is the "shared computer" is used using ssh or at least different users, then you can just do:
import os
user_name = os.environ['USER'])
To get the username of the session.
If you really want to have a "session" user, meaning that one user can have different usernames for many running sessions... Then I strongly recommend using a terminal multiplier, use tmux (or Screen) and make the username to be the user (os.environ...) plus the session name.
For instance in tmux would be something like:
import subprocess
user_name = os.environ['USER'])
session = subprocess.getoutput("tmux display-message -p '#S'")
user = user_name + session

Ziur Olpa
- 1,839
- 1
- 12
- 27