[Tutor] How to use subprocess module to get current logged in user?

Mats Wichmann mats at wichmann.us
Fri Aug 25 21:39:17 EDT 2017


import getpass 
getpass.getuser ()

depending on how fussy you want to be...

On August 25, 2017 6:13:12 PM MDT, boB Stepp <robertvstepp at gmail.com> wrote:
>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
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.


More information about the Tutor mailing list