expect 分发、远程执行命令

  1. 分发系统介绍
  2. expect脚本远程登录
  3. expect脚本远程执行命令
  4. expect脚本传递参数
  5. expect脚本同步文件
  6. expect脚本指定host和要同步的文件
  7. 构建文件分发系统
  8. 批量远程执行命令

1. 分发系统介绍

yum install -y expect 批量的发布代码,远程过去执行命令

2. expect脚本远程登录

#!/usr/bin/expect
set host "192.168.88.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact
#指定变量ip地址
#指定机子密码
#执行远程登录
#如果遇到yes/no则传送yes  \r表示回车,exp_continue表示继续
#遇到password:则传送密码 \r回车
#interact 停留在这个机器上
#expect eof 停留几秒钟然后退出来
#如果不加interact也不加expect eof马上就就会退出来
#报错
[root@localhost sbin]# sh expect.sh 
expect.sh:行4: spawn: 未找到命令
couldn't read file "{": no such file or directory
expect.sh:行6: yes/no: 没有那个文件或目录
expect.sh:行6: exp_continue}: 未找到命令
expect.sh:行7: password:: 未找到命令
expect.sh:行8: 未预期的符号 `}' 附近有语法错误
expect.sh:行8: `}'
#说明
给脚本执行权限
./脚本这样执行,不能用sh 脚本执行

3. expect脚本远程执行命令

set user "root"
set passwd "123456"
spawn ssh $user@192.168.88.132

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
#遇到 ]* 执行命令touch /tmp/12.txt  \r 回车
#遇到 ]* 执行命令echo 1212 > /tmp/12.txt \r 回车
#遇到 ]* 执行命令"exit \r 回车
#实现远程登录并执行命令后退出

4. expect脚本传递参数

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
#先定义变量的参数变量  通过脚本传递参数到达脚本中的功能
执行命令:./expect2.sh root 192.168.88.132 "w;ls;ip add"
#结果显示:
[root@mylinux ~]# w;ls;ip add
 22:24:06 up 52 min,  2 users,  load average: 0.00, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.88.1     21:32   12:38   0.01s  0.01s -bash
root     pts/1    192.168.88.131   22:24    0.00s  0.00s  0.00s w
1  anaconda-ks.cfg  test
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b3:42:cc brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.132/24 brd 192.168.88.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::c68f:5cde:926a:e86f/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::faa7:4194:d604:c32a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@mylinux ~]# [root@localhost sbin]# 

5. expect脚本同步文件

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.88.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
#说明:
#使用rsync命令将远程主机上的/tmp/12.txt文件同步到本地
#expect eof会给你时间操作,如果不加则可能你刚进去就退出来了
#结果:
[root@localhost sbin]# ./expect3.sh 
spawn rsync -av root@192.168.88.132:/tmp/12.txt /tmp/
root@192.168.88.132's password: 
receiving incremental file list
12.txt

sent 43 bytes  received 96 bytes  278.00 bytes/sec
total size is 5  speedup is 0.04

6. expect脚本指定host和要同步的文件

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
#执行:[root@localhost sbin]# ./expect4.sh 192.168.88.132 "/tmp/12.txt"
#结果:
[root@localhost sbin]# ./expect4.sh 192.168.88.132 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt root@192.168.88.132:/tmp/12.txt
root@192.168.88.132's password: 
sending incremental file list

sent 44 bytes  received 12 bytes  112.00 bytes/sec
total size is 5  speedup is 0.09
#同步单个文件

7. 构建文件分发系统

 需求背景
 对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
 实现思路
 首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
 核心命令
 rsync -av --files-from=list.txt  /  root@host:/

rsync.expect 内容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
 /tmp/ip.list内容
192.168.133.132
192.168.133.133
/tmp/list.txt内容
/tmp12.txt
/root/shell/1.txt
rsync.sh 内容
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done
执行:sh rsync.sh

8. 批量远程执行命令

exe.expect 内容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
 exe.sh 内容
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done
/tmp/ip.list内容
192.168.133.132
192.168.133.133

扩展:

shell多线程 http://blog.lishiming.net/?p=448
给你提供一本电子书 链接:http://pan.baidu.com/s/1mg49Taw 密码:yk4b
shell习题做一下 http://www.apelearn.com/study_v2/chapter15.html#shll

转载于:https://my.oschina.net/zenghong133/blog/3064834

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值