Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.getlogin does not work #1

Closed
Asher256 opened this issue Sep 13, 2016 · 3 comments
Closed

os.getlogin does not work #1

Asher256 opened this issue Sep 13, 2016 · 3 comments

Comments

@Asher256
Copy link

Asher256 commented Sep 13, 2016

apssh does not work because of an exception raised by os.getlogin():

$ apssh
Traceback (most recent call last):
  File "/home/asher256/.local/bin/apssh", line 3, in <module>
    from apssh.apssh import Apssh
  File "/home/asher256/.local/lib/python3.5/site-packages/apssh/apssh.py", line 13, in <module>
    from .config import default_time_name, default_timeout, default_username, default_private_key, default_remote_workdir
  File "/home/asher256/.local/lib/python3.5/site-packages/apssh/config.py", line 5, in <module>
    default_username =              os.getlogin()
FileNotFoundError: [Errno 2] No such file or directory

According to http://stackoverflow.com/questions/4399617/python-os-getlogin-problem :

From the os.getlogin() docs: "Returns the user logged in to the controlling terminal of the process." Your script does not have a controlling terminal when run from cron. The docs go on to suggest: "For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the currently effective user id."

I suggest you to replace os.getlogin with:

import pwd
import os

getlogin = lambda: pwd.getpwuid(os.getuid())[0]
default_username = getlogin()

I my case, it solved the issue.

@parmentelat
Copy link
Owner

sounds good to me, will do that when I have a chance

olehermanse added a commit to olehermanse/core that referenced this issue Aug 10, 2020
Fixes error on my Ubuntu / WSL setup:

```
>>> os.getlogin()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory
>>> getpass.getuser()
  'olehermanse'
```

See:
parmentelat/apssh#1

Changelog: None
Ticket: None
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
@andriemc
Copy link

andriemc commented Jun 4, 2022

AttributeError: 'module' object has no attribute 'getlogin'

@parmentelat
Copy link
Owner

Hi
can you please elaborate a little on this issue ?

  • what version of apssh are you using ?
  • on what operating system is that ?
  • what is exactly the command line sentence that triggered that message ?

I could find only one instance of a call to that method, and it's protected in a try/except so this case should be taken care of


the only place where we need to call this is in order to get a decent default for the username to use when logging into a remote box

os.getlogin() has its caveats - as pointed out by @Asher256 - and using getpass.getuser(), as the Python doc now suggests, is probably the way to go

anybody up for a PR ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants