Linux批量部署工具Expect

简介:

前言*随着IT企业的迅猛发展,Linux运维逐渐趋向于自动化,所谓的自动化运维,常常表现为:从程序打包-代码管理-上传-更新部署-代码回滚等等一系列工作。实现自动化运维后将减去人工的重复工作、避免人工的误操作等等。

目前主流的自动化工具有puppet、Expect、pssh等等,今天我们来研究一下expect如何来实现自动部署和日常管理维护。

一、Expect简介

expect是一种能够按照脚本内容里面设定的方式与交互式程序进行“会话”的程序。根据脚本内容,Expect可以知道程序会提示或反馈什么内容以及什么是正确的应答。它是一种可以提供“分支和嵌套结构”来引导程序流程的解释型脚本语言。

我们熟知的shell编程功能虽然很强大,但是不能实现有交互功能的多机器之前的操作,例如ssh和scp等。而expect可以帮助我们来实现。

二、Expect安装

1
yum  install  expect -y

三、Expect使用

使用Expect批量管理和部署服务器大致分为两个步骤,使用for循环读取服务器IP、密码列表并取值,远程执行命令。如下需求,在两台服务器上执行mkdir /tmp/`date +%Y%m%d`命令,如何实现?

首先定义expect 登陆脚本:

1、login.exp,内容如下(详细的参数就不解释了):

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/expect -f
set  ip [lindex $argv 0 ]
set  passwd  [lindex $argv 1 ]
set  command  [lindex $argv 2]
set  timeout 1
spawn  ssh  root@$ip
expect {
"yes/no"  { send  "yes\r" ;exp_continue }
"password:"  { send  "$passwd\r"  }
}
expect  "*#*"  { send  "$command\r"  }
expect eof

2、创建批量执行脚本auto_exec.sh

1
2
3
4
5
6
7
8
9
#!/bin/sh
#auto exec command
#wugk 20130712
CMD= "$*"
for  i  in  ` awk  '{print $1}'  passwd .txt`
do
     j=` awk  - v  I= "$i"  '{if(I==$1)print $2}'  passwd .txt`
     expect  /data/sh/login .exp  $i  $j   "$CMD"
done

3、建立批量IP、密码文件

cat  passwd.txt内容如下:(第一列为IP,第二列为密码)

1
2
192.168.1.100  abc_123
192.168.1.101  abc_456

四、测试脚本

直接执行:

1
/bin/sh  auto_exec.sh  "mkdir -p /tmp/`date +%Y%m%d`"

然后登陆两台服务器查看是否在/tmp/下生产一个以当前系统日期为名称的目录。

五、SCP远程拷贝

如果需要远程推送文件,重新建立文件login.scp相关参数和auto_exec.sh变量:

1、login.scp内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/expect -f
set  ip [lindex $argv 0 ]
set  passwd  [lindex $argv 1 ]
set  src_file [lindex $argv 2]
set  des_dir [lindex $argv 3]
set  timeout 1
spawn  scp  -r $src_file root@$ip:$des_dir
expect {
"yes/no"  { send  "yes\r" ;exp_continue }
"password:"  { send  "$passwd\r"  }
}
expect  "#*"
expect eof

2、auto_exec_scp.sh脚本内容如下

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
#auto exec command
#wugk 20130712
read  -p  "Please Enter insert Source File or DIR: "  src_file
echo  ======================================================
sleep  1
read  -p  "Please Enter insert Destination DIR: "  des_dir
for  i  in  ` awk  '{print $1}'  passwd .txt`
do
     j=` awk  - v  I= "$i"  '{if(I==$1)print $2}'  passwd .txt`
     expect login. scp   $i  $j $src_file $des_dir
done

密码文件保持不变即可。

看上面的脚本可能有点晕,直接上下面的脚本,拷贝即可使用,一个是用于远程执行执行命令的,一个是用于远程拷贝文件的。

六、一键安装expect、ssh批量执行auto_exec_ssh.sh脚本:(远程执行命令)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh
#auto exec expect shell scripts
#wugk 20130712
if
       [ ! -e  /usr/bin/expect  ]; then
       yum   install  expect -y
fi
#Judge passwd.txt exist
if
       [ ! -e . /passwd .txt ]; then
       echo  -e  "The passwd.txt is not exist......Please touch ./passwd.txt ,Content Example:\n192.168.1.11 passwd1\n192.168.1.12 passwd2"
       sleep  2 && exit  0
fi
#Auto Tuoch login.exp File
cat >login.exp <<EOF
#!/usr/bin/expect -f
set  ip [lindex \$argv 0 ]
set  passwd  [lindex \$argv 1 ]
set  command  [lindex \$argv 2]
set  timeout -1
spawn  ssh  root@\$ip
expect {
"yes/no"  { send  "yes\r" ;exp_continue }
"password:"  { send  "\$passwd\r"  }
}
expect  "*#*"  { send  "\$command\r"  }
expect   "#*"   { send  "exit\r"  }
expect eof
EOF
##Auto exec shell scripts
CMD= "$*"
if
     [  "$1"  ==  ""  ]; then
     echo  ========================================================
     echo  "Please insert your command ,Example {/bin/sh  $0 'mkdir -p /tmp'} ,waiting exit ........... "
     sleep  2
     exit  1
fi
for  i  in  ` awk  '{print $1}'  passwd .txt`
do
     j=` awk  - v  I= "$i"  '{if(I==$1)print $2}'  passwd .txt`
     expect . /login .exp  $i  $j   "$CMD"
done

七、一键安装expect、scp批量拷贝auto_exec_scp.sh脚本:(批量SCP拷贝)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh
#auto exec expect shell scripts
#wugk 20130712
if
       [ ! -e  /usr/bin/expect  ]; then
       yum   install  expect -y
fi
#Judge passwd.txt exist
if
       [ ! -e . /passwd .txt ]; then
       echo  -e  "The passwd.txt is not exist......Please touch ./passwd.txt ,Content Example:\n192.168.1.11 passwd1\n192.168.1.12 passwd2"
       sleep  2 && exit  0
fi
#Auto Tuoch login.exp File
cat >login.exp <<EOF
#!/usr/bin/expect -f
set  ip [lindex \$argv 0]
set  passwd  [lindex \$argv 1]
set  src_file [lindex \$argv 2]
set  des_dir [lindex \$argv 3]
set  timeout -1
spawn  scp  -r \$src_file root@\$ip:\$des_dir
expect {
"yes/no"      { send  "yes\r" ; exp_continue }
"password:"   { send  "\$passwd\r"  }
}
expect  "100%"
expect eof
EOF
##Auto exec shell scripts
if
     [  "$1"  ==  ""  ]; then
     echo  ========================================================
     echo  "Please insert your are command ,Example {/bin/sh  $0 /src  /des } ,waiting exit ........... "
     sleep  2
     exit  1
fi
for  i  in  ` awk  '{print $1}'   passwd .txt`
do
       j=` awk  - v  I= "$i"  '{if(I==$1)print $2}'  passwd .txt`
       expect . /login .exp  $i  $j $1 $2
done

脚本已在真实环境使用,附上两个脚本执行过程图:

172331956.png

172333152.png


本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/1271543


相关文章
|
安全 Linux Shell
四、Linux核心工具:Vim, 文件链接与SSH
要想在Linux世界里游刃有余,光会“走路”还不够,还得配上几样“高级装备”。首先是Vim编辑器,它像一把瑞士军刀,让你能在命令行里高效地修改文件。然后要懂“软硬链接”,软链接像个快捷方式,硬链接则是给文件起了个别名。最后,SSH是你的“传送门”,不仅能让你安全地远程登录服务器,还能用scp轻松传输文件,设置好密钥更能实现免-密登录,极大提升效率。
748 5
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
1160 16
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
561 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
NoSQL 关系型数据库 Linux
ERPNext 搭建教程:Linux 一键部署与维护
ERPNext 是一款开源免费的企业资源计划系统,适用于中小企业信息化管理。基于 Python 和 Frappe 框架开发,支持财务、销售、人力、库存等模块,具备高度可定制性。本文介绍如何通过 Websoft9 在 Linux 下快速部署 ERPNext,并提供环境配置、系统维护等实用建议,适合开发者和企业用户快速上手。
2806 7
ERPNext 搭建教程:Linux 一键部署与维护
|
缓存 监控 Linux
Linux系统性能调优技巧和相关工具
Linux 作为一种应用应展和系统服务的优选操作系统,在处理性能和端到端点评估上持有出色表现。但是,在处理进程或系统处于低效状态时,性能调优就显得十分重要。本文将探讨一些 Linux 系统性能调优的常用技巧,并介绍相关工具
454 1
Linux系统性能调优技巧和相关工具
|
Linux 数据安全/隐私保护 iOS开发
推荐Linux环境下效能优良的双向文件同步工具
综合上述条件,对于Linux环境下的双向文件同步需求,Unison 和 Syncthing 是两个非常出色的选择。它们都有良好的社区支持和文档资源,适用于不同规模的环境,从个人使用到商业部署。Unison 特别适合那些需要手动干预同步过程、需要处理文件冲突解决的场景。而 Syncthing 更加现代化,适合需要自动、实时的数据同步与备份的环境。对于选择哪一个,这将取决于个人的使用场景和具体需求。
1579 16
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
757 32
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
1431 13