expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行命令
7月20日任务
20.31 expect脚本同步文件
20.32 expect脚本指定host和要同步的文件
20.33 构建文件分发系统
20.34 批量远程执行命令
扩展:
shell多线程 http://blog.lishiming.net/?p=448
20.31 expect脚本同步文件
使用expect脚本实现在一台机器上把文件同步到另外一台机器上,这里需要用到核心命令rsync,如果是手动方式进行同步,那么还需要单独输入密码,所以没有脚本方式操作方便。
示例:自动同步文件
[root@jimmylinux- sbin]# vi .expect #!/usr/bin/expect
set passwd "***@126.com"
spawn rsync -av root@192.168.52.129:/tmp/.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof 如果不加这条语句,那么还没有开始执行数据传输,就马上结束了,甚至有可能还没有远程登录成功,就已经退出来了,所以脚本里面必须要加这条语句。
执行效果
[root@jimmylinux- sbin]# chmod a+x .expect
[root@jimmylinux- sbin]# ./.expect
spawn rsync -av root@192.168.52.129:/tmp/.txt /tmp/
root@192.168.52.129's password:
receiving incremental file list
.txt sent bytes received bytes 92.67 bytes/sec
total size is speedup is 0.04
[root@jimmylinux- sbin]# cat /tmp/.txt
20.32 expect脚本指定host和要同步的文件
之前的3.expect文件默认是10秒钟超时,当然也是可以增加超时时间甚至可以让永久不超时。
只需要在脚本文件中添加第3行set timeout语句即可
expect "]*"
send "$cm\r"
set timeout 设置超时秒数,如果是-1表示永久不会超时
expect "]*"
send "exit\r"
示例:指定host和要同步的文件,这种方式只适合同步一个文件。
[root@jimmylinux- sbin]# vi .expect #!/usr/bin/expect
set passwd "***@126.com"
set host [lindex $argv ] 第一个变量是主机host(也就是主机IP地址)
set file [lindex $argv ] 第二个变量是要同步的文件
spawn rsync -av $file root@$host:$file 这里是从本机到对方,而且file要写绝对路径。
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
执行效果
[root@jimmylinux- sbin]# chmod a+x .expect
[root@jimmylinux- sbin]# ./.expect 192.168.52.129 "/tmp/12.txt"
spawn rsync -av /tmp/.txt root@192.168.52.129:/tmp/.txt
root@192.168.52.129's password:
sending incremental file list sent bytes received bytes 112.00 bytes/sec
total size is speedup is 0.09
20.33 构建文件分发系统
需求背景:对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路:首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令:rsync -av --files-from=list.txt / root@host:/
文件分发系统的实现
1、编写 rsync.expect
[root@jimmylinux- sbin]# vi rsync.expect #!/usr/bin/expect
set passwd "***@126.com"
set host [lindex $argv ]
set file [lindex $argv ]
spawn rsync -avR --files-from=$file / root@$host:/ 如果不确定对方机器有相同的路径,可以加-avR
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
2、编写文件列表 file.list
[root@jimmylinux- sbin]# vi /tmp/file.list /tmp/.txt
/tmp/.txt
/root/shell/.sh
3、编写IP地址列表文件 ip.list
[root@jimmylinux- sbin]# vi /tmp/ip.list 192.168.52.129
192.168.52.130
4、创建 rsync.sh shell文件
[root@jimmylinux- sbin]# vi rsync.sh #!/bin/bash
for ip in `cat /tmp/ip.list`
do
./rsync.expect $ip /tmp/file.list
done
执行效果
[root@jimmylinux- sbin]# chmod a+x rsync.expect
[root@jimmylinux- sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ ./rsync.expect 192.168.52.129 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.52.129:/
root@192.168.52.129's password:
building file list ... done
root/
root/shell/
root/shell/.sh
tmp/
tmp/.txt sent bytes received bytes 238.67 bytes/sec
total size is speedup is 0.11
+ for ip in '`cat /tmp/ip.list`'
+ ./rsync.expect 192.168.52.130 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.52.130:/
The authenticity of host '192.168.52.130 (192.168.52.130)' can't be established.
ECDSA key fingerprint is SHA256:i7B62YOT5sTvxaFu5nD0ETjwadiNfG/RVEb9F/Eh1Nw.
ECDSA key fingerprint is MD5:b9:6f:1e::0e:5b:bc:::b4:5b:f1:cf:de:b0:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.52.130' (ECDSA) to the list of known hosts.
root@192.168.52.130's password: [root@jimmylinux-001 sbin]#
[root@jimmylinux- sbin]#
在192.168.52.129机器上,可以查看到刚才远程同步的3个文件。
[root@jimmylinux- ~]# ls -l /tmp/ -rw-r--r-- root root 7月 : .txt
-rw-r--r-- root root 7月 : .txt
[root@jimmylinux- ~]# ls -l /root/shell/ -rwxr-xr-x root root 7月 : .sh
20.34 批量远程执行命令
想批量远程执行命令,可以通过2个脚本来实现。
1、创建 exe.expect 脚本
[root@jimmylinux- sbin]# vi exe.expect #!/usr/bin/expect
set host [lindex $argv ]
set passwd "***@126.com"
set cm [lindex $argv ]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
2、添加执行权限
[root@jimmylinux- sbin]# chmod a+x exe.expect
3、创建 exe.sh shell脚本
[root@jimmylinux- sbin]# vi exe.sh #!/bin/bash
for ip in `cat /tmp/ip.list`
do
./exe.expect $ip "hostname"
done
执行效果
[root@jimmylinux- sbin]# sh exe.sh
spawn ssh root@192.168.52.129
root@192.168.52.129's password:
Last login: Sun Jul :: from 192.168.52.1 [root@jimmylinux- ~]# hostname
jimmylinux-
[root@jimmylinux- ~]# spawn ssh root@192.168.52.130
root@192.168.52.130's password:
Last failed login: Sun Jul :: CST from 192.168.52.128 on ssh:notty
There were failed login attempts since the last successful login.
Last login: Sun Jul :: from 192.168.52.1 hostname
[root@jimmylinux- ~]# hostname
[root@jimmylinux- sbin]#
expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行命令的更多相关文章
- expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令
自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
centos shell编程4[分发系统] 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要 ...
- Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行
一运行告警系统 创建一个任务计划crontab -e 每一分钟都执行一次 调试时把主脚本里边log先注释掉 再次执行 没有发现502文件说明执行成功了,每日有错误,本机IP 负载不高 二.分发系统-e ...
- 分发系统介绍、expect脚本远程登录、expect脚本远程执行命令、expect脚本传递参数
7月19日任务 20.27 分发系统介绍20.28 expect脚本远程登录20.29 expect脚本远程执行命令20.30 expect脚本传递参数 20.27 分发系统介绍 公司业务逐渐扩大时, ...
- expect脚本远程登录、远程执行命令和脚本传参简单用法
expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...
- 一键帮你复制多个文件到多个机器——PowerShell小脚本(内附PS远程执行命令问题解析)
作为一个后台程序猿,经常需要把一堆程序集(DLL)或者应用程序(EXE)复制到多个服务器上,实现程序的代码逻辑更新,用以测试新的功能或改动逻辑.这里给大家介绍一个自己实现的PowerShell脚本,方 ...
- Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件
一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...
- 执行 vue inspect > output.js 报错,无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\vue.ps1,因为在此系统中禁止执行脚本
无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\vue.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 "get-help ab ...
随机推荐
- SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列
分享一道今天的面试题:SQL语句实现:数据库中有A B C三列,当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列 第一种:使用case when...then...else ...
- 前端技术之:常见前端UI相关开源项目
Bootstrap https://getbootstrap.com/BootstrapVue provides one of the most comprehensive implementatio ...
- 泛微e-cology OA系统远程代码执行漏洞及其复现
泛微e-cology OA系统远程代码执行漏洞及其复现 2019年9月19日,泛微e-cology OA系统自带BeanShell组件被爆出存在远程代码执行漏洞.攻击者通过调用BeanShell组件中 ...
- Centos6 Tengine开启http2传输协议
1.前言 最近在优化网站的访问速度,为网站开启http2协议,这个协议有什么优点呢?如下: http2是下一代的传输协议,以后都会普遍用它,是一个趋势. http2有多路复用特性,意思是访问一个域名下 ...
- 爬虫之selenium爬取斗鱼主播图片
这是我GitHub上简单的selenium介绍与简单使用:https://github.com/bwyt/spider/tree/master/selenium%E5%9F%BA%E7%A1%80 & ...
- PyCharm安装及使用教程
1 PyCharm下载 PyCharm的下载安装非常简单,可以直接到Jetbrains公司官网下载,具体步骤如下: (1)打开Pycharm官网http://www.jetbrains.com,选择 ...
- Python基础学习(一)之Python的概述与环境安装
Python介绍 Python语言介绍 Python是一门高级的.面向对象的.解释性.脚本语言. 高级语言:贴近开发者,对应底层语言,底层语言贴近机器:java.C#.php .ruby 面向对象对应 ...
- .NET手撸绘制TypeScript类图——下篇
.NET手撸绘制TypeScript类图--下篇 在上篇的文章中,我们介绍了如何使用.NET解析TypeScript,这篇将介绍如何使用代码将类图渲染出来. 注:以防有人错过了,上篇链接如下:http ...
- PHP获取图片每个像素点
PHP获取图片每个像素点<pre> $i = imagecreatefromjpeg("test.jpg"); //图片路径 for ($x = 0; $x < ...
- Linux跨网段通信小实验
一.实验场景. 实验准备,Linux主机4台.分别是主机A,路由主机R1,路由主机R2,主机 C,主机A的ip是192.168.56.66/24,且只有一块网卡eth0:路由主机R1有两块网卡eth0 ...