Executing Commands and Scripts Remotely with ssh

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.

5 thoughts on “Executing Commands and Scripts Remotely with ssh

  1. Your style is unique compared to other folks I ave read stuff from. Thank you for posting when you ave got the opportunity, Guess I will just bookmark this page.

  2. ggwpstore adalah sebuah agen yang terlalu lengkap aman dan terpercaya dii indonesia,
    tidak serupa berasal dari agen – agen yang lainnya, ggwpstore menawarkan kemenangan yang terlampau enteng dan layanan yang terlalu ramah, di
    percaya sebagai aggen pkv games sejaak 2015 sampai kala ini pengguna /
    user terbukti terlampau meningkat dari tahun ke tahun, Artinya
    jadi banyak penggunja spesifik nyaa orang indonesia gunakan jasa sarana
    agen ggwpstore.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.