SSH Remote Execute command, multiple command and with interaction

This article shows how to execute remote commands via ssh, but you’ll send the commands from your own shell.

ssh my_server 'ls -l /home/my_home_dir'

This will result in this output:

$ ssh ae2 'ls -lha ~'
total 36K
drwxr-xr-x  2 joris joris 4.0K Jan 23 11:42 .
drwxr-xr-x. 5 root  root  4.0K Jan 23 11:41 ..
-rw-r--r--  1 joris joris   54 Jan 23 11:41 .bash_logout
-rw-r--r--  1 joris joris  507 Jan 23 11:41 .bash_profile
-rw-r--r--  1 joris joris  213 Jan 23 11:41 .bashrc
-rw-------  1 joris joris   51 Jan 23 11:42 .history
-rw-r--r--  1 joris joris  171 Jan 23 11:41 .kshrc
-rw-r--r--  1 joris joris  375 Jan 23 11:41 .profile
-rw-r--r--  1 joris joris  153 Jan 23 11:41 .vimrc

What’s even better, is that you can run multiple commands separated with a semi colon, like this:

ssh my_server 'ls -l /home/my_home_dir;whoami'

And the best trick is this one, user input with an interactive command, sending input and output back and forth!

ssh -t my_server 'vi ~/.bash_profile'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s