批量远程执行命令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量远程执行命令相关的知识,希望对你有一定的参考价值。
批量远程执行命令在一台机器上远程到多台机器上执行多条命令,怎么实现呢?
写一个登录到多台机器并执行命令的脚本文件remote-exec-command.sh
vim /usr/local/sbin/expect/remote-exec-command.sh
#!/usr/bin/expect
set host [lindex $argv 0]
set command [lindex $argv 1]
set user "root"
set passwd "root"
spawn ssh [email protected]$host
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "$passwd\r"}
}
expect "]*"
send "$command\r"
expect "]*"
expect eof
写一个远程机器的ip列表集合文件
vim /tmp/desip.txt
192.168.221.20
192.168.221.30
写一个调用remote-exec-command.sh这个脚本的文件exec.sh
vim /usr/local/sbin/expect/exec.sh
#!/bin/bash
for ip in `cat /tmp/desip.txt`; do
./remote-exec-command.sh $ip "who;ifconfig"
done
执行exec.sh
[[email protected] expect]# bash exec.sh
spawn ssh [email protected]
[email protected]‘s password:
Last login: Sun Mar 18 16:47:04 2018 from 192.168.221.10
[[email protected] ~]# who;ifconfig
root tty1 2018-03-03 10:46
root pts/0 2018-03-18 13:29 (192.168.221.10)
root pts/1 2018-03-18 10:01 (192.168.221.1)
root pts/2 2018-03-18 16:59 (192.168.221.10)
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.221.20 netmask 255.255.255.0 broadcast 192.168.221.255
inet6 fe80::5d25:422c:6b81:44f6 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:03:2c txqueuelen 1000 (Ethernet)
RX packets 18903 bytes 1403073 (1.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18097 bytes 3892662 (3.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 72 bytes 5776 (5.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 72 bytes 5776 (5.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# spawn ssh [email protected]
[email protected]‘s password:
Last login: Sun Mar 18 16:47:15 2018 from 192.168.221.10
[[email protected] ~]# who;ifconfig
root pts/0 2018-03-18 15:45 (192.168.221.1)
root pts/1 2018-03-18 17:00 (192.168.221.10)
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.221.30 netmask 255.255.255.0 broadcast 192.168.221.255
inet6 fe80::52c5:3f9d:6d2b:445a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:34:2f:3a txqueuelen 1000 (Ethernet)
RX packets 17781 bytes 16618859 (15.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12771 bytes 903704 (882.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 688 bytes 174164 (170.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 688 bytes 174164 (170.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
以上是关于批量远程执行命令的主要内容,如果未能解决你的问题,请参考以下文章
linuxsshpass 离线 rpm 包 与使用 远程执行命令 批量执行命令
GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段
GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段