Im trying to make a script where every time I press x, it prints y.
When I run the code:
import keyboard
if keyboard.is_pressed('x'):
print ("y")
The console outputs:
raise OSError("Error 13 - Must be run as administrator")
OSError: Error 13 - Must be run as administrator
Thanks!
You can't run a script with virtual keyboard inputs like you regular python file in the macOS terminal due to a security feature.
Let's assume your filename is script.py.
If you type
python3 script.py
macOS would view this as a security breach as recording keyboard inputs (like keyboard.is_pressed('x')) is a typical method for recording someone's password as they type it in on a website, application, etc.
To prevent that error, you'd need to run the file as an administrator.
To do so, type:
sudo python3 script.py
It will ask you for your user's password and then proceed to execute the code.
The keyboard module registers global key events (they trigger without application focus) and this requires administrator permissions under MacOS.
Related
I have a terminal command I wish to execute from a python file.
However this command needs says it needs to be run as root (i.e sudo).
I am running this command from a python file by doing:
os.system(sudo pmset schedule wake "08/31/21 00:26:00")
its basically just telling my laptop to wakeup at a specific time.
As you know that this then requires requires a the users password to be manually input to execute the command. Is there a way of removing this from requiring a password? i was trying to enter my password in as an 'os.system' command but it wont take. it seems like only a manual input will work. any ideas?
If using subprocess, you have the option to run a command as shell, with the shell=True argument. However, this requires the python program to already be running with elevated privilages (run python with sudo), otherwise it is not permitted to start a subprocess with an elevated privilage.
You can read more about the docs: https://docs.python.org/3/library/subprocess.html#subprocess.run
I just learned python. I want to make a python program to setup my ubuntu linux distribution.
How to simulate to enter "apt-get install firefox" and an enter key?
When installation is sucessful, "finished!" shows on the terminal. How my python program can know key word "finished" is displayed?
The terminal is a just way for a person to issue commands to the operating system and for the operating system to show results back to the person. With Python and other languages, you can issue commands to the operation system without a terminal and programmatically process responses received back from the OS or the program you had the OS run. For python, use the subprocess module. For example, you might write something like subprocess.POpen(["apt-get", "install", "firefox"], ...). I am on Windows, so cannot test a specific example on Ubuntu. Read the docs and experiment with the examples and options.
I'm trying to decrypt the Windows wireless password stored in the profile xml file using Python. I came across a blog post giving an example of how to do it calling Windows CryptUnprotectData using Python's win32crypt module. My problem is I get the Key not valid for use in specified state error and need to run it using LocalSystem.
You will get that error even if you run cmd.exe as an administrator. Here's where you need to know a bit about Windows that, as a Windows n00b, I didn't know: the LocalSystem account is different from the administrator privilege. In order to run cmd.exe with the LocalSystem account, you need to install a Microsoft package called PsTools. Inside PsTools a program called PsExec, which is a little bit like sudo on Un*x. Just download the zip linked at the bottom of the Microsoft TechNet page above and unzip it somewhere where you can find it.
To use PsExec, open cmd.exe as an administrator (open the start menu in the bottom-left of your screen, type cmd.exe into the search box, and press Ctrl+Shift+Enter to run it as an admin). Hit "continue" on the User Account Control dialog box that opens. In the command shell that opens, navigate to the directory where you unzipped PsTools. Now run "psexec.exe /s /i cmd.exe". After you agree to PsTools's EULA, PsTools should open a new cmd.exe shell window
running as LocalSystem.
Is there a way around this error without using psexec.exe as the blog post states? Perhaps using CryptoPy or PyCrypto?
For reference, the encrypted password I retrieve is the keyMaterial key from the Windows Vista profile xml file.
The code I'm using:
import win32crypt
mykey='01000000D08C9DDF.....' # 308 characters long
binout = []
for i in range(len(mykey)):
if i % 2 == 0:
binout.append(chr(int(mykey[i:i+2],16)))
pwdHash=''.join(binout)
output = win32crypt.CryptUnprotectData(pwdHash,None,None,None,0)
print "hex:", "".join(["%02X" % ord(char) for char in output[1]])
print "ascii:", output[1]
Thanks in advance.
Wht not ask your system administrator to give you LocalSystem privileges, if that is what you need?
BTW, don't bother with the complex conversion from hex to binary. Just do:
In [5]: '01000000D08C9DDF'.decode('hex')
Out[5]: '\x01\x00\x00\x00\xd0\x8c\x9d\xdf'
I'm trying to run a Python script from the command line in Windows 7, but the interpreter seems to ignore all commend line arguments. It does not do so when I run the command as
python C:\PathToScript\script.py arg1
or when I launch the terminal with Administrator permissions. It used to work normally until I had to reinstall Python (2.7.2), so I've probably managed to break some file association for standard users. The solutions in Python Command Line Arguments (Windows) do not work for me, as the registry entry is already set up with %*:
Key Name: HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
Class Name: <NO CLASS>
Last Write Time: 3/04/2012 - 14:19
Value 0
Name: <NO NAME>
Type: REG_SZ
Data: "C:\Python27\python.exe" "%1" %*
It's not a major problem, but I've gotten used to just shift+clicking in a directory and opening a terminal there, whereas I now have to run an elevated cmd prompt, type the Administrator account name and password and navigate to the right folder in order to run the script properly. Does anyone know where I can find this user specific file association or if there's another way to solve this?
I would presume that you are running the script from some kind of the shortcut, as console is not involved.
Can it be that the shortcut did have parameters, while not it does not and you should only resupply those in the Program section of the properties for the shortcut.
I'm trying to make my python script run upon startup but I get the error message windowserror access denied, but I should be able to make programs start upon boot because teamviewer ( a third-party program I downloaded ) runs every time I restart my computer so I know that I should be able to make my program run at startup (I might be doing something different though, so if you could shed some light on what teamviewer is doing differently to get its script to run at startup that would be helpful).
Here is my script
import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run')
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py')
key.Close()
webbrowser.open('www.youtube.com')
Any input is appreciated.
import webbrowser
webbrowser.open('www.youtube.com')
Get rid of all of that _winreg stuff. Instead, you (assuming double-clicking on a py file opens the console) should simply place it in your startup folder (C:\Users\yourusername\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup on Windows 7, and C:\Documents and Settings\yourusername\Start Menu\Programs\Startup in XP). This works because Windows tries to open all files in the startup folder, and if Python opens PYs by default, Windows will open the Python console. Try restarting, that should work.
Baboon:
I am a little late posting, but you seem to have left off the sam at the end of your code here.
When you open a key you need to add the user rights, if you do not _winreg defaults to "READ":
Here is a snippet from the python site
http://docs.python.org/2/library/_winreg.html#access-rights
sam is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ. See Access Rights for other allowed values.
Here is the code corrected:
import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run',_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py')
key.Close()
webbrowser.open('www.youtube.com')