'Python: communication between an app and terminal which is asking for Admins password
I’m trying to accomplish the following but no luck, any suggestions? ⇒ My app will run a command that requires admin’s password, if you run the command in Terminal, the command will stop asking for the password, however, if you run it within Python, it will just skip that and will end with error (as Admin's password weren't entered. I have used elevate module to launch the app as root, however, the command im using doesn't allow to run as a root :
Do not run this script with root privileges. Do not use 'sudo'
Any suggestions how to communicate between cli and python when cli is waiting for the admins password instead of just skipping it? Thank you all
My code:
Import os
os.system('killall Box')
os.system('/Library/Application\ Support/Box/uninstall_box_drive')
Result:
/usr/local/bin/python3.10 /Users/user/PycharmProjects/pythonProjectBoxUnsaved/venv/test.py
No matching processes belonging to you were found
Unload failed: 5: Input/output error
Try running `launchctl bootout` as root for richer errors.
0
/usr/bin/fileproviderctl
File provider com.box.desktop.boxfileprovider/Box not found. Available providers:
- iCloud Drive (hidden)
com.apple.CloudDocs.MobileDocumentsFileProvider
~/L{5}y/M{14}s
fileproviderctl: can't find domain for com.box.desktop.boxfileprovider/Box: (null)
No matching processes belonging to you were found
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
* * * * * *
Process finished with exit code 0
Error when trying to run the code
Error when using 'elevate' module
I have also tried pexpect, will not work, code below:
import os
import pexpect
os.system('killall Box')
child = pexpect.spawn('/Library/Application\ Support/Box/uninstall_box_drive')
print(child.after)
child.expect_exact('Password:')
print(child.after)
child.sendline('my_password')
print(child.after)
result below:
None
b'Password:'
b'Password:'
Solution 1:[1]
Solved it.
Had to include Bash path in the spawn arguments:
cmd = "/Library/Application\ Support/Box/uninstall_box_drive"
child = pexpect.spawn("/bin/bash", ["-c", cmd])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ramiro Quay |