SSH remote command executing a script - bash

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'

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.

sshpass: No such file or directory

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.

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.

run ssh script into ubuntu instance do something, when exit, stay in ubuntu

I am running a very simple script that will ssh into a remote ubuntu instance, move around the directory structure execute a few things, then I want the prompt to stay in Ubuntu. When the script ends, in ends back at the local prompt. How do I make modify the script so that it finishes with the remote prompt?
local$ ssh -i xxx.pem ubuntu#xxx.ap-region.compute.amazonaws.com \
"cd virtualenv; ls -lh;"
There are two things needed to be added to your commandline:
The bash command in the end starts the bash shell (you can start any other you want)
The -t switch will make sure the remote server will allocate you TTY and your shell will work as expected:
local$ ssh -t -i xxx.pem ubuntu#xxx.ap-region.compute.amazonaws.com \
"cd virtualenv; ls -lh; bash"

sbt (Scala) via SSH results in command not found, but works if I do it myself

So I'm trying to do something that involves running sbt over an SSH command, and this is what I'm trying:
ssh my_username#<server ip> "cd <project folder>; sbt 'run-main Foo' "
When I do that however, I get an error message: bash: sbt: command not found
Then I go SSH into the server myself, cd to the project folder, and run sbt 'run-main Foo' and everything works nicely. I have checked to make sure sbt is on the $PATH variable on the remote server via ssh my_username#<server ip> "echo $PATH" and it shows the correct value.
I feel like this is a simple fix, but cannot figure it out... help?
Thanks!
-kstruct
When you log in, bash is run as an interactive shell. When you run commands directly through ssh, bash is run as a non-interactive shell, and therefore different initialization files are sourced (see the bash manual pages for which exactly). There are a number of ways to fix this, e.g.:
Use the full path to sbt when calling it directly through ssh
Edit .bashrc and add the missing directories to the PATH environment variable
Note that your test ssh my_username#<server ip> "echo $PATH" actually prints PATH on your client, not your server, because of the double quotes. Use ssh my_username#<server ip> 'echo $PATH' or ssh my_username#<server ip> env to print PATH from the server's environment. When checking using env, you will see that PS1 is only set in interactive shells.

Resources