centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
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脚本执行方式 第三十八节课的更多相关文章
- Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行
一运行告警系统 创建一个任务计划crontab -e 每一分钟都执行一次 调试时把主脚本里边log先注释掉 再次执行 没有发现502文件说明执行成功了,每日有错误,本机IP 负载不高 二.分发系统-e ...
- expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令
自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...
- expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行命令
7月20日任务 20.31 expect脚本同步文件20.32 expect脚本指定host和要同步的文件20.33 构建文件分发系统20.34 批量远程执行命令扩展:shell多线程 http:// ...
- 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 ...
- centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课
centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobacku ...
- 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 ...
- 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 第三十七 ...
- centos shell 编程-通过端口号kill对应的进程
centos shell 编程-通过端口号kill对应的进程 方式一.端口固定 ------------------killportprocess.sh fSum() { pid=$(lsof ...
- centos tomcat/resin安装配置 卸载系统自带的java tomcat安装配置 安装JDK resin安装配置 第二十八节课
centos tomcat/resin安装配置 卸载系统自带的java tomcat安装配置 安装JDK resin安装配置 第二十八节课 tomcat和java都不需要编译 tom ...
随机推荐
- openstack中nova组件Hypervisors、Floating_ips的全部python API 汇总
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- 使用 MVVMLight 绑定数据
如果你还不知道如何在VS中加入MVVMLight的引用,那么建议你先翻阅这篇文章:在VS中安装/使用 MVVMLight 这篇文章主要是介绍如何使用MVVMLight来绑定数据到界面中(View),以 ...
- Codeforces Round #313 D. Equivalent Strings(DFS)
D. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- day27<反射&JDK5新特性>
反射(类的加载概述和加载时机) 反射(类加载器的概述和分类) 反射(反射概述) 反射(Class.forName()读取配置文件举例) 反射(通过反射获取带参构造方法并使用) 反射(通过反射获取成员变 ...
- Intel S5000VSA(SAS)主板设置RAID 步骤【转】
Intel S5000VSA(SAS)主板设置RAID 步骤 我近日亲自安 装了一台服务器,用的是intel S5000VSA 4DIMM主板,因为在安装过程中没有注意到一些细节,所以在安装时碰到了一 ...
- C++11新特性之五——可变参数模板
有些时候,我们定义一个函数,可能这个函数需要支持可变长参数,也就是说调用者可以传入任意个数的参数.比如C函数printf(). 我们可以这么调用. printf(); 那么这个函数是怎么实现的呢?其实 ...
- 原生JS,实现图片可拖拽,并且移动四个角和四条变能够自由变换图片大小
网上搜的资料,源码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- Android之ListView分页数据加载
1.效果如下: 实例如下: 上图的添加数据按钮可以换成一个进度条 因为没有数据所以我加了一个按钮添加到数据库用于测试:一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户: 点击加载按 ...
- (转)从程序员到CTO
好好努力吧,向优秀的人看齐.文章来自http://blog.csdn.net/smarttony/article/details/6697617
- ios 给键盘上面加上“完成”
#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController<UITextFieldDelega ...