Linux Shell脚本专栏_批量主机远程执行命令脚本_08
生活随笔
收集整理的這篇文章主要介紹了
Linux Shell脚本专栏_批量主机远程执行命令脚本_08
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 安裝expect
- 2. 腳本制作
- 3. 賦予權限
- 4. 運行腳本
- 5. 直接運行腳本
- 6. vmstat測試
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 ResolutionDependencies Resolved==========================================================================Package Arch Version Repository Size ========================================================================== Installing:expect x86_64 5.45-14.el7_1 base 262 kTransaction Summary ========================================================================== Install 1 PackageTotal 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 transactionInstalling : 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); doUSER=$(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@$IPexpect {\"(yes/no)\" {send \"yes\r\"; exp_continue}\"password:\" {send \"$PASS\r\"; exp_continue}\"$USER@*\" {send \"$COMMAND\r exit\r\"; exp_continue}}"echo "---------Execute Successful! ----------" don3. 賦予權限
chmod +x 8.sh4. 運行腳本
[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 -hexit ------------------- [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 st1 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. vmstatexit ------------------- [root@localhost ~]#總結
以上是生活随笔為你收集整理的Linux Shell脚本专栏_批量主机远程执行命令脚本_08的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux CentOS7最小化安装环境
- 下一篇: Spring Boot2 整合 MyBa