一键自动化批量安装httpd服务脚本:
1.创建用户
useradd ceshi
echo 123456|passwd --stdin ceshi
id ceshi
echo "ceshi         ALL=            NOPASSWD:ALL">>/etc/sudoers
visudo -c
su - ceshi
2.创建脚本
脚本1
[ceshi@m01 ~]$ vim fenfa_sshkey.exp
#!/usr/bin/expect 
if { $argc != 2 } {
 send_user "usage: expect fenfa_sshkey.exp file host\n"
 exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "123456"
#spawn scp /etc/hosts root@10.0.0.142:/etc/hosts
#spawn scp -P52113 $file ceshi@$hosts:$dir
spawn ssh-copy-id -i $file "-p 52113 ceshi4@$host"
expect {
        "yes/no"        {send "yes\r";exp_continue}
        "*password"     {send "$password\r"}
}
expect eof
exit -onexit {
 send_user "Ceshi say goodbye to you!\n"
}
#script usage
#example
#expect fenfa_sshkey.exp file host dir
#expect fenfa_sshkey.exp ~/hosts 10.0.0.41:~


脚本2
[ceshi@m01 ~]$vim auto_deploy.sh 
#!/bin/sh
. /etc/init.d/functions
#1.product key pair
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
  if [ $? -eq 0 ];then
     action "create dsa $ip" /bin/true
  else 
     action "create dsa $ip" /bin/false
     exit 1
  fi
#2.dis pub key
for ip in 31 41
do 
  expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 172.16.1.$ip >/dev/null 2>&1
  if [ $? -eq 0 ];then
     action "$ip" /bin/true
  else 
     action "$ip" /bin/false
  fi
done
#3.dis fenfa scripts
for n in 31 41
 do
   scp -P 52113 -rp ~/scripts ceshi@172.16.1.$n:~
 done
#4.install service
for m in 31 41
do
  ssh -t -p 52113 ceshi@172.16.1.$m sudo /bin/bash ~/scripts/install.sh
done


脚本3
mkdir -p ~/scripts   #创建目录
vim ~/scripts/install.sh  #在目录下创建脚本
yum install httpd -y


3.执行脚本2查看结果
[ceshi@m01 ~]$ sh -x auto_deploy.sh
执行过程中的内容
+ echo -n $'\347\241\256\345\256\232'
确定+ '[' color = color ']'
+ echo -en '\033[0;39m'

注:这部分出现确定且颜色为绿色说明执行成功,如果出现失败且颜色为红色说明有错误


客户端查看:(成功)
[ceshi@backup ~]$ rpm -qa httpd
httpd-2.2.15-60.el6.centos.6.x86_64