[Tutor] How to use subprocess module to get current logged in user?
boB Stepp
robertvstepp at gmail.com
Fri Aug 25 20:13:12 EDT 2017
SunOS 5.10, Python 2.4/2.6
I ask forgiveness in advance as I cannot copy and paste into an email
what I am doing on this system. So I will have to manually type
things, introducing the usual possibility of typos.
My objective: Determine who the currently logged in user is and
determine if that user is in my list of users that I have authorized
to use my programs.
I tried in the interpreter:
=================================================================================================
py2> cmd = ['who', '-m']
py2> import subprocess as sp
py2> p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
py2> out, err = p.communicate()
py2> out
''
py2> err
"Must be attached to terminal for 'am I' option\n"
py2> p = sp.Popen(cmd, shell=True, stdin=sp.PIPE, stdout=sp.PIPE,
stderr=sp.PIPE)
py2> out, err = p.communicate()
py2> out
'rstepp ...' followed by all the other currently logged in users as if
I had just typed 'who' in the terminal.
=================================================================================================
What am I doing wrong? Why cannot I get the '-m' to be used? And is
there an easier way to accomplish my objective?
If I cannot work this out, then I will be forced to do:
py2> import os
py2> current_usr = os.popen('who -m')
This should not be a security issue for me as no user input is
involved. But I would rather use the subprocess module.
TIA!
--
boB
More information about the Tutor
mailing list