Linux批量自动化管理_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2617 | 回复: 0   主题: Linux批量自动化管理        下一篇 
    本主题由 Administrator 于 2014-9-9 23:09:35 移动
white
注册用户
等级:少校
经验:1327
发帖:305
精华:0
注册:2011-7-21
状态:离线
发送短消息息给white 加好友    发送短消息息给white 发消息
发表于: IP:您无权察看 2014-9-9 15:10:52 | [全部帖] [楼主帖] 楼主

·TCP/IP状态变迁图和TCP三次握... 

    ·cookie 信息跟踪调试 

    ·PIC(与位置无关代码)在u-bo... 

    ·命令行关闭特定服务和调整服... 

    ·《Unix/Linux日志分析与流量... 

    ·Oracle 11G 备份与恢复 块改... 

·Oracle Data Guard LOG_ARCHI...


 ·海量数据迁移之使用shell启用... 

    ·[MySQL] 查询5天之内过生日的... 

    ·Oracle RAC 手动升级 11.2.0.... 

    ·Flex Builder教程如何查看所... 

    ·RI本动态ip服务器租用 买五个... 

    ·fopen拷贝文件 

    ·Struts2入门 

    ·TQ2440之IIS 

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

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

    一、Expect简介 

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

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

    二、Expect安装 

yum install expect -y


 三、Expect使用 

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

    首先定义expect 登陆脚本: 

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

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

#!/bin/sh
#auto exec command
#wugk 20130712
CMD="$*"
for i in `awk '{print $1}' passwd.txt`
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,第二列为密码) 

192.168.1.100 abc_123
192.168.1.101 abc_456


 四、测试脚本 

    直接执行: 

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


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

    五、SCP远程拷贝 

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

    1、login.scp内容如下: 

#!/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.sh脚本内容如下 

#!/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`
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.sh脚本:(其实就是如上几个脚本合成的) 

#!/bin/sh
#auto exec expect shell scripts
#wugk 20130712
[ ! -e /usr/bin/expect ];then
yum install expect -y
#Judge passwd.txt exist
[ ! -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
#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
      ##Auto exec shell scripts
      CMD="$*"
      [ "$1" == "" ];then
      echo ========================================================
echo "Please insert your command ,Example {/bin/sh $0 'mkdir -p /tmp'} ,waiting exit ........... "
      sleep 2
      exit 1
for i in `awk '{print $1}' passwd.txt`
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
      expect ./login.exp $i $j "$CMD"
      done


 七、一键安装expect、scp批量auto_exec.sh脚本:(其实就是如上几个脚本合成的) 

#!/bin/sh
#auto exec expect shell scripts
#wugk 20130712
[ ! -e /usr/bin/expect ];then
yum install expect -y
#Judge passwd.txt exist
[ ! -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
#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
      ##Auto exec shell scripts
      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`
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
      expect ./login.exp $i $j $src_file $des_dir
      done


 如上脚本执行完毕后如下图: 

    kernel 报错l701.exel[16922]:... 

    C语言 如何在一个整型左边补0... 

    python无法爬取阿里巴巴的数据... 

    linux-2.6.28 和linux-2.6.32.... 

    linux su - username -c 命... 

该贴由system转至本版2014-9-9 23:09:35




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论