SSH command execution/variable substitution issue [duplicate] - bash

This question already has answers here:
Escape dollar sign in string by shell script
(6 answers)
remote ssh command: first echo output is lost
(2 answers)
Closed 1 year ago.
I was trying to execute a ssh command and I see it takes environment variable from the host (local) machine rather than the machine I ssh into (remote server).
Example:
firstuser#remote> ssh testuser#remote echo ${USER}
firstuser
It was expecting it to echo testuser instead of firstuser as it is the username I connected with.
If it can help, I am using CentOS7.
Any thoughts/explanations on this specific issue?

If I understand this correctly, you are getting the variable ${USER} from the host (local) machine not from the SSH (remote) server. This is because you are executing it all in one command. Your command is being processed locally - meaning the variable is being substituted - before being sent to the remote (SSH) server.
You can solve this by passing the command as a string that will not be translated/processed/substituted locally to, in your case, firstuser, or whichever other previously declared variable.
firstuser#mymachine> ssh testuser#mymachine 'echo ${USER}'
Important note here is that echo ${USER} is in single quotes, it will not work with double quotes. This is basic bash/shell rule but as a courtesy, you can find out more about this at https://missing.csail.mit.edu/2020/shell-tools/#shell-scripting
In a nutshell, when a variable is being processed/interpreted/substituted locally as you are trying to do, the remote SSH server receives the litteral: echo firstuser not echo ${USER}, which would be processed remotely, as intended.

Related

Shell script EXECUTE CODE ON REMOTE SERVER AND get the result in local [duplicate]

This question already has answers here:
how to execute an local script in remote server with parameters
(2 answers)
Closed 4 years ago.
I want to check when the files have arrived on an remote unix server. I have made an script on my local server which puts filename and its date into an csv file but I need that file to be saved in my local server and not on remote server.
What should be the command like which let's me ssh to that server and execute rest of my code there and output the result in my local.
you may try doing:
ssh [username]#[servername] "command" >> /path/to/outfile.out
However, This command required password-less authentication between source and destination.
You need to setup key based authorization and then just execute commands like that:
ssh USER#HOST 'COMMAND'
Or you can use sshpass to give a password directly in command line (not recommended!):
sshpass -p 'YourPassword' ssh USER#HOST 'COMMAND'

SSH inside SSH heredoc [duplicate]

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 4 years ago.
I'm writing a script that logs into a remote server via SSH and executes some commands, one of which logs into a second remote server (which I can't access locally) via SSH and executes a command. After that nested SSH command, my original SSH command terminates and does't complete the rest of the heredoc. I've simplified the script a bit, but I get the same results:
#!/bin/bash
ssh server1 <<'EOF'
echo one $HOSTNAME
ssh server2 'echo two $HOSTNAME'
echo three $HOSTNAME
EOF
my output looks like this:
one server1
two server2
I would expect to see three server1 at the end of my output, but it doesn't happen. I'm able to separate these into two SSH commands and get what I need, but I'm curious why this is happening, and is it possible to get what I expect in one shot?
Replace
ssh server2
with
ssh -n server2
to prevent ssh reading from stdin.

execute commands on remote server using shell script [duplicate]

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I have a deployment script located on a build server. Each time I need to generate the build I need to login to the server using ssh and then trigger the deployment script.
I managed to generate the public/private keys so that I need not enter the password to login to the build server. But still I need to login and run the deployment script.
Is there a way where to automate the login, executing the deployment script on the build server and then exit from in one local script. How to achieve this
You can use a pipe (assuming you are using a *nix OS):
echo "your --command --here" | ssh user#host
Usually it's just
ssh buildserver /path/to/build.sh
You may need to tweak the options though.
Just do ssh <HOST> <COMMAND> in a single line. If you can already
log in using keys you won't have to type a password. Example:
$ ssh localhost 'echo hi'
hi
This <COMMAND> is run synchronously. That means that ssh won't
finish until <COMMAND> run on the remote server has finished. See yourself:
$ ssh localhost 'sleep 10'
This command will wait for 10 seconds and you won't be able to type new commands until it's finished.

Can I call a function in ssh in unix scripting [duplicate]

This question already has answers here:
Shell script: Run function from script over ssh
(3 answers)
Closed 5 years ago.
Am trying to login into a remote server, and I want to function to be executed on remote server. Can I send the function name including in ssh command.
Of course you can. the below is the appropriate syntax for it. You can run one or more commands separated by semicolon.
ssh -n -l yourusername yourremoteserver "pwd; hostname; netstat -tupln | tail -5"
Let me know if this works for you.
Note - Be aware that in will ask for your password. If you are planning to use this inside a script, you should copy your keys to the remote server you are trying to run the command on, and only then it will authenticate using the keys instead of prompting for your password. Copy the same is a straight forward process, really simple, you can see the steps here:
https://askubuntu.com/questions/4830/easiest-way-to-copy-ssh-keys-to-another-machine

run a local script as root on remote server [duplicate]

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I try to run a local script on multiple remote servers as root. I don't have su to root on those but just can run root commands using sudo. So far I tried:
for host in $(cat hosts_list); do ssh -tt $host "echo mypassword | sudo bash -s" < ./myscript.sh
And in myscript.sh there is something like:
echo "test test123" >> /etc/tests
exit 0
But it looks like not working and won't change the file. What is the proper way to run this script as root and without typing password separately for each host?
Ok, then why do you "echo mypassword" ?
Can't you add your SSH account to the sudoers file with NOPASSWD ?
From man sudoers:
authenticate If set, users must authenticate themselves via a password (or other means
of authentication) before they may run commands. This default may be
overridden via the PASSWD and NOPASSWD tags. This flag is on by default.

Resources