I'm looking to get JUST the username of the owner of a process, not in a list, array or table. I need it completely by itself so when I store the output in a variable, it is ONLY the username. (An SID instead of a username will also suffice) This has to work in Powershell 2.0 or Python 2.*. I'm still fairly new to both powershell and python so working examples are greatly appreciated, and if it helps, explorer.exe is the process I want to find the username of the owner for. Also, I need to run the command/script provided as an answer under the SYSTEM context.(You can provide your answer either completely in python or completely in powershell, it does not matter for what I am doing)
P.S. Windows Only :D
Python's psutil seems like a good choice for this:
import psutil
pid = 1
username = psutil.Process(pid).username
print "Process {} is owned by {}".format(pid, username)
On Linux the result is:
Process 1 is owned by root
Answer accepted above, I know, but for future reference:
Get-WmiObject -Class Win32_Process -Filter 'Name = "explorer.exe"' | ForEach-Object { $_.GetOwner() }
Related
I have made a user creation script in powershell, and I am almost done writing the website automation part of it in python with selenium. My problem lies in the joining of the 2. I would like my Python script to use the new user creds I entered in powershell.
So hopefully the PS script would run fully, but before exiting it starts my python script and uses the creds to build him website profiles as well. I have done quite a bit of research the last couple days and cannot figure this out.
Thank you!
You can solve it with passing them only one time rather than saving passwords clear text. Although, if you enable Powershell logging, check what will end up in those logs.
$user1 = "test1"
$cred1 = "testpass1"
# you can also concatenate if necessary, adding all users/pws with some separators
$user2 = "test2"
$cred2 = "testpass2"
$users=$user1+","+$user2
$creds=$cred1+","+$cred2
PS > py .\path_to\create_web_profiles.py $users $creds # make sure you use _py_ and not python / python3.
create_web_profiles.py:
import sys
users = sys.argv[1]
passwords = sys.argv[2]
def getusers(users, passwords):
users=users.split(",")
passwords=passwords.split(",")
print('Usernames: ',users,'Passwords: ',passwords)
for user,passw in zip(users,passwords):
create_web_user(user,passw)
def create_web_user(user, passw):
# your web functions come here
print(user,passw)
pass
getusers(users, passwords)
Output:
PS > py .\path_to\create_web_profiles.py $users $creds
Usernames: ['test', 'test2'] Passwords: ['tpass', 'tpass2']
test tpass
test2 tpass2
I created a service based on this code Is it possible to run a Python script as a service in Windows? If possible, how?
But when i run the code I can only run it as a service and not a normal process.
Is it possible to create a script that when it is run by an admin it runs as a service, but when its activated without admin privileges it runs as a normal process?
With win32service (some docs here) you can also get the user info by calling the method GetUserObjectInformation, with differente type parameters to get one of UOI_FLAGS,UOI_NAME, UOI_TYPE, or UOI_USER_SID. The complete list of user info types can be found here.
To have a look just print the info somewhere on your program, like the snippet below. If the user name is all that you need win32api will get you there easier.
print win32api.GetUserName()
print win32service.GetUserObjectInformation( win32service.OpenInputDesktop(0,0,1), 4)
# or simply:
print win32service.GetUserObjectInformation( win32service.GetProcessWindowStation(), 4)
Finally, in order to get the privileges directly you can use win32net like so:
win32net.NetUserGetInfo(None, win32api.GetUserName() , 11 )['priv']
This will return an int which maps to:
win32netcon.USER_PRIV_GUEST (0)
win32netcon.USER_PRIV_USER (1)
win32netcon.USER_PRIV_ADMIN (2)
In your program you can check if win32net.NetUserGetInfo(None, win32api.GetUserName() , 11 )['priv'] == win32netcon.USER_PRIV_ADMIN to launch it as a service or not.
For more information on the extensions you can find a brief reference here.
I'm wondering how to completely automate a checkout. I've tried
os.system('cleartool co ' + pathname)
but that still prompts me to enter a comment about the checkout. Adding more os.system() commands right after doesn't quite work -- they only execute after I've entered the comment.
I'm looking at using subprocess and maybe Popen, but I don't quite understand how they work from the documentation I can find online.
Any help would be much appreciated, thanks!
If you don't need to enter a comment, a simple -nc would be enough:
os.system('cleartool co -nc ' + pathname)
See cleartool checkout man page.
If the comment is known, you can add it directly (-c xxx)
In both cases, the checkout becomes non-interactive, more suite to batch process.
You can use Popen and communicate to enter the comment after calling cleartool:
from subprocess import Popen
p = Popen(['cleartool','co',pathname])
p.communicate("comment\n")
This is a reposted question from raspberrypi.stackexchange.com. While I am trying to get something to work on python on the raspberry pi, since it doesn't involve any pi-specific things, it was suggested by someone I post here instead. Original post is here.
I am trying to make a web ui to change the date in the rapsberry pi, but I keep getting a return code of 256.
Currently what I have goes like this:
web page -> submits an ajax request to a python script
python checks what type of command (a time/date command in this case) and pieces together a string looking like:
sudo date --set="20130901 20:10"
and stores it in a variable commandString. Then python goes:
os.system(commandString)
and the return value is passed all the way up to the web ui where it is printed out.
I also currently return the commandString value to the web ui too to verify it and it looks okay.
The problem is that every time I test, I keep getting back 256 as the error return code. The date on the raspberry pi of course doesn't change as I manually check it before and after.
However, if I manually go in to python on the raspberry pi and try:
commandString = 'sudo date --set="20130901 20:10"'
os.system(commandString)
It works with no issues. If I try it without sudo then I get a return value of 256 as well, so I thought maybe it was a permissions issue with my original script. I tried this link to check my script's permissions and it seems to be okay? (os.geteuid() is 0)
If it matters, I am using lighttpd and fastcgi to run python from a web ui. My lighttpd config is currently:
fastcgi.server = (
".py" => (
"python-fcgi" => (
"socket" => "/tmp/fastcgi.python.socket",
"bin-path" => "/var/www/command.py",
"check-local" => "disable",
"max-procs" => 1)
)
)
Any ideas on what I'm missing?
On the original post, it was also suggested I try something like:
echo <password> | sudo -S date --set="20130829 02:02
While it's probably not a good idea to put in my root password like that, I tried it and got the same result: it works when doing in the terminal/shell and within the python interpreter, but not via the web ui to python.
UPDATE: or maybe just use /usr/bin/sudo /bin/date ... to make sure the commands are found.
Try this:
import subprocess
p = subprocess.Popen('sudo -S date --set ...', shell=True, stdin=subprocess.PIPE)
p.communicate(input='<your password>')
This is a more proper way of launching a subprocess (via the shell), and sending it some input.
If you also need to read the output of the process, then for example:
p = subprocess.Popen('sudo -S date --set ...', shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(input='<your password>')
print "\n".join("out: " + x for x in out.split('\n'))
print "\n".join("err: " + x for x in err.split('\n'))
...or just take the contents of out and/or err and parse them.
Check the environment whether sudo and date(or any other command you want to execute) can actually be found on the search path.
You can also use absolute paths to sudo and other commands, e.g. /usr/sbin/sudo
Wondering if you know if there is a slick way to get full username from shell?
example: if my unix username is froyo then I want to get my full name
as registered in the system in this case [froyo === Abhishek Pratap]
finger command does it but for all the logged in users at the same time ..so that needs some parsing to get the right value. Anything slicker ?
Anything from inside python would be great too.
Thanks!
-Abhi
One way to achieve this is using pwd.getpwuid and os.getuid:
>>> import os
>>> import pwd
>>> pwd.getpwuid(os.getuid())[4]
This is the traditional Unix way of doing it (based on standard C library calls).