使用 expect 命令执行自动分发系统

简介: 一、命令 except 实例详解 1. 介绍 expect 使用场景 expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。

一、命令 except 实例详解

1. 介绍 expect 使用场景

expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。

使用之前先安装 expect 软件

yum install -y expect

2. 自动远程登录,登陆后不退出

#! /usr/bin/expect
set user "root"
set host "192.168.11.102"
set passwd "123456"
spawn ssh  $user@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact

3. 登录后执行命令,然后退出

#!/usr/bin/expect
set user "root"
set host "192.168.11.18"
set passwd "123456"
spawn ssh $user@$host
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"

4. 还可以传递参数

#!/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"

执行:

[root@localhost ~]# ./test.expect root 192.168.11.18 2

5. 自动同步文件

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.11.18:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

6. 指定 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 ~]# ./test.expect 192.168.11.18 /tmp/12.txt

 

二、构建文件分发系统

1. 需求背景

对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

2. 实现思路

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

3. 核心命令

rsync -av --files-from=list.txt  /  root@host:/

4. 文件分发系统的实现

 rsync.expect

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

rsync.sh

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done

5. 自动批量执行脚本命令

cat 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"

cat exe.sh

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done

 

三、扩展 expect 语法介绍

shell脚本需要交互的地方可以使用 Here 文档是实现,但是有些命令却需要用户手动去就交互如passwd、scp。对自动部署免去用户交互很痛苦,expect能很好的解决这类问题。

1. expect的核心是spawn expect send set

spawn:调用要执行的命令
expect:等待命令提示信息的出现,也就是捕捉用户输入的提示:
send:发送需要交互的值,替代了用户手动输入内容
set:设置变量值
interact:执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。
expect eof:这个一定要加,与 spawn 对应表示捕获终端输出信息终止,类似于 if....endif
expect 脚本必须以 interact 或 expect eof 结束,执行自动化任务通常 expect eof 就够了。

2. 设置 timeout

设置 expect 永不超时
set timeout -1

设置 expect 300 秒超时,如果超过 300 没有 expect 内容出现,则推出
set timeout 300

 

相关文章
|
6月前
|
Python
【python脚本】执行过程中触发若干次就停止执行脚本的方式
【python脚本】执行过程中触发若干次就停止执行脚本的方式
|
16天前
|
监控 Linux Shell
Linux脚本的作用是监控IP登录失败次数
Linux脚本的作用是监控IP登录失败次数
9 1
|
3月前
|
监控 Unix Shell
shell定时检查
shell定时检查
38 1
|
监控 Shell 应用服务中间件
使用crontab配合shell脚本定时检测拉起服务
使用crontab配合shell脚本定时检测拉起服务
200 0
|
Shell 网络安全 数据安全/隐私保护
expect - 自动交互脚本
1. expect参数 expect教程中文版 expect说明 2. 启用选项 -c :执行脚本前先执行的命令,可多次使用。 -d :debug模式,可以在运行时输出一些诊断信息,与在脚本开始处使用 exp_internal 1 相似。
1389 0