文章目录

1. 安装expect

[root@localhost ~]# yum install expect
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package expect.x86_64 0:5.45-14.el7_1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================
Package Arch Version Repository Size
==========================================================================
Installing:
expect x86_64 5.45-14.el7_1 base 262 k

Transaction Summary
==========================================================================
Install 1 Package

Total download size: 262 k
Installed size: 566 k
Is this ok [y/d/N]: y
Downloading packages:
expect-5.45-14.el7_1.x86_64.rpm | 262 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : expect-5.45-14.el7_1.x86_64 1/1
Verifying : expect-5.45-14.el7_1.x86_64 1/1

Installed:
expect.x86_64 0:5.45-14.el7_1

Complete!
[root@localhost ~]#

2. 脚本制作

host.info

#格式: ip地址 用户 端口 密码
192.168.43.242 root 22 root
192.168.43.134 root 22 root
192.168.43.228 root 22 root

脚本内容:

#!/bin/bash
COMMAND=$*
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
PASS=$(awk -v ip=$IP 'ip==$1{print $4}' $HOST_INFO)
expect -c "
spawn ssh -p $PORT $USER@$IP
expect {
\"(yes/no)\" {send \"yes\r\"; exp_continue}
\"password:\" {send \"$PASS\r\"; exp_continue}
\"$USER@*\" {send \"$COMMAND\r exit\r\"; exp_continue}
}
"
echo "---------Execute Successful! ----------"
don

3. 赋予权限

chmod +x 8.sh

4. 运行脚本

[root@localhost ~]# ./8.sh df -h
spawn ssh -p 22 root@192.168.43.228
root@192.168.43.228's password:
Last login: Tue Feb 25 10:32:22 2020 from desktop-0shdb7s
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 2.1G 48G 5% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.7M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 146M 869M 15% /boot
/dev/mapper/centos-home 47G 33M 47G 1% /home
tmpfs 182M 0 182M 0% /run/user/0
[root@localhost ~]# exit
logout
Connection to 192.168.43.228 closed.
-------------------
[root@localhost ~]#

5. 直接运行脚本

[root@localhost ~]# bash 8.sh df -h
spawn ssh -p 22 root@192.168.43.228
root@192.168.43.228's password:
Last login: Tue Feb 25 10:36:06 2020 from 192.168.43.134
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 2.1G 48G 5% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.7M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 146M 869M 15% /boot
/dev/mapper/centos-home 47G 33M 47G 1% /home
tmpfs 182M 0 182M 0% /run/user/0
[root@localhost ~]# exit
logout
Connection to 192.168.43.228 closed.
df -h
exit
-------------------
[root@localhost ~]#

6. vmstat测试

[root@localhost ~]# bash 8.sh vmstat
spawn ssh -p 22 root@192.168.43.228
root@192.168.43.228's password:
Last login: Tue Feb 25 10:39:57 2020 from 192.168.43.134
[root@localhost ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1511112 2108 196804 0 0 4 1 15 20 0 0 100 0 0
[root@localhost ~]# exit
logout
Connection to 192.168.43.228 closed.
vmstat
exit
-------------------
[root@localhost ~]#