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


相关文章
|
7天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
40 2
|
10天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
26 0
|
17天前
|
存储 前端开发 Linux
Linux系统之部署ToDoList任务管理工具
【4月更文挑战第1天】Linux系统之部署ToDoList任务管理工具
61 1
|
18天前
|
存储 传感器 运维
linux系统资源统计工具
【4月更文挑战第1天】Linux系统监控工具如dstat、htop、glances、vmstat、top、iostat、mpstat、sar和atop,用于跟踪CPU、内存、磁盘I/O、网络和进程性能。这些工具提供实时、交互式和历史数据分析,助力管理员优化系统性能和故障排查。例如,dstat是vmstat等工具的增强版,htop提供彩色界面的进程管理,而atop则结合了多种功能并记录历史数据。
27 5
linux系统资源统计工具
|
10天前
|
Linux
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
20 0
|
10天前
|
Linux Shell 虚拟化
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
14 0
|
15天前
|
资源调度 JavaScript 安全
Linux系统之部署web-check网站分析工具
【4月更文挑战第3天】Linux系统之部署web-check网站分析工具
63 9
|
16天前
|
运维 监控 Linux
不是所有的Linux工具都会让人惊叹,但这个绝对让你叫绝
【4月更文挑战第3天】不是所有的Linux工具都会让人惊叹,但这个绝对让你叫绝
29 0
不是所有的Linux工具都会让人惊叹,但这个绝对让你叫绝
|
25天前
|
缓存 Linux iOS开发
【C/C++ 集成内存调试、内存泄漏检测和性能分析的工具 Valgrind 】Linux 下 Valgrind 工具的全面使用指南
【C/C++ 集成内存调试、内存泄漏检测和性能分析的工具 Valgrind 】Linux 下 Valgrind 工具的全面使用指南
62 1
|
28天前
|
安全 Shell Linux
【Shell 命令集合 系统管理 】Linux 终端复用工具 screen命令 使用指南
【Shell 命令集合 系统管理 】Linux 终端复用工具 screen命令 使用指南
33 0