How to un-elevate permissions in windows with python - python

I'm trying to run commands as a particular user rather than an administrator.... seems strange, but roll with me here.
I am running my python script as an administrator, but would like to perform certain functions of the script as an underprivileged user.
For example, something like this would be what I would want to accomplish
from shutil import copyfile
as_user('testuser', 'P#$$w0rd1', copyfile(src, dst))
Other parts of the script would proceed to run as administrator. I've tried using run-as and psexec without any luck as the just end up popping open a new window entirely :|
Other than this, I have no idea how to accomplish this task on windows, with linux this sort of stuff is easy.

Related

Is there a way to reference an "ipconfig" file in Windows

So for a little context. In linux the "ifconfig" command is actually executing a "ifcfg-eth0" file found "/etc/sysconfig/network-scripts."
In windows, do command line (or powershell) commands correspond to a specific file? If so where? If it exists I having a hard time finding it.
Reason:
I am trying to execute commands from a program I am writing in Python. I know there are other ways to accomplish this ie. "import os, import subproccess." I am trying to brainstorm a simpler way to execute these commands before my program gets to heavy.
Basically I would like to tell python to execute a file ie. "ifcfg-eth0" in linux but in windows. Also, I'm just using "ipconfig" as an example There are a lot of commands I want to add.
In Windows, an easy way to find the path of a program is to use Where.exe.
where.exe ipconfig
Usually cmd commands are located in system32.
There's an executable ipconfig.exe in C:\Windows\system32

Change the Windows folder permissions to Read only from Python

I have a test case to automate, where in I need to change the folder's permission to read only from Python script. I am using Test Complete tool with python scripting for automating the manual tests.
I am not able to accomplish the tasks, I tried Googling but no luck.
you can try this line of code.
import os, stat
os.chmod(ur"file_path_name", stat.S_IREAD)

How do I run external programs in Python without using the entire path?

Now, I'm working on a voice controlling assistant application.
When I say "open Google Chrome", it should open Chrome. I do not have any problems with speech recognition but I do have a problem with starting the program.
For example, the following code:
import os
os.system("chrome.exe")
Works fine for the notepad.exe or cmd.exe programs but when I give external programs like chrome, I need to give the entire path.
However, in C# we can only give the direct name like chrome.exe to run the program.
So, my problem is that, is there any ways to start external programs without giving the entire path like in C#?
Giving path to start the program would be a serious problem. Because when we move the program to another computer, we will face many code errors.
Try os.path.abspath
os.path.abspath("chrome.exe")
returns the following:
C:\Users\Yourname\chrome.exe
The PATH system variable governs this inside Python just as outside. If you want to modify it from Python, it's os.environ.
As an aside, a better solution is probably to use subprocess instead, as suggested in the os.system documentation.
The PATH manipulation will be the same regardless.
import os
import subprocess
os.environ['PATH'] = os.environ['PATH'] + r';c:\google\chrome\'
subprocess.run(['chrome.exe'])
... where obviously you need to add the directory which contains chrome.exe to your PATH.
Probably you will want to make sure the PATH is correct even before running Python in your scenario, though.
The first part of this answer is OS specific and doesn't solve it in code or python
Add the path where Chrome exists to your PATH variable (you'll likely need a restart), and then when you execute a command such as chrome.exe it will try to run it from that path location
UPDATE:
If you want to implement a search for the absolute path here are some good resources on path listing How do I list all files of a directory?
But again you'll likely need some educated guesses and will need to do some OS specific things to find it.
The Chrome binary is often installed in the same set of locations
I solved this problem by using C#. Like I already mentioned, we can directly call programs like "chrome.exe" in C#. I created a console application in C# which open Google Chrome via using bellow code. Then I complied that console application as .exe, after that I link that exe file with my python program.
C#
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "chrome.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
Python
import os
os.system('console_application.exe')

Can I open windows task scheduler with Python?

I've just been experimenting with python so that I can make life a little more continent for myself. I tend to be doing multiple things at a time and python has been extremely helpful with moving files and opening programs. I was just wondering if there is a way it can open the scheduler, and if you can bind it to a key?
With standard os library
import os
os.system('control schedtasks')
Or for more complicated options, with subprocess
import subprocess
p = subprocess.Popen("control schedtasks")
I would try something simple like this to open the scheduler:
import os
os.system("taskschd.msc")
Note that if you don't want the UAC popup to ask you for elevation whenever your script runs, your script itself will need to be run as admin (so that every sub-process that it executes, such as the Task Scheduler, will also subsequently be run as admin).
As far as binding the script to a key...you may honestly have the most luck using something like https://autohotkey.com/ to do so. Alternatively, you could make your script run at login, and then use a package like https://pypi.python.org/pypi/system_hotkey/1.0.3 to implement hotkey watchers into it (note that this is a Python 3 package).

How to turn Python program into executable file

I apologize for such a basic question. I've done some research online and still cannot figure out for the life of me how to turn a python folder into something like an actual app I can open in OS X. I am using Mac OS X, Terminal and Coderunner for my Python project.
Here are a few options:
Platypus is not Python-specific. It lets you wrap a simple GUI around a command line tool.
py2app is Python-specific and a good choice if you have a GUI, or need to run in the background.
PyInstaller is similar to py2app but cross-platform; I've never used it, so I don't know how well it works.
The right choice depends on what your program does; who is the expected audience — do you need to redistribute it, if so how, and so forth. If you want to make the application entirely self-contained — not dependent on anything else beyond the OS — then things get more complicated (though certainly not insoluble; there are several commercial Mac desktop apps written in Python.)
Typically you would make the script executable by putting
#!/usr/bin/env python
as the first line, and then in a terminal window typing
chmod u+x myscript.py
You might also want to put it in a special scripts folder and then add that to your PATH by editing .bash_profile. (I am putting a lot of buzz-words here to help you find the tutorials explaining how these things work.)
You can wrap your script into an Automator object if you want to try running it that way, but in the long run you will be better off getting comfortable working in a terminal window.
It would also help to know what your app does: Process files, generate a GUI, do a calculation, etc...

Categories

Resources