Call remote sh script from local sh file - bash

I have a problem with running sh scripts on CentOS which calls remote sh file. On user#host1 I have start.sh file with next command inside
NODE1_SSH_PATH=user#host2
PROGRAM_HOME=/home/user/app
ssh $NODE1_SSH_PATH $PROGRAM_HOME/bin/run.sh > start.log
Result of this script is next:
bash: /home/user/app/bin/run.sh: Permission denied
I tried run this script with chmod like this:
ssh $NODE1_SSH_PATH chmod u+x $PROGRAM_HOME/bin/run.sh > start.log
But in this case I have't got any result, log file is empty to. Could someone help me to slow this I hope simple task?

I believe /home/user/app/bin/run.sh is not executable.
try this
ssh $NODE1_SSH_PATH /bin/bash $PROGRAM_HOME/bin/run.sh > start.log

Related

Running into error with BASH script with permission denied, but when running the command directly in bash shell its getting executed

When running below command directly in bash shell, I am able to get the output. But when I am passing it via BASH script getting access denied. Any help would be appreciated
$ jq -r '.id' Repooutput.txt
dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
Error:
$ sh test.sh
test.sh: line 3: /c/ProgramData/chocolatey/bin/jq: Permission denied
I think the reason is that when executing the script with sh test.sh we're asking the POSIX interpreter (shell) to execute the content on the script, while when executing it with ./test.sh we're asking the script to "execute itself".
For the latter, the file needs to have execution permissions, which you can add with
chmod +x test.sh
Issue was with the naming convention of JQ inside BASH folder path, because of which the script was unable to pick the command. Renaming the JQ within BASH folder resolved this

permission denied reading from file with <, even with sudo in use

I need to run a script from server A in Server B. After ssh into server B, I ran the following command:
sudo ssh root#ip_A 'bash -s' < root/work/task.sh
I am getting the error below:
-bash: /root/work/task.sh: Permission denied.
On server A, I have done sudo chmod 777 task.sh.
Please thanks.
This is one of the few places where cat adds value even when not concatenating multiple files:
sudo cat /root/work/task.sh | ssh root#ip_A 'bash -s'
Because redirections such as < are run by the shell before the program being invoked is started, sudo can't change the permissions used for such redirections (it hasn't started yet!). By contrast, sudo cat somefile runs sudo first, then cat, which then opens somefile; since sudo runs first in that case, escalated permissions are available.
i test case 1:
sudo ssh root#ip_A 'bash -s < /root/work/task.sh'
which task.sh saved in ip_A, and works
and test case 2:
sudo ssh root#ip_A 'bash -s' < /root/work/task.sh
and it works too, no task.sh in ip_A, only has this file in local host.
do not know what your problem, can u show us your tash.sh?

EC2 User Data Script .sh file and Manual Execute Differ

I am trying to execute the following user data script
sudo wget https://files.mysite.com/downloads/myFile.tar -P /opt
sudo tar -xvf /opt/myTar.tar -C /opt
sudo /opt/myFile.sh
When I execute the .sh file manually I see this:
Extracting...
Unpacking...
Cleaning up...
Complete
and it creates a directory in /opt/myDirectory
I do see the console in /var/log/cloud-init-output.sh but it doesn't seem to create the directory when run as part of the userdata script.
You're calling a shell script within a shell script, so you need to make sure:
have this as the first line of your ec2 user data script #!/bin/bash
call myFile.sh with the source command (alias is .) like this: . /opt/myFile.sh so it will run the myFile script
Note: the ec2 user data script runs as root so you do not need to have sudo each time you run a command.
Solution: CD into the directory first.
#!/bin/bash
cd /opt
wget https://example.com/myTAR.tar
tar -xvf myTAR.tar
/opt/mySH.sh

ssh can use in bash shell, can't use in shell file

I use ssh to login EC2(Amazon)
ssh -i /home/XXX/mykey.pem ec2-user#ec2-NN-NNN-NN-NN.us-foo-N.compute.amazonaws.com
It success,but when I write the command to the file like this:
login.sh
#! /bin/bash
ssh -i /home/XXX/mykey.pem ec2-user#ec2-NN-NNN-NN-NN.us-foo-N.compute.amazonaws.com
after chmod +x login.sh
I run the script, it return:
ssh: Could not resolve hostname
So how to solve it, thanks.
There are two likely reasons:
You have a typo in the hostname in the script.
The script has a CRLF instead of LF as the line ending on the ssh line. This often happens when you edit the file on a Windows system and transfer it to Unix. Use dos2unix to fix the script.

How do you execute a command on a remote system insde a BASH script?

As part of an intricate BASH script, I'd like to execute a command on a remote system from within the script itself.
Right now, I run the script which tailors files for the remote system and uploads them, then through a ssh login I execute a single command.
So for full marks:
How do I log into the remote system from the bash script (i.e. pass the credentials in non-interactively)?
How can I execute a command (specifically "chmod 755 /go && /go") from within the script?
Following Tim Post's answer:
Setup public keys and then you can do the following:
#!/bin/bash
ssh user#host "chmod 755 /go && /go"

Resources