do shell script results in command not found - terminal

I've found sh-script for CPU Throttling and would like to schedule it on startup using AppleScript in the following way:
do shell script "sudo /Users/BohdanAir/Documents/Automation/cputhrottler.sh" "password mypassword" with administrator privileges
And get the following error:
error "sudo: /Users/BohdanAir/Documents/Automation/cputhrottler.sh: command not found" number 1
P.S: The pass provided to the sh file is being fully copied from the Get Info option (CDM + I)

Not even sure how you received that error message when the do shell script command shown in your question does not even compile.
The "password mypassword" portion actually has to be password "mypassword" for it to compile.
The reason you're getting:
error "sudo: /Users/BohdanAir/Documents/Automation/cputhrottler.sh: command not found" number 1
Is because cputhrottler.sh is not set as executable.
Make it executable using the following command:
chmod u+x /Users/BohdanAir/Documents/Automation/cputhrottler.sh
This will allow it to execute and not throw that particular error message.
Or use the following convention:
do shell script "sh /Users/BohdanAir/Documents/Automation/cputhrottler.sh" password "mypassword" with administrator privileges
Note that sudo was replaced with sh, or use other shell. In this manner the shell script does not need to be made executable.

Related

Unable to remotely execute Shell script with root priviledges

There is a script located in following path
/usr/local/bin/subrun
The Owner & usergroup of above file is root
When I run above script locally using following command in BASH shell:
/bin/sh /usr/local/bin/subrun
It runs perfectly fine
But When I try to run same script remotely using following command in BASH shell:
ssh user#host /usr/local/bin/subrun
It throws an error :
/usr/local/bin/subrun: Command not found.
Question : How do I resolve this ? Does this has to do with 'root' (Owner & Usergroup of script)
PS: Also there is another script in the same location with different Owner & usergroup (for e.g. Owner : manager & Usergroup : admin). This script can be run locally or remotely without any issue
PS2: 'subrun' script file has following levels of permission '-rwxr-xr-x' (And I am not allowed to change permission using chmod. It says Operation not permitted
Since you run it locally as:
/bin/sh /usr/local/bin/subrun
rather than just:
/usr/local/bin/subrun
it's probaby not an executable file on either machine and so you should do the same when trying to run it remotely and use:
ssh user#host '/bin/sh /usr/local/bin/subrun'
instead of
ssh user#host /usr/local/bin/subrun
or make it executable on every machine by running chmod oug+x /usr/local/bin/subrun or similar on every machine and THEN you can call it as just /usr/local/bin/subrun on every machine.

sudo: command not found while using plink

Hi i have created a batch file (run.bat) that after execution connects me to UNIX server with help of plink. But issue starts from this point i have to execute a script after connection to my server the script contains a command sudo -l. After the execution i get the error as mentioned in subject can anyone help me on this issue ??
Batch File-:
"C:\Program Files\PuTTY" plink -ssh -pw Tos#12Ts w44dvftyw#caa1607UX009.wvd.abcd.net /opt/sieb/w44dvftyw/run.sh
Script file(run.sh) -:
#!/bin/bash
sudo -l
It says
sudo: command not found
But when i run my script normally on UNIX server it runs with no issues. What am i missing here to make it work this way please help.
Scripts such as ~/.profile or ~/.bash_profile responsible for setting up the current user's PATH are run only on login shells.
Running sh -c 'somescript' (as performed by ssh host 'somescript') is neither a login shell, nor an interactive shell; thus, it does not gain the benefit of such scripts.
This means that additions to the PATH (in your case, /usr/local/bin) may not be present with commands run in this way.
Among your options:
Pass the PATH you want as part of the command to remotely run. This might look like:
plink -ssh user#host "PATH=/bin:/usr/bin:/usr/local/bin /opt/sieb/w44dvftyw/run.sh"
Embed a working value in the script you're running:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin
# ...put the rest of your script here.

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 ~/.

PSQL run from shell script - command not found?

I try to execute an PSQL from shell, and the thing is - it returns the error "command not found". I have a shell script in which there're lines:
ID3=`more DATA/Id3.txt`
psql -h localhost test test -Atc "SELECT id, reference, timestamp FROM restricted WHERE id='`$ID3`'"
In the Id3.txt there's only the ID. When the psql command is written and executed direct through prompt - there's no problem at all and correct value is returned. When executed with a .sh file - error "command not found" is brought up. I have no clue why. Maybe anyone have a idea?
In your script try to add which psql to see whether you can find the executable
Run below command on your console: whereis psql
And then replace psql inyour script with the output of above command. This

How to use sudo inside of a Run Script build phase in Xcode 4?

I need use execute a command inside of a script in a Run Script build phase in Xcode 4 using sudo. However, the compiler complains:
sudo: no tty present and no askpass program specified
Anyone have a clever solution for this problem?
One solution is to place the sudo password in an executable shell script like the following:
#!/bin/bash
echo thesudopassword
This shell script might be called password.sh
Then, setup the environment variable SUDO_ASKPASS=password.sh
Once this is setup, the -A option can be passed to sudo. This option uses the ASKPASS program to obtain the sudo password. The ASKPASS program need only write the password to stdout.
So, for example,
sudo -A ditto -V /tmp/testserver.dst /
This is obviously a rather insecure solution, but it does work.
Two ideas that haven't been suggested yet, both of which are probably better/safer than the currently accepted answer:
First option would be to put the part of the script that needs to be run as root in a script file (.sh or something), and then make it setuid as root: chmod go-w,+sx scriptfile, sudo chown root scriptfile. This means that script will automatically run as root, which avoids you needing to authenticate to run it (just to change it). As long as its operation isn't subject to user input, this should be quite safe. (Of course, if you make a script that takes an input argument and deletes it or runs it, or does most anything else with it, that would not be safe.)
Second option would be to use applescript (possibly via osascript). Applescript allows you to do shell script "sudo command goes here" with administrator privileges, which will pop up a graphical dialog asking for a password.
The first of these options would be good for an automated environment, though it might not deal well with (for example) being checked into an SCM, or being sent to another user. The second option would work better with that, but requires a password input every time, so doesn't work as well for an automated build script.
Another solution to this problem is to modify sudoers file and add your account to it and state that you should never be asked for the sudo password. To accomplish this is fairly straightforward:
run:
sudo visudo
In the User privilege specification section add a line that looks like
youraccountname ALL=(ALL) NOPASSWD: ALL
Of course, this can be a dangerous thing to do, so be careful. I would suggest reading the man page for sudoers and visudo before going this route.
After much searching I found the following solution.
https://forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112
Summary
Create a keychain and store your admin password in the keychain
Create a script which uses /usr/bin/security to access the password In your run script,
Set the ASK_PASS env variable and use the -A option with sudo
You can either run commands directly as a administrator with the following (changing echo YourCommandHere > /tmp/hello to your command):
osascript -e 'do shell script "sudo echo YourCommandHere > /tmp/hello " with administrator privileges'
Or run a script in your source directory using:
osascript -e 'do shell script "bash -x $SOURCE_ROOT/MyAdminScript.sh 1>/tmp/build-log 2>/tmp/build-log.err" with administrator privileges'
This runs the script and logs it output to /tmp/build-log and /tmp/build-log.err
For useful variables in the script see https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6
You can also execute XCode giving it the project as parameter from the Terminal using sudo like this:
sudo /Developer/Applications/Xcode.app/Contents/MacOS/Xcode /path/to/your/project.xcodeproj
This is the easiest solution I could think of, but there may be some drawbacks, since you would be executing XCode as root.
No need to write your sudo password anywhere. Just open a terminal window and type
$ sudo echo "hello"
Once you've typed your password, it will be good for a while - not sure how long - and the shell spawned by Xcode will inherit this permission.
If you get the "no tty present" message again later, just repeat the procedure

Resources