Running bash command through ssh that only exists on remote - bash

I am writing a bash script that involves ssh-ing into a remote host and running commands there. That in itself is not a problem. The issue is that I want to run a command which doesn't exist locally, only on the remote. The script fails with bash: line 1: type: remote_only_command: not found, even though it's successfully connecting to the remote host and can run basic commands without issue.
I can run the command on the remote host if I ssh in and run it manually. I've tried writing a separate bash script on the remote host and running that through the script (sh remote_script.sh), but that gets the same command not found error.
ssh $REMOTE var=$var 'bash -s' << 'EOF'
ls # works no problem, lists files on the remote server
remote_only_command # bash: line 1: type: remote_only_command: not found
EOF
Is it possible to run a command that is only accessible from the remote host and not locally where the script is being run?

I think this is the way it should work, as the command is only executed on the remote host. But i suspect your problem is the environment, which is NOT permitted over ssh. Try to use the complete path to the command, eg:
/path/to/remote_command

Related

How to ssh into remote linux server using username / ip address and password from window using shell script?

What I am trying to do is to login to remote Linux server using SSH from my Windows machine by running a shell script via git bash.
I would like to write a script which will be used by an user with basic IT knowledge. This script will execute a bunch of commands on the remote machine, so it does need to establish a SSH connection.
What I have tried to write in this script so far is:
ssh username#ip <password>
EDIT: You should consider installing Jenkins on the remote system.
Running a command on a remote system can be done via:
ssh user#host command arg1 arg2
If you omit the password, a password prompt will appear, to get around the password prompt, you should consider setting passwordless SSH login. https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login
Rephrasing what you said, you want to write a script (namely script1.sh) which does the following (in this order):
Start a ssh connection with a remote
Execute some commands, possibly written in another script (namely script2.sh)
Close the connection / Keep it open for additional command line commands
If you want to put the remote commands on script2.sh instead of listing them in script1.sh, you need to consider that script2.sh must be on the remote server. If not, then you may consider to delegate to script1.sh the creation/copy of script2.sh on the remote machine. You may copy it in a temporary folder.
In this case, my suggestion is to write script1.sh as follows:
Copy of script2.sh with
scp /path/to/local/script2.sh user#host:/path/to/remote/script2.sh
Switch bash sheel to remote shell with
ssh user#host
Execution of script2.sh
sh /path/to/remote/script2.sh
Instead, if you prefer to list everything in just one script file, you may want to write:
Switch bash sheel to remote shell with
ssh user#host
Execution of commands
echo "This command has been executed on the remote server"
echo "This command has also been executed on the remote server"
..
Possibly closing the SSH connection to prevent that the user execute additional commands
You may consider to copy the ssh-keys on the remote server so to avoid password prompts. More information here.

How to run a script from local on remote but at some point continue running the script on the local server?

I need to run a bash script that takes some parameters from server-1 and then from my local server where I ran the script with
ssh user#server-1 bash -s <script.sh
I then need to use those parameters to be executed with all kind of commands on my local server and also server-2 is involved. But the script will still be running on server-1 because of
ssh user#server-1 bash -s <script.sh
Maybe I can use 2 scripts but I want them to be only on local server. and putting in the script more commands after SSH doesn't seem to be working.
I would place the script on the remote server and remote execute it via SSH.
If the script should change over time, then break it up into 2-3 steps
1. gather any additional parameter from remote machine
2. copy script to remote machine using scp
3. ssh to "remote execute" script on remote machine
Am not sure what parameter you need from the remote system.
I would try to hand it over via command line options to the script in #3.
Otherwise "hack"/patch it in before #2.

Command doesn't run from shell script on remote windows machine: Unable to execute command or shell in remote system

I have a Windows Client machine and I would like to invoke a powershell script on that client from my local machine i.e. Mac.
I have ssh on Windows via freesshd.
Also, i need to provide password via script hence I ended up using sshpass.
From my Mac Terminal, while the command:
sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"
runs perfectly and returns the content of whatever directory the shell lands in, i am unable the get the same result from inside a shell script.
The simple script is as below:
cmd='sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"'
echo `$cmd`
What I get is:
Unable to execute command or shell on remote system: Failed to Execute
process.
I have tried various permutations of " and ' to get the correct command string but to no avail.
What is wrong here?

Jenkins Execute shell is failed after executing ssh to a remote server

I am creating a Jenkins job in which am running a ssh command to execute a script for comparing two folders using diff command on a remote server. Script is running fine, output file is getting created. But after this command Jenkins execute shell block is failed.
Command:
ssh -T user#dtest.com "bash /tmp/sample.sh" >> result.txt
Log:
ssh -T user#dtest.com "bash /tmp/sample.sh" >> result.txt
stdin: is not a tty
"Execute shell" is marked as failure
I am not sure what sample.sh is supposed to do, but I understand that you are trying to capture what is logged by this script.
I would try several solutions:
ssh -T user#dtest.com "bash /tmp/sample.sh >> result.txt"
This should save your output in your remote server. Then you could copy this file from remote to local using:
scp user#dtest.com:/remote/dir/result.txt /local/dir/
More context: Copying files from server to local computer using ssh
If you are choosing this solution, you could also consider to write your result.txt directly from your script, and keep the console output for important logging purpose.
Another Solution I could think of would be to use
ssh user#dtest.com "bash /tmp/sample.sh" > result.txt
With this solution you will redirect your output directly to your local machine.
But you will need to delete the ssh "-T" option. And you will run into other problems with Jenkins. So this might not fit you.
ssh -T Disables pseudo-tty allocation, what sounds like your problem's root cause. (https://docs.oracle.com/cd/E36784_01/html/E36870/ssh-1.html)

How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer?

I welcome rephrasing of my question, because I'm not sure exactly what the problem is called.
I am trying to run a shell script via SSH using a command of the following form:
ssh -o StrictHostKeyChecking=no lxplus0035 "cd ~/test; bash script1.sh"
When I do this, the complex script script1.sh breaks with many syntax errors, operand errors and other errors. An example is as follows:
stty: standard input: Invalid argument
The script works fine when run directly, not via SSH, in the local system and in the remote system (when connected in an SSH session). Why might executing the script via SSH cause these problems?
The stty problem is not related to your environment, it's a result of the SSH command not allocating you a TTY (What is Pseudo TTY-Allocation? (SSH and Github)). Adding -t argument for ssh should fix it.
Further information:
http://go2linux.garron.me/linux/2010/11/ssh-t-open-pseudo-tty-run-commands-remote-server-809/
http://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/

Resources