expect批量执行命令
时间:2016-06-05 00:27 来源:linux.it.net.cn 作者:IT
今天在网上发现了一个expect批量执行命令的脚本,比我之前的批量修改密码的脚本要简单些,现在共享给大家.
系统:centos
需要的软件包:expect(有网的自己yum安装,没网的在光盘里找吧)
1、定义主机ip
cat ip.txt
192.168.0.192
192.168.0.193
192.168.0.194
192.168.0.195
192.168.0.196
2、定义脚本内容
cat script.sh
mkdir test_directory
3、shell嵌套expect脚本
cat shut.sh
view source
print?
01
#!/bin/bash
02
#Create By : www.rootop.org
03
#Version : 1.0
04
#Data : 2015/05/22
05
#Last Modify: 2015/05/22-m1
06
#Desc : auto ops
07
#Tested : RedHat6/CentOS6 with "expect" package
08
09
passwd='123456'
10
sc=$(cat script.sh)
11
12
cat ip.txt | while read line
13
do
14
15
/usr/bin/expect <<EOF
16
set timeout 30
17
spawn ssh root@$line
18
expect {
19
"yes/no" { send "yes\r";exp_continue }
20
"password:" { send "$passwd\r" }
21
}
22
expect "]# "
23
send "$sc\r"
24
send "exit\r"
25
expect eof
26
EOF
27
28
done
29
exit 0
ps:首先定义密码为passwd变量,把发送的命令定义为sc变量,然后用循环读ip地址定义为line变量.
http://www.rootop.org/pages/3420.html
(责任编辑:IT)
今天在网上发现了一个expect批量执行命令的脚本,比我之前的批量修改密码的脚本要简单些,现在共享给大家. 系统:centos 需要的软件包:expect(有网的自己yum安装,没网的在光盘里找吧) 1、定义主机ip cat ip.txt 192.168.0.192 192.168.0.193 192.168.0.194 192.168.0.195 192.168.0.196 2、定义脚本内容 cat script.sh mkdir test_directory 3、shell嵌套expect脚本 cat shut.sh
view source
ps:首先定义密码为passwd变量,把发送的命令定义为sc变量,然后用循环读ip地址定义为line变量. http://www.rootop.org/pages/3420.html (责任编辑:IT) |