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

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

Related

SSH command execution/variable substitution issue [duplicate]

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.

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'

Automatic ssh script without expect and without key (with user/password) [duplicate]

This question already has answers here:
Shell script to automate SSH login using password
(2 answers)
Closed 5 years ago.
I am trying to make a script in bash that connects via ssh to another machine.
I only have user access to this machines so
I can't use expect (It is not installed and I can't install it)
I can't install ssh keys
So I have to log in via username and password.
Is there a way to make my script send my password just using bash?
You can do that with sshpass, however, it's insecure.
sshpass -p YOUR_PW ssh ...
That will connect without asking for password.
You might need to add the flag -oStrictHostKeyChecking=no to ssh for auto-accepting keys.

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.

Sftp using a remote user without shell [duplicate]

This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 8 years ago.
I need to make a shell script that will transfer files to a remote server however, the account given to me has no shell mainly because they want to restrict the access to sftp only. I already have a shell script my only problem is that I cannot automate it. The script stops on the password prompt. I read on most of the passwordless sftp tutorials that I need to generate a keypair but like i've said I cannot do this as the remote account has no shell access. What are the alternatives for passwordless sftp considering an account without shell? The remote server has no 'expect' installed.
Thank you very much.
EDIT: Please also note that I cannot install anything in the local server. I MAY be able to do something on the remote server but not on the local.
On Debian and Ubuntu (maybe other systems also) you can use sshpass
apt-get install sshpass
sshpass -p 'YourPassword' ssh user#host
OR
sshpass -p 'YourPassword' sftp user#host
But the you will have your password printed in your bash_history..
So you might want to pass the password from a file or an environment-variable.
Greets, Eric

Resources