Detailed explanation of SSH Service -- local command calling remote host

Posted by -Karl- on Wed, 12 Jan 2022 19:42:34 +0100

Detailed explanation of SSH service (IV) -- local calling command of remote host

  • The SSH server runs on ubuntu 18.04.1
  • SSH client in Windows10

This section mainly explains that ssh calls commands on the remote host locally without logging in to the remote host shell

Application scenario:

  • In the actual shell script writing process, you need to execute a command remotely through ssh, such as creating a new directory, etc

Implementation method:

ssh username@hostname command

SSH sends a command

Send the following command through Windows powershell

ssh tyustli@192.168.10.22 "ls"

Return result of command

cross-tool
Desktop
src
temp
test.c

Create a new directory

ssh tyustli@192.168.10.22 "mkdir tyustli;ls"

Return result of command

cross-tool
Desktop
src
temp
test.c
tyustli

You can see that ssh returns the directory of the remote host by sending a command, but it is still under the powershell of windows

SSH sends multiple commands

ssh tyustli@192.168.10.22 "ls;pwd"

If you are sending multiple commands, the commands are separated by semicolons

SSH executes remote commands interactively

By specifying the - t parameter in the ssh command, you can interactively execute the remote command. In short, the executed command needs to stay in the shell of the remote host and wait for user interaction, that is, the interaction between remote shells,

By using the - t parameter, ssh keeps logging in to the remote host shell until the interactive command exits

top command without - t parameter:

tyustli@172.16.19.129 top
TERM environment variable not set.

top command using the - t parameter

ssh -t tyustli@172.16.19.129 top

Execution results:

top - 00:43:30 up 2 days,  2:45,  2 users,  load average: 0.00, 0.02, 0.00
Tasks: 379 total,   1 running, 294 sleeping,   0 stopped,   1 zombie
%Cpu(s):  0.1 us,  0.3 sy,  3.2 ni, 96.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  4002276 total,   815300 free,  1508844 used,  1678132 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  2193032 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 67259 tyustli   20   0   51468   4236   3404 R   6.2  0.1   0:00.01 top
     1 root      20   0  226204   9788   6700 S   0.0  0.2   0:11.33 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.10 kthreadd
     3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_gp
     4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp
     6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H-kb
     9 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
    10 root      20   0       0      0      0 S   0.0  0.0   0:00.22 ksoftirqd/0
    11 root      20   0       0      0      0 I   0.0  0.0   1:51.00 rcu_sched
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.58 migration/0
    13 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/0
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0
    15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1
    16 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/1
    17 root      rt   0       0      0      0 S   0.0  0.0   0:01.03 migration/1
    18 root      20   0       0      0      0 S   0.0  0.0   0:00.32 ksoftirqd/1
    20 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/1:0H-kb
    21 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2
    22 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/2
    23 root      rt   0       0      0      0 S   0.0  0.0   0:01.05 migration/2
    24 root      20   0       0      0      0 S   0.0  0.0   0:00.18 ksoftirqd/2
    26 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/2:0H-kb
    27 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/3
    28 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/3
    29 root      rt   0       0      0      0 S   0.0  0.0   0:01.02 migration/3
    30 root      20   0       0      0      0 S   0.0  0.0   0:00.98 ksoftirqd/3
    32 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/3:0H-kb
    33 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs
    34 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 netns
    35 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_tasks_kthre
    36 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kauditd
    40 root      20   0       0      0      0 S   0.0  0.0   0:00.14 khungtaskd
    41 root      20   0       0      0      0 S   0.0  0.0   0:00.00 oom_reaper
    42 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 writeback
    43 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kcompactd0
    44 root      25   5       0      0      0 S   0.0  0.0   0:00.00 ksmd
    45 root      39  19       0      0      0 S   0.0  0.0   0:00.00 khugepaged
   137 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kintegrityd
   138 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kblockd
   139 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 blkcg_punt_bio
   140 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 tpm_dev_wq
   141 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 ata_sff
   142 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 md

When you issue an interactive command through Ctrl + C, you will be disconnected from the remote host

Through the above test, using the - t option, ssh will remain on the remote shell after issuing the command, waiting for the command execution to complete or waiting for the user to give subsequent commands. When the user sends Ctrl + C to exit the interactive command, he will disconnect from the remote shell and return to the local shell

Reference connection: https://www.cnblogs.com/chuanzhang053/p/8549122.html

Topics: Linux ssh server