sshpass: No such file or directory - shell

Bellow command if i write inside a script (test.sh) and execute directly on the specific machine it works.
sshpass -p $HOST_PWD sftp testuser#host <<!
cd parent
mkdir test
bye
!
But when i try to run (directly below scrip or invoking the test.sh file in the specif path) in Jenkins with "Execute shall script on remote host using ssh" it failing with
sshpass: Failed to run command: No such file or directory
I have installed sshpass, lftp and rsync in the remote machine
Issue :
I have added export $HOST_PWD in .bashrc of specific machine as well as Jenkins but in not finding it
Script placed in specific machine, if directly executed the script in that machine it works even with $HOST_PWD. But not working if we invoke from jenkins either script or directly scrip using "Execute shall script on remote host using ssh"
Working with Changes :
Instead of $HOST_PWD if i added directly password it works.

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.

run .sh script via Jenkins to execute aws command error

my problem is that i try to execute shell script to copy created files from msbuild to AWS s3 via Jenkins.
Then i add new build step "Execute Shell" and set to execute shell script by command: sh publishS3.sh nothing happens and files doesn't apper in s3 bucket.
my Jenkins use Local Windows Server.
Then i try to execute the shell script by typing sh publishS3.sh in Jenkins local directory all ok , files was copyed secessfully to s3 bucket , but if i try to do it from jenkins nothing was happen. My publishS3.sh script is:
#!/bin/bash
aws s3 cp Com.VistaDraft.Common.dll s3://download.vistadraft.com/MVP
i was tryed to to check witch output i receive after execute by adding at the end command > output.txt but Jenkins generate an empty file. If i try to do the same locally i was receive an message that i secessfully copyed files to s3. i Set the shell script path of jenkins C:\Program Files\Git\git-bash.exe and using git-bash.exe locally too. Maybe whom know where is a problem ? Please suggest.
You could try to add -ex in the first line of the script to allow you to see what it's doing and ease the debugging:
#!/bin/bash -ex
# rest of script
Make sure the aws tool is in the PATH of the environment where Jenkins runs your script. It might help if you specify full path to the command.
You could put which aws in your script to see what's going on.

SSH remote command executing a script

I have two hosts, hosts A and B. A has a script (generate) that compiles my thesis:
#!/bin/sh
pdflatex Thesis.tex
When running this command on host A (console window) it works perfectly.
I am basically trying to connect from host B to A and run the generation command as an ssh remote command. All the keys are properly set. When I run the command, I get the following:
hostB> ssh user#hostA exec ~/Thesis/generate
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
! I can't find file `Thesis.tex'.
<*> Thesis.tex
I tried adjusting the script so that it considers the directory:
pdflatex ~/Thesis/Thesis.tex
But because the Thesis.tex inputs some others files (images), I get an error message.
I presume the problem is some sort of enviroment that doesn't exist in remote commands. How do I fix this?
ssh will run your command in your home directory. You probably wanted to run it in your ~/Thesis directory.
Just cd first and it should be fine:
ssh user#hostA 'cd ~/Thesis && ./generate'

Jenkins not able to execute ssh script

I have below ssh script which I am trying to execute by Jenkins, it runs fine when I invoke it from shell.
#ssh to remote machine
sshpass ssh 10.40.94.36 -l root -o StrictHostKeyChecking=no
#Remove old slave.jar
rm -f slave.jar
#download slave.jar to that machine
wget http://10.40.95.14:8080/jnlpJars/slave.jar
pwd
#make new dir to that machine
mkdir //var//Jenkins
# make slave online
java -jar slave.jar -jnlpUrl http://10.40.95.14:8080/computer/nodeV/slave-agent.jnlp
When I execute this script through shell it downloads the jar file to remote machine and also makes a new directory. But When I invoke it by shell plugin of jenkins, every command runs seprately. so the jar gets downloaded at master and also directory get created at master.
Also I am using sshpass for passwordless automated login, which fails sometime. Is there any other way of doing this.

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