Skip to content

[Linux] Use "ssh" Command to log in the Remote Server and Execute Commands

In Linux system, we often create many automated scripts to help us handle all kinds of work. Today, I need to automatically log in to the remote server on a local host and execute commands on the server.

I thought it would be a little troublesome, but when I search it on Internet, it simply surpassed my imagination. The record is as follows.


ssh into the remote server and execute commands

First, we take a look for the command to log in remote server:

ssh user_name@remote_server_ip


Then if you need to execute a command, you can use the following command to do it (for example: use ls command):

ssh user_name@remote_server_ip "ls"

Output:

My bash has a warning, but only because of the different locale

Is it convenient?

If you need to execute multiple instructions, you can use && to separate the instructions.

By the way, I have a problem in Vim editor:

ssh user_name@remote_server_ip "vim test.py"

Output:

Vim: Warning: Output is not to a terminal 
Vim: Warning: Input is not from a terminal

And the bad thing is that I can't even quit in the VIM editor. After checking on the Internet, I found out that if you need to be able to interact (including htop), you need to add the -t parameter.

ssh -t user_name@remote_server_ip "vim test.py"

Output:

Sure enough, it was normal.


References


Read More

Tags:

Leave a Reply