码迷,mamicode.com
首页 > 其他好文 > 详细

批量远程执行命令

时间:2018-03-18 17:45:16      阅读:159      评论:0      收藏:0      [点我收藏+]

标签: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 root@192.168.221.20
root@192.168.221.20‘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 root@192.168.221.30
root@192.168.221.30‘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

批量远程执行命令

标签:expect bash

原文地址:http://blog.51cto.com/13480443/2088205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!