How to execute a script on remote server by ssh command

Introduction

From this tutorial you would learn how to execute a local script on remote server by ssh command.

Environments

  • Linux/Mac OS

The script

we write a script in current directory,which just prints the version info of the OS.

➜  ~ cat a.sh
#!/bin/bash
uname -a

Execute it on remote server

ssh [email protected] 'bash -s' < ./a.sh
Linux server1 2.6-el6.x86_64 #1 SMP Thu Jul 30 17:20:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

Summary

The key command is ssh [email protected] ‘bash -s’ < ./a.sh

You can find detail documents about the linux and ssh here: