如何通过 SSH 在远程 Linux 系统上运行命令( 二 )

或者你也可以使用以下格式在远程系统上运行多条命令:
$ ssh daygeek@CentOS7.2daygeek.com << EOFhostnamectlfree -mgrep daygeek /etc/passwdEOF上面命令的输出如下:
Pseudo-terminal will not be allocated because stdin is not a terminal. Static hostname: CentOS7.2daygeek.com Icon name: computer-vm Chassis: vm Machine ID: 002f47b82af248f5be1d67b67e03514c Boot ID: dca9a1ba06374d7d96678f9461752482 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-64 total used free shared buff/cache available Mem: 1838 335 1146 11 355 1314 Swap: 2047 0 2047 daygeek:x:1000:1000:2daygeek:/home/daygeek:/bin/bash7) 如何在远程系统上运行本地 Bash 脚本下面的例子允许用户通过 ssh 在远程 Linux 机器上运行本地 bash 脚本 remote-test.sh 。
创建一个 shell 脚本并执行它 。
$ vi /tmp/remote-test.sh#!/bin/bash#Name: remote-test.sh#-------------------- uptime free -m df -h uname -a hostnamectl上面命令的输出如下:
$ ssh daygeek@CentOS7.2daygeek.com 'bash -s' < /tmp/remote-test.sh 01:17:09 up 22 min, 1 user, load average: 0.00, 0.02, 0.08 total used free shared buff/cache available Mem: 1838 333 1148 11 355 1316 Swap: 2047 0 2047 Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 27G 4.4G 23G 17% / devtmpfs 903M 0 903M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 9.3M 910M 2% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 1014M 179M 836M 18% /boot tmpfs 184M 12K 184M 1% /run/user/42 tmpfs 184M 0 184M 0% /run/user/1000 Linux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux Static hostname: CentOS7.2daygeek.com Icon name: computer-vm Chassis: vm Machine ID: 002f47b82af248f5be1d67b67e03514c Boot ID: dca9a1ba06374d7d96678f9461752482 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-64或者也可以使用管道 。如果你觉得输出不太好看 , 再做点修改让它更优雅些 。
$ vi /tmp/remote-test-1.sh#!/bin/bash#Name: remote-test.sh echo "---------System Uptime--------------------------------------------" uptime echo -e "n" echo "---------Memory Usage---------------------------------------------" free -m echo -e "n" echo "---------Disk Usage-----------------------------------------------" df -h echo -e "n" echo "---------Kernel Version-------------------------------------------" uname -a echo -e "n" echo "---------HostName Info--------------------------------------------" hostnamectl echo "------------------------------------------------------------------"上面脚本的输出如下:
$ cat /tmp/remote-test.sh | ssh daygeek@CentOS7.2daygeek.com Pseudo-terminal will not be allocated because stdin is not a terminal. ---------System Uptime-------------------------------------------- 03:14:09 up 2:19, 1 user, load average: 0.00, 0.01, 0.05 ---------Memory Usage--------------------------------------------- total used free shared buff/cache available Mem: 1838 376 1063 11 398 1253 Swap: 2047 0 2047 ---------Disk Usage----------------------------------------------- Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 27G 4.4G 23G 17% / devtmpfs 903M 0 903M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 9.3M 910M 2% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 1014M 179M 836M 18% /boot tmpfs 184M 12K 184M 1% /run/user/42 tmpfs 184M 0 184M 0% /run/user/1000 tmpfs 184M 0 184M 0% /run/user/0 ---------Kernel Version------------------------------------------- Linux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux ---------HostName Info-------------------------------------------- Static hostname: CentOS7.2daygeek.com Icon name: computer-vm Chassis: vm Machine ID: 002f47b82af248f5be1d67b67e03514c Boot ID: dca9a1ba06374d7d96678f9461752482 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-648) 如何同时在多个远程系统上运行多条指令下面的 bash 脚本允许用户同时在多个远程系统上运行多条指令 。使用简单的 for 循环实现 。
为了实现这个目的 , 你可以尝试 PSSH 命令 或 ClusterShell 命令 或 DSH 命令。
$ vi /tmp/multiple-host.sh for host in CentOS7.2daygeek.com CentOS6.2daygeek.com do ssh daygeek@CentOS7.2daygeek.com${host} "uname -a;uptime;date;w" done上面脚本的输出如下:
$ sh multiple-host.sh Linux CentOS7.2daygeek.com 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 01:33:57 up 39 min, 1 user, load average: 0.07, 0.06, 0.06 Wed Sep 25 01:33:57 CDT 2019 01:33:57 up 39 min, 1 user, load average: 0.07, 0.06, 0.06 USER TTY FROM daygeek@CentOS7.2daygeek.com IDLE JCPU PCPU WHAT daygeek pts/0 192.168.1.6 01:08 23:25 0.06s 0.06s -bash Linux CentOS6.2daygeek.com 2.6.32-754.el6.x86_64 #1 SMP Tue Jun 19 21:26:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 23:33:58 up 39 min, 0 users, load average: 0.00, 0.00, 0.00 Tue Sep 24 23:33:58 MST 2019 23:33:58 up 39 min, 0 users, load average: 0.00, 0.00, 0.00 USER TTY FROM daygeek@CentOS7.2daygeek.com IDLE JCPU PCPU WHAT


推荐阅读