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

2017-08-26 Thread boB Stepp
Thanks Alan (and Mats)!  It appears there are many ways to skin this cat!

On Sat, Aug 26, 2017 at 2:21 AM, Alan Gauld via Tutor  wrote:
>
>> 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.
>
> In addition to Steve and Mats answers:
>
> import os
> os.getlogin()   #-> 'alan'
> os.getuid() #-> 1001
>
> import pwd
> pwd.getpwuid(os.getuid()) #-> loads of user related stuff...
>
> You can also use the environment variables such as
>
> os.getenv('USER') #-> 'alan'
> os.getenv('HOME') #-> '/home/alan'
>
> the latter being a useful place to look for the
> individual config file... You can use os.environ
> if you prefer a dictionary style to a function.
>
> And of course
>
> os.cwd() #-> '/home/alan'
>
> For the current directory.


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-08-26 Thread Alan Gauld via Tutor

> 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.

In addition to Steve and Mats answers:

import os
os.getlogin()   #-> 'alan'
os.getuid() #-> 1001

import pwd
pwd.getpwuid(os.getuid()) #-> loads of user related stuff...

You can also use the environment variables such as

os.getenv('USER') #-> 'alan'
os.getenv('HOME') #-> '/home/alan'

the latter being a useful place to look for the
individual config file... You can use os.environ
if you prefer a dictionary style to a function.

And of course

os.cwd() #-> '/home/alan'

For the current directory.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-08-25 Thread boB Stepp
On Fri, Aug 25, 2017 at 8:33 PM, Steven D'Aprano  wrote:
> On Fri, Aug 25, 2017 at 07:13:12PM -0500, boB Stepp wrote:
>
>> My objective:  Determine who the currently logged in user is
>
> py> import os
> py> os.getlogin()
> 'steve'

Sweet!  Much better.  I did not scan the os modules closely enough.

>
>> and
>> determine if that user is in my list of users that I have authorized
>> to use my programs.
>
> That's probably best handled at the OS level, by setting your programs
> to be executable only by members of a particular group. In Linux, the
> relevant commands are chmod and chgrp but I don't know what they are in
> Solaris.

If that was all I needed the user name for (Sorry!  I did not mention
*everything*.), then I would probably go this route.  I also will have
certain default configuration information associated with each user.
But it sounds like it is probably better to do what you suggest, and
then do the configuration settings as planned.

>
>> 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"
>
> Check the documentation for who on your system, e.g. man who, but my
> guess is that the -m or "am I" option requires you to be attached to a
> terminal. By launching a subprocess, you're no longer attached to one.

I had checked the man pages for who, but did not appreciate/realize
that my launched subprocess was not attached to the user's terminal.
I probably should have realized this though, in retrospect.

> Under my Linux system, the above gives no output or error.
>
> No, I don't understand it either :-)
>
> But reading man who tells me that the -m option returns the user
> connected to stdin. Sure enough, this works:
>
> py> p = sp.Popen(cmd, stdin=sys.stdin, stdout=sp.PIPE, stderr=sp.PIPE)
> py> out, err = p.communicate()
> py> out
> 'stevepts/52017-08-25 15:52 (:0.0)\n'

Works for me, too.  Thanks, Steve!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-08-25 Thread Mats Wichmann
import getpass 
getpass.getuser ()

depending on how fussy you want to be...

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


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

2017-08-25 Thread Steven D'Aprano
On Fri, Aug 25, 2017 at 07:13:12PM -0500, boB Stepp wrote:

> My objective:  Determine who the currently logged in user is

py> import os
py> os.getlogin()
'steve'


> and
> determine if that user is in my list of users that I have authorized
> to use my programs.

That's probably best handled at the OS level, by setting your programs 
to be executable only by members of a particular group. In Linux, the 
relevant commands are chmod and chgrp but I don't know what they are in 
Solaris.


> 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"

Check the documentation for who on your system, e.g. man who, but my 
guess is that the -m or "am I" option requires you to be attached to a 
terminal. By launching a subprocess, you're no longer attached to one.

Under my Linux system, the above gives no output or error.

No, I don't understand it either :-)

But reading man who tells me that the -m option returns the user 
connected to stdin. Sure enough, this works:

py> p = sp.Popen(cmd, stdin=sys.stdin, stdout=sp.PIPE, stderr=sp.PIPE)
py> out, err = p.communicate()
py> out
'stevepts/52017-08-25 15:52 (:0.0)\n'



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-08-25 Thread boB Stepp
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor