1. 程式人生 > >批量遠程執行命令

批量遠程執行命令

expect bash

批量遠程執行命令

在一臺機器上遠程到多臺機器上執行多條命令,怎麽實現呢?

寫一個登錄到多臺機器並執行命令的腳本文件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 $user@$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

[root@localhost 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
[root@apenglinux-002 ~]# 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

[root@apenglinux-002 ~]# spawn ssh [email protected]
[email protected]‘s password: 
Last login: Sun Mar 18 16:47:15 2018 from 192.168.221.10
[root@apenglinux-002 ~]# 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

批量遠程執行命令