How can I run a sudo terminal command from applescript? - terminal

How can I run code "sudo ettercap -C" with sudo permissions in applescript?
display dialog "Start ettercap?" buttons {"Boot", "Cancel"}
do shell script "sudo ettercap -C"
This is for a project i'm doing. Dont ask :D

Instead of sudo use with administrator privileges
display dialog "Start ettercap?" buttons {"Boot", "Cancel"}
do shell script "ettercap -C" with administrator privileges
See Technical Note TN2065: do shell script in AppleScript

Related

When I start my terminal I get "login: /usr/local/bin/bash: No such file or directory [Process completed]" and i cant type anything in my terminal

These are the series of commands I entered that cause the problem.
brew install bash
echo '/usr/local/bin/bash' | sudo tee -a /etc/shells
chsh -s /usr/local/bin/bash
Now when I start my terminal, I get this.
Last login: Sun Apr 7 14:40:48 on ttys008
login: /usr/local/bin/bash: No such file or directory
[Process completed]
I can't write to the terminal unless I got to Shell->New Command and then type in "\bin\bash"
Anyone know a solution to this?
Go to "System Preferences" > "Users & Groups"
Click the "Lock" icon and authenticate
Right-click the your user icon and select "Advanced Options"
Change the value for "Login shell" to /bin/bash
The "System Preferences" option did not work for me cause there wasn't any "Advanced Options" within "Users & Groups" (For reference: I am running Monterey 12.2). The following worked for me:
Open a new terminal. For now it will show /usr/local/bin/bash: No such file or directory.
Press Command + , which should open the terminal preferences.
Select the Command (complete path) option for the Shells open with: choice.
Enter the value as /bin/bash or /bin/zsh based on your preference.
Close and reopen the terminal. You should now have a working terminal.

Make osascript execute in parent bash

I want to extend the sudo timeout using GUI on my Mac for a script which contains sudo commands but is not allowed to run with root in total.
so I want to do something like:
osascript -e "do shell script 'sudo -v'" with administrator privileges"
do_some_thing_without_root
sudo do_some_thing_with_root_but_without_asking_the_password

Ruby use Sudo GUI instead of command line

I was wondering if it is possible to invoke a sudo GUI prompt instead of prompting through the terminal such as
osascript -e 'do shell script \" command here \" with administrator privileges'
and how I would go about it in ruby.

How do I run a shell script with administrator privileges through AppleScript without prompting for a password?

I want to have my AppleScript application run a Python script with sudo, but I don't want the application to prompt the user for a password (our users do not have sudo privileges).
The Python script has been added to the /etc/sudoers file appropriately (ALL ALL=NOPASSWD: /path/to/script.py). In the terminal, I can do (as a regular, non-privileged user):
$ sudo ./script.py
and it runs perfectly well. But in AppleScript when you try to do:
do shell script "sudo ./script.py"
You of course get the "sudo: no tty present and no askpass program specified" error. But if you change it to:
do shell script "./script.py" with administrator privileges
AppleScript insists on presenting a popup window to ask for the password. I have also tried passing a null password to sudo with a pipe:
do shell script "echo '' | sudo -S ./script.py"
but that also does not work. (I think it tries to run sudo individually first and then pass the command through, which won't work because the user doesn't have sudo privileges!)
I need a solution where AppleScript will run the Python script with sudo. I would prefer the script stays unreadable and un-executable by average users for security reasons, and is only executed through the AppleScript. (I know that, hypothetically, the users could call sudo script.py and it would run, but that's assuming they even know about sudoers; I'm trying to keep it as secure as possible while still usable).
I'm still pretty new to AppleScript, so any help would be greatly appreciated! Thanks!
When I added ALL ALL=NOPASSWD: /Users/myusername/a to sudoers and ran echo $'#!/bin/bash\nsay $(ls ~root|head -n1)'>~/a;chmod +x ~/a, do shell script "sudo ~/a" ran the script as root without requiring a password.
I'm guessing the problem is that you specified the path like do shell script "sudo ./script.py". Try to use do shell script "sudo ~/script.py" instead. The default working directory is for do shell script is / and not ~/.

How to create an Apple Script that runs a Terminal command?

I want to create an apple script that opens terminal and summons this command : sudo killall coreaudiod
How is this possible?
do shell script "sudo killall coreaudiod" with administrator privileges
the user gets asked for the password then

Resources