This is quite a common task for Linux system administrators, when it is needed to execute some command or a local Bash script from a one Linux workstation or a server on another remote Linux machine over SSH.
In this article you will find the examples of how to execute a remote command, multiple commands or a Bash script over SSH between remote Linux hosts and get back the output (result).
This information will be especially useful for ones, who want to create a Bash script that will be hosted locally on a one Linux machine but would be executed remotely on the other hosts over SSH.
Cool Tip: Connect to a remote SSH server without typing a password! Configure a passwordless authentication! Only 3 easy steps! Read more →
SSH: Execute Remote Command
Execute a remote command on a host over SSH:
$ ssh USER@HOST 'COMMAND'
Examples
Get the uptime of the remote server:
$ ssh root@192.168.1.1 'uptime'
Reboot the remote server:
$ ssh root@192.168.1.1 'reboot'
SSH: Run Multiple Remote Commands
In the most cases it is not enough to send only one remote command over SSH.Much more often it is required to send multiple commands on a remote server, for example, to collect some data for inventory and get back the result.
There are a lot of different ways of how it can be done, but i will show the most popular of them.
Run multiple command on a remote host over SSH:
$ ssh USER@HOST 'COMMAND1; COMMAND2; COMMAND3'
– or –
$ ssh USER@HOST 'COMMAND1 | COMMAND2 | COMMAND3'
– or –
$ ssh USER@HOST << EOF COMMAND1 COMMAND2 COMMAND3 EOF
Cool Tip: SSH login is too slow? This can be fixed easily! Get rid of delay during authentication! Read more →
Examples
Get the uptime and the disk usage:
$ ssh root@192.168.1.1 'uptime; df -h'
Get the memory usage and the load average:
$ ssh root@192.168.1.1 'free -m | cat /proc/loadavg'
Show the kernel version, number of CPUs and the total RAM:
$ ssh root@192.168.1.1 << EOF uname -a lscpu | grep "^CPU(s)" grep -i memtotal /proc/meminfo EOF
SSH: Run Bash Script on Remote Server
The equally common situation, when there is some Bash script on a Linux machine and it needs to connect from it over SSH to another Linux machine and run this script there.The idea is to connect to a remote Linux server over SSH, let the script do the required operations and return back to local, without need not to upload this script to a remote server.
Certainly this can be done and moreover quite easily.
$ ssh USER@HOST 'bash -s' < SCRIPT
Cool Tip: Want to ROCK? Start a GUI (graphical) application on a remote Linux workstation over SSH! Read more →
Example
Execute the local script.sh
on the remote server:
$ ssh root@192.168.1.1 'bash -s' < script.sh
Nice Post there..I was wondering what if want to execute the script with an argument.
Try this:
as explained here:
https://unix.stackexchange.com/questions/87405/how-can-i-execute-local-script-on-remote-machine-and-include-arguments
Hi,
How about executing a command remotely and making sure that it will be killed once i kill my ssh session. or getting of PID of either SSH or the actual command on remote-node via a script.
Something like this I want to achieve:
Example: collecting pcap on remote addr and storing it locally.
I was unable to get the proper Pids in this case. Any reply would be helpful.
– Thanks.
How to check remote server services which require sudo privilege?
How to check remote server services which require sudo privilege from local script file?
is what I use to send a bash script over the wire with sudo priviliges
How do create a scrip to run commands on multiple remote machines?
When the first one gets executed, the response returned to STOUT, and the next one is not executed until I ^C the previous command.
I tried using ‘exit’ command and ‘&’ , neither is working.
useless writing! Where it’s remote script executing example about sudo use?
Hello everyone,
I am new to linux and trying to learn it. I have task to complete “Get last 3 login details of list of linux machine with date and time.”
Is there any way to achive it?
Thanks in advance
Thank you mate!
Command $backdoor
$bash -0 $(wich bash)
Whoami..
#root
You should change these examples to use double quotes – I got tripped up putting variables in these single quotes and took me a while to realise bash treats it as a string…
como puedo hacer que solo me pida una vez la contraseña de mi servidor? hice mi escript y como realizo varias tareas me pide varias veces la contraseña
usa ssh-copy-id para copiar la llave
ssh root@192.168.0.10 <<Filler
feh -F ~/image.jpg &
exit
Filler
did not help me.. still in ssh connection =(
This was very helpful !
Thanks 🙂
Can someone help me write a shell script to shutdown a Ubuntu computer?
I’m going to have it run on a Mac, so that it will ssh into the Ubuntu and shut it down. I need it to open Application “Terminal” then ssh name@123.45.67.89, then give it the password.
Then I need it to issue command “sudo poweroff”, and give it the password again.
I know how to do this manually by opening Mac’s Terminal. I just type in “ssh name@123.456.78.9, it asks for the password, I type it in, and it’s connected. Then I just type in “sudo poweroff” and it asks for the password again, I type it in, and bam, it shuts down the Ubunt computer.
The problem is, I need to automate this to do it at a specific time of day. On the Mac, there is what is called “Automator”, and you can set up ICalendar Events to run an “Automator Workflow” with a Shell Script. I just don’t know how to write the Shell Script to do what I can do manually? Any help is appreciated greatly!
I solved this. Here’s what works:
tell application “Terminal”
activate
do script (“ssh test@192.168.1.10“)
delay 6
do script “password” in front window
delay 7
do script “sudo poweroff” in front window
delay 5
do script “password” in front window
end tell
Why when I get a script from somewhere by curl & then pipe to bash to run it on my local server, some commands in a bash functions failed to execute, no error, no code error btw..just the called functions don’t execute.. How do I know my code is ok, because when I output it as a file from curl, I chmod and run it like ./..scriptfile.sh.. Code run fine.. Any idea and how do I resolve this?
This was very helpful for me. I am very new to this and just ordered a VPS yesterday and these guides are really helping me work my way around. I appreciate this simple resource you’ve put together!
how to run a script with sudo previllages on remote server:
ssh -tt rundeck@$Batch_Host ‘ bash -s’ < /home/abhishekc/dem.sh
i am getting permission denied
hi is it possible to use the variable inside EOF, like below