execute commands on remote server using shell script [duplicate] - bash

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.

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.

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'

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.

How to return to the script once sudo is done

I am on a server and running a script file that has following code.
ssh username#servername
sudo su - root
cd /abc/xyz
mkdir asdfg
I am able to ssh... but then the next command is not working.. the script is not sudo-ing. any idea?
Edit: Able to create a mech id and then do the things.. though still looking for the answer to above question :|
First of all your command will "stuck" on the first line because it will go into an interactive mode. The ssh command will require a password to be provided by a user (unless there is an sshkey being used) . And if the ssh is logged into the remote server then it will wait for user commands from standard input.
Secondly the lines following the ssh command will be executed only when the first process has exited. This is why your script is not "sudoing" - it's waiting for the ssh to end.
So if your point is to run a command on a remote server then put the command as a parameter into the same line as ssh connection. In your case:
ssh user#server sudo su - root
But this will not be of satisfaction for you. I suggest you create a script of what you want to execute on the remote server and then execute the script.
ssh user#server scriptName
The sudo thing here is very tricky because again your script might get stuck in the interactive mode waiting for a password to be inserted so I suggest you think again on the basis of the script.
mb47!
You want to run the script on the remote computer, correct?
On the remote machine, create a file containing the commands you would like to execute.
Then, on the other machine, run ssh user#machine /path/to/script/you/created/earlier
I hope this helps!
ALinuxLover

Resources