centos shell编程4【分发系统】 服务器标准化  mkpasswd 生成密码的工具  expect讲解   expect传递参数   expect自动同步文件  expect指定host和要同步的文件   expect文件分发系统  expect自动发送密钥脚本  Linux脚本执行方式  第三十八节课

expect:TCL语言非常经典的扩展部分,实现程序的自动交互

服务器标准化:所有程序的路径,所有用户的密码,root密码都是一样的
定期会全部更新root密码,这个叫标准化

http://www.cnblogs.com/MYSQLZOUQI/p/4811790.html
mkpasswd 生成密码的工具,安装 expect包

yum install -y expect

上半节课

服务器标准化
mkpasswd 生成密码的工具
expect讲解
expect传递参数
expect自动同步文件
expect指定host和要同步的文件

下半节课

expect文件分发系统
expect自动发送密钥脚本
Linux脚本执行方式

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

spawn 它主要的功能是给ssh运行进程加个shell,用来传递交互指令。
set .. lindex:从程序输入参数中获取变量值并赋给变量
expect ...send对:expect等待希望出现的匹配串,对于匹配到的串,send发送命令进行执行。
expect eof  等待表示子进程已结束的标示符eof,然后退出。(注:这个等待eof必须要有,如果没有eof,很可能在子进程没有结束前就退出,造成问题。)
interact 执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行

使用expect之前,需要先安装expect:
yum install -y expect

1. 自动远程登录,并执行命令
首先来看一个登录后不退出的脚本:
#! /usr/bin/expect
set host "192.168.11.102"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" } #匹配字符串,可以不用写全,因为可以模拟匹配,比如assword,他找到password是模块匹配assword的
}
interact

vim 1.expect
chmod 700 1.expect #涉及到密码
./1.expect 执行脚本 ,自动登录
logout退出

如果要批量登录,需要每台机器的root或者登录用户的密码必须一样
\r:回车

再来看一个登陆后,执行命令然后退出的脚本:

#!/usr/bin/expect
set user "root"
set passwd "123456"

spawn ssh $user@192.168.11.18

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"

expect "]*":匹配 ]$ ]# ]任意字符

2. 我们还可以传递参数
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set command [lindex $argv 2]

spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$command\r"
expect "]*"
send "exit\r"

传参格式:[lindex $argv 0]
上面脚本定义了三个参数
set user [lindex $argv 0]
set host [lindex $argv 1]
set command [lindex $argv 2]
shell格式:$1

执行:
./2.expect root 192.168.11.18 w
./2.expect root 192.168.11.18 "ls /tmp/11.txt"

传密码
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd [lindex $argv 3]
set command [lindex $argv 2]

spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$command\r"
expect "]*"
send "exit\r"

./2.expect root 192.168.11.18 "ls /tmp/11.txt" pwd123456

3. 自动同步文件
#!/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

因为是非交互,所以要加expect eof

4. 指定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

执行: ./4.expect 192.168.11.18 /tmp/12.txt

第二部分:构建文件分发系统
1. 需求背景
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

2. 实现思路
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
3. 核心命令
rsync -av --files-from=list.txt / root@host:/
4. 文件分发系统的实现
cat 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

cat ip.list
192.168.11.18
192.168.11.19
......

list.txt 里面要写绝对路径 --files-from=list.txt

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

chmod 700 rsync.expect

5. 命令批量执行脚本
cat exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set command [lindex $argv 1]

spawn ssh root@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$command\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

如果复杂的话,用rsync发生shell脚本到服务器,然后执行shell脚本sh xx.sh,完成一些服务器的优化工作

文章

http://blog.chinaunix.net/uid-20465760-id-3064252.html

http://blog.sina.com.cn/s/blog_5140741f01015w3j.html


扩展阅读

自动发送密钥脚本

#跳板机
yum install -y expect
su - steven
------------------------------------------
vi auto_ssh.expect
#!/usr/bin/expect
set timeout
set port [lindex $argv ]
set password [lindex $argv ]
set hostname [lindex $argv ]
spawn ssh-copy-id -i /home/steven/.ssh/id_rsa.pub " -p $port steven@$hostname" #first connect, no public key in ~/.ssh/known_hosts
expect {
"*want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
"*assword:" { send "$password\r" }
} #already has public key in ~/.ssh/known_hosts
expect "*assword:" send "$password\r" expect "* weren't expecting." send "\r" ----------------------------------------
vi ip.list
192.168.1.109
--------------------------------------- vi forkey.sh
#!/bin/bash for ip in `cat /home/steven/ip.list`
do
echo $ip
expect /home/steven/auto_ssh.expect $ip > /tmp/error.log
if [ $? == ]
then
continue
else
break
fi
done
------------------------------------------
chmod auto_ssh.expect
chmod forkey.sh
sh forkey.sh #如果对方服务器没有steven用户的报错
cat error.log
usage: send [args] string
while executing
"send"
invoked from within
"expect "*assword:" send "$password\r""
(file "/home/steven/auto_ssh.expect" line )

Linux脚本执行方式

http://www.jb51.net/article/66824.htm

1、相对路径方式,需先cd到脚本路径下
[root@banking tmp]# cd /tmp
[root@banking tmp]# ./ceshi.sh

2、绝对路径方式
[root@banking tmp]# /tmp/ceshi.sh

3、bash命令调用  使用所用脚本语言 比如 expect xx.expect  python xx.py   bash xx.sh  sh  xxx.sh
[root@banking /]# bash /tmp/ceshi.sh

4、. (空格)  相对或绝对路径方式   
[root@banking /]# . /tmp/ceshi.sh

一般用第三种和第四种
第一、二、三种需要赋予脚本执行权限
第一、二、三种都是开子shell,第四种在当前shell,注意环境变量继承

f

centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课的更多相关文章

  1. Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行

    一运行告警系统 创建一个任务计划crontab -e 每一分钟都执行一次 调试时把主脚本里边log先注释掉 再次执行 没有发现502文件说明执行成功了,每日有错误,本机IP 负载不高 二.分发系统-e ...

  2. expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令

    自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...

  3. expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行命令

    7月20日任务 20.31 expect脚本同步文件20.32 expect脚本指定host和要同步的文件20.33 构建文件分发系统20.34 批量远程执行命令扩展:shell多线程 http:// ...

  4. centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课

    centos shell编程5  LANMP一键安装脚本 lamp  sed  lnmp  变量和字符串比较不能用-eq  cat > /usr/local/apache2/htdocs/ind ...

  5. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...

  6. centos linux系统日常管理复习 CPU物理数逻辑核数,iftop ,iotop ,sar ,ps,netstat ,一网卡多IP,mii-tool 连接,ethtool速率,一个网卡配置多个IP,mii-tool 连接,ethtool速率 ,crontab备份, 第十八节课

    centos linux系统日常管理复习 物理CPU和每颗CPU的逻辑核数,uptime ,w,vmstat,iftop ,iotop ,sar ,ps,netstat ,一个网卡配置多个IP,mii ...

  7. centos shell编程3【告警系统】 没有服务器端和客户端的概念 main.sh mon.conf load.sh 502.sh mail.php mail.sh disk.sh 第三十七节课

    centos shell编程3[告警系统]  没有服务器端和客户端的概念 main.sh mon.conf load.sh 502.sh mail.php mail.sh  disk.sh  第三十七 ...

  8. centos shell 编程-通过端口号kill对应的进程

    centos shell 编程-通过端口号kill对应的进程 方式一.端口固定 ------------------killportprocess.sh fSum() {    pid=$(lsof ...

  9. centos tomcat/resin安装配置 卸载系统自带的java tomcat安装配置 安装JDK resin安装配置 第二十八节课

    centos  tomcat/resin安装配置  卸载系统自带的java  tomcat安装配置  安装JDK   resin安装配置    第二十八节课 tomcat和java都不需要编译 tom ...

随机推荐

  1. PHPCMS v9设置文章的审核功能

    对于新建的站点,如果想设置会员发布的文章必须通过审核后才能发布,则需要以下几步来完成: 1.根据需要自定义管理员角色或选择已有角色. 步骤:设置->管理员设置->角色管理->权限设置 ...

  2. HTTP 错误 404.3 解决

    问题 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决 在开始菜单中找到 Visual Studio 命令提示工具.然后用管理员 ...

  3. Maven配置默认使用的JDK版本

    问题: 创建maven项目的时候,jdk版本是1.7版本,而自己安装的是1.8版本,从而导致无法使用lambda等Java8新特性. 每次右键项目名-maven->update project ...

  4. mybatis由浅入深day01_4.9删除用户_4.10更新用户

    4.9 删除用户 4.9.1 映射文件 4.9.2 代码: 控制台: 4.10 更新用户 4.10.1 映射文件 4.10.2 代码 控制台:

  5. 什么叫做hack

    由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到 ...

  6. php第二例

    参考: http://www.php.cn/code/3645.html 前言 由于navicat在linux平台不能很好的支持, PHP的学习转到windows平台. php IDE: PhpSto ...

  7. java基础---->多个排序的拓展

    根据一些特定的规则,我们可以利用java的反射机制做到很有趣的封装. java的排序封装 一.定义一些comparator AmountComparatorAsc:amount升序 package c ...

  8. 银联支付-产品测试sdk使用流程

    准备工作: 到https://open.unionpay.com/ajweb/help/file/techFile?productId=66下载开发文档和sdk 下载之后进行解压将Java Versi ...

  9. 关于Java中按值传递和按引用传递的问题详解

    写了两个方法,一个是多关键字的快速排序,一个是基于多关键字的基速排序.两个方法的参数列表是一样一样的,但是快速排序正常工作,但是基数排序传出来的参数一点没有改变,苦思冥想了半天也没想通是怎么回事,于是 ...

  10. 为非ajax请求绑定回调函数的方法

    我们都知道jQuery为ajax请求封装了success和error两个回调方法,其实jQuery也实现了为非ajax请求的普通方法也设计了绑定回调函数的方法. 当一个方法需要等待另一个耗时很长的方法 ...