osascript and sudo password entry - macos

I am using osascript in a BASH script for dialog boxes on a MAC system. The problem I am having is several of the commands I need to use require privilages to function correct. If I use sudo in the BASH script, the password prompt shows in the terminal window. Is the some way I can hook the sudo password prompt into an osascript dialog box? Or is there a different way I can handle asking for the password in an osascript dialog box and passing it to some other program to handle it?

What worked for me was to create the BASH script and then use osascript to call it.
$ osascript -e 'do shell script "/Path/yourbashscript.sh" with administrator privileges'
This will prompt a dialog box straight from Apple's infrastructure. Same one you see when you're asked for your username & password.
You can run this in terminal or use a third-party wrapper like, Platypus

You can suppress the password interface by modifying your Mac's authorization rights.
Use the built-in security command line tool or authbuddy to change the system.preferences.accessibility right to allow:
sudo security authorizationdb write system.preferences.accessibility allow
Opening up the system.preferences.accessibility right will permit any user to change the accessibility settings without a password prompt.

Related

Which user is AppleScript using when executing scripts

• Here is the script to be executed via AppleScript:
bash-3.2$ cd /Users/jack/Desktop/
bash-3.2$ ls -l | grep static
-rwxrwxrwx 1 jack admin 65 5 May 08:10 static-routes.sh
bash-3.2$ cat static-routes.sh
#!/bin/bash
sudo route -n add -net 192.168.3.0/24 172.16.254.134
~
• AppleScript contains the following:
do shell script "~/Desktop/static-routes.sh"
• When executing the script from within an AppleScript, by clicking on "Run" button, pop up window saying:
Script Error
sudo: a terminal is required to read the password;
Either use the -S option to read from standard input or
configure an askpass helper
• When exeucuting script from the console without sudo, no additional prompts appear:
bash-3.2$: Desktop jack$ ./static-routes.sh
add net 192.168.3.0: gateway 172.16.254.134
• Here is the snippet from /etc/sudoers:
bash-3.2$ sudo visudo
# root and users in group wheel can run anything on any machine as any user
root ALL = (ALL) ALL
%admin ALL = (ALL) ALL
jack ALL = (ALL) NOPASSWD: /Users/jack/Desktop/static-routes.sh
## Read drop-in files from /private/etc/sudoers.d
## (the '#' here does not indicate a comment)
#includedir /private/etc/sudoers.d
Defaults timestamp_timeout=60
Questions:
• Why this error is showing up, since, I have explicitly added the script to the sudoers file to be executed without password prompt via sudo?
• Which user does AppleScript use to execute the scripts? Is it possible to modify it?
The run a command that requires privileges from AppleScript, you need to specify that by adding the administrator privileges key, as in one of the following:
-- this will presented a standard authorization dialog
do shell script "~/Desktop/static-routes.sh" with administrator privileges
-- this will specifies an administrator account and password
-- (though note, the password will be visible as plain text in the script)
do shell script "~/Desktop/static-routes.sh" with administrator privileges user name XXXX password YYYY
You should not use sudo at the same time you use with administrator privileges; it's unnecessary and creates security holes. However, since you've changed the sudoers file already, you could try this:
do shell script "sudo ~/Desktop/static-routes.sh"
Putting sudo up front like that might cue AppleScript to do the correct thing.
See Technote 2065 for more information.

How do you set the icon for a MacOS app, when you aren't building with Xcode?

I have created a Mac OS app. I am building the app on Ubuntu, and not able to make use of Xcode. When it comes to setting an icon for the app, I am at a loss. How can this be done?
This might appear as a duplicate to this question:
How do I set the icon for my application's Mac OS X app bundle?
However, the solution given here (to simply add the CFBundleIconFile tag in info.plist, with the associated .icns file in the app's Resources directory) does not work for me, nor does it appear to work for some others in that thread. The answer is quite old - is there a newer process? Or, must other steps be taken to get this to work? I am using a program called Image2icon to generate an .icns file - is it that this is not sufficient, and a different process must be taken to generate the .icns?
Good evening, first question with answer, is that once the icon created and placed in the resource your current has not changed? (imagine that in the file info.plist you have named the icon) in this case you may have to delete 2 files to find the new icons below I leave you an applescriipt script that delete them 2 files that the system recreates automatically. in the script you have to change "yourname" and "yourpassword" by your username and password otherwise it will not work. once launched the script the screen will go black and reappear, I put xtrafinder, if you do not use it, erase the line with xtrafinder.
try
set erase to do shell script "sudo find /private -name" & quoted form
of "com.apple.dock.iconcache" user name "yourname" password
"yourpassword" with administrator privileges
do shell script "echo" & quoted form of erase do shell script "sudo rm
-rf -v" & quoted form of erase user name "yourname" password "yourpassword" with administrator privileges
set erase to do shell script "echo" & quoted form of erase & " | sed 's
com.apple.dock.iconcache#com.apple.iconservices#'"
set erase to do shell script "echo" & quoted form of erase
do shell script "sudo rm -rf -v" & quoted form of erase user name
"yourname" password "yourpassword" with administrator privileges
do shell script "sudo killall Dock" user name "yourname" password
"yourpassword" with administrator privileges
do shell script "sudo killall iconservicesagent" user name "yourname"
password "yourpassword" with administrator privileges
do shell script "Sudo killall Finder" user name "yourname" password
"yourpassword" with administrator privileges quit application
"XtraFinder"
do shell script "Sudo open -a /Applications/XtraFinder.app" user name
"yourname" password "yourpassword" with administrator privileges
do shell script "sudo pkill loginwindow" user name "yourname" password
"yourpassword" with administrator privileges
end try

How can i run a sudo command in Bash script?

I want to run the following sample bash script which needs sudo password for a command
#!/bin/bash
kinit #needs sudo password
vi hello.txt
while running the above script it is asking for password.
How can i pass the username and password in the command itself or is there any better way i can skip passing my password in the script ?
TL;DR
You can't—at least, not the way you think.
Longer Answer with Alternatives
You have a couple of options:
Authenticate interactively with sudo before running your script, e.g. sudo -v. The credentials will be temporarily cached, giving you time to run your script.
Add a specific command such as /usr/lib/klibc/bin/kinit to your sudoers file with the NOPASSWD option. See sudoers(5) and and visudo(8) for syntax.
Use gksudo(1) or kdesu(1) with the appropriate keyring to cache your credentials if you're using a desktop environment.
One or more of these will definitely get you where you want to go—just not the way you wanted to get there.
So if you have access to your full system, you can change your sudoers file to allow certain sudo commands to be run w/o a password.
On the command line run visudo
Find your user and change the line to look something like this:
pi ALL=(ALL) NOPASSWD: /path/to/kinit, /path/to/another/command
That should do it. Give it another shot!
Hope that helps
You shouldn't pass username and password. This is not secure and it is not going to work if the password is changed.
You can use this:
gksudo kinit # This is going to open a dialog asking for the password.
#sudo kinit # or this if you want to type your password in the terminal
vi hello.txt
Or you can run your script under root. But note that vi is going to be ran as root as well, which means that it will probably create files that belong to root, that might be not what you want.

Getting sudo to ask for password via the GUI

I have a lua script, running on the Mac, that needs to call sudo.
I'd hoped that Mac OS would automatically bring up a password request dialog, but instead it the command fails by returning 256.
Is there anyway that I can achieve my goal?
Tim
Quick and easy way: run it like this
/usr/bin/osascript -e 'do shell script "/path/to/myscript args 2>&1 etc" with administrator privileges'
Proper and configurable way: use AuthorizationExecuteWithPrivileges API from Authorization Services (in Security.framework).
Both will display standard Mac OS X GUI asking for administrator password and then execute the command as root, the same way as sudo does except that SUDO_USER environment variables will not be set.
If you need to execute individual commands from under user account when you're already elevated to root, you can prepend them with /usr/bin/sudo -u $USER.

how to use sudo command without password in cocoa

I am developing cocoa application for mac. In my application I want to create bandwidth pipe. I am using command "sudo ipfw pipe 1 config bw 50" to create bandwidth pipe.
My problem is that its asking for user password when execute this command. How can i execute this command using objective-c without asking password?
I read somewhere that it can be achieved by inserting "user_name ALL=NOPASSWD ALL" in visudo file. I tried this and found that by inserting this entry in visudo file its not asking for password.
My query is that is there any other method to run this command without password or is there any method to insert "user_name ALL=NOPASSWD ALL" in visudo???

Resources