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和要同步的文件、构建文件分发系统、批量远程执行命令的更多相关文章

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

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

  2. python实现批量远程执行命令及批量上传下载文件

    #!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...

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

    centos shell编程4[分发系统] 服务器标准化  mkpasswd 生成密码的工具  expect讲解   expect传递参数   expect自动同步文件  expect指定host和要 ...

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

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

  5. 分发系统介绍、expect脚本远程登录、expect脚本远程执行命令、expect脚本传递参数

    7月19日任务 20.27 分发系统介绍20.28 expect脚本远程登录20.29 expect脚本远程执行命令20.30 expect脚本传递参数 20.27 分发系统介绍 公司业务逐渐扩大时, ...

  6. expect脚本远程登录、远程执行命令和脚本传参简单用法

    expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...

  7. 一键帮你复制多个文件到多个机器——PowerShell小脚本(内附PS远程执行命令问题解析)

    作为一个后台程序猿,经常需要把一堆程序集(DLL)或者应用程序(EXE)复制到多个服务器上,实现程序的代码逻辑更新,用以测试新的功能或改动逻辑.这里给大家介绍一个自己实现的PowerShell脚本,方 ...

  8. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  9. 执行 vue inspect > output.js 报错,无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\vue.ps1,因为在此系统中禁止执行脚本

    无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\vue.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 "get-help ab ...

随机推荐

  1. 纯HTML+JS实现轮播

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  2. [考试反思]0811NOIP模拟测试17:虚无

    (sdfz未参加,也就是一共就51个人) 也不粘具体排名了,只写分数线. []220 []201 []194 [5]181 [10]141 [15]132 [20]122 [25]116 [30]10 ...

  3. 和manacher有关的乱写

    当初学kmp hash的时候被教导manacher非常的鸡肋 今天因为一篇神奇的题解我忍不住颓废了两节课把它学了 思路,代码都比较好懂 虽然它不如各种自动机霸气,唯一的功能貌似就是$O(n)$求出所有 ...

  4. T3hack大部分随机化数据

    1000 2000 1 2 1269 1 3 7707 1 4 3329 4 5 6789 1 6 6691 3 7 -1 1 8 2037 6 9 5427 6 10 5690 4 11 4847 ...

  5. VNC的安装以及使用

    VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UNIX 和  ...

  6. ssm整合的登录

    新建一个web工程,主要结构如下: 数据库创建如下: 控制层的代码FormController 类 package codeRose.controller; import org.springfram ...

  7. linux No module named yum错误的解决办法

    linux No module named yum错误的解决办法 肯定是yum的版本与当前python的版本不一致造成的 <pre>所以修改yum的配置,修改文件: vim /usr/bi ...

  8. JS 原生面经从入门到放弃 篇幅较长,建议收藏

    前言 是时候撸一波 JS 基础啦,撸熟了,银十速拿 offer; 本文不从传统的问答方式梳理,而是从知识维度梳理,以便形成知识网络; 包括函数,数组,对象,数据结构,算法,设计模式和 http. 函数 ...

  9. Linux系统中nc工具那些不为人知的用法

    Linux nc命令用法 参考地址:https://www.cnblogs.com/jjzd/p/6306273.html -g<网关>:设置路由器跃程通信网关,最多设置8个; -G< ...

  10. C#winfrom打开指定的文件

    直接打开指定的文件 System.Diagnostics.Process.Start(v_OpenFilePath); 直接打开目录 string v_OpenFolderPath = @" ...