或者你也可以使用以下格式在远程系统上运行多条命令:
$ ssh daygeek@CentOS7.2daygeek.com << EOF hostnamectl free -m grep daygeek /etc/passwd EOF
上面命令的输出如下:
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/bash
7) 如何在远程系统上运行本地 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 "------------------------------------------------------------------"
(编辑:ASP站长网)
|