Often it’s required to execute on a remote server a command or a whole bash script.
Not everyone knows that through ssh
it’s possible to execute this task.
Here’s the ssh syntax:
usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-e escape_char] [-F configfile] [-i identity_file] [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R [bind_address:]port:host:hostport] [-S ctl_path] [-w local_tun[:remote_tun]] [user@]hostname [command]
In the following example , the ls
command is run on the remote server.
ssh remoteuser@remoteserver.com ls
To run a local script on the remote server, it’s required to upload it (through scp
), set it as executable and finally run it.
The related example:
scp myscript.sh remoteuser@remoteserver.com:/remotedir/myscript.sh ssh remoteuser@remoteserver.com "chmod +x /remotedir/myscript.sh" ssh remoteuser@remoteserver.com /remotedir/myscript.sh
Of course it’s required to type the password after each command (or to use a identity key file)
Looking through the ssh
options it’s possible to find many other feature offered by this common command.