分发系统介绍


expect脚本远程执行命令

安装expect

[root@garytao-01 mon]# yum install -y expect
自动远程登录
[root@garytao-01 shell]# vi 1.expect

增加以下脚本内容:

#! /usr/bin/expect
set host "172.16.111.110"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact

这个文件是就保证登录信息的,清空的话,重新远程登录ssh 会有提示/root/.ssh/known_hosts

#加入执行权限
[root@garytao-01 shell]# chmod a+x 1.expect
#成功登录
[root@garytao-01 shell]# ./1.expect
spawn ssh [email protected]
The authenticity of host '172.16.111.110 (172.16.111.110)' can't be established.
ECDSA key fingerprint is 09:6d:70:42:42:9a:12:69:51:9b:ad:e5:73:98:b9:c0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.111.110' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Mon Feb 26 18:22:32 2018 from 172.16.111.100
[root@garytao-02 ~]# 登出
Connection to 172.16.111.110 closed.
[root@garytao-01 shell]#

expect脚本运程传递参数

自动远程登录后,执行命令并退出
[root@garytao-01 shell]# vi 2.expect

增加脚本如下内容:


#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh [email protected]

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"


#增加权限
[root@xietaolinux1 ~]# chmod a+x 2.expect

#机器一执行脚本,远程创建文件,写入文件内容,回车退出
[root@garytao-01 shell]# ./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:23:28 2018 from 172.16.111.100
[root@garytao-02 ~]# touch /tmp/12.txt
[root@garytao-02 ~]# echo 1212 > /tmp/12.txt
[root@garytao-02 ~]# [root@garytao-01 shell]#
[root@garytao-01 shell]#

##重新执行自动登录脚本
[root@garytao-01 shell]# ./1.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:34:42 2018 from 172.16.111.100

#机器二查看远程创建的文件
[root@garytao-02 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 2月  27 10:34 /tmp/12.txt

##查看远程脚本创建的文件内容
[root@garytao-02 ~]# cat /tmp/12.txt
1212
[root@garytao-02 ~]#

expect脚本传递参数

传递参数
[root@garytao-01 shell]# vi 3.expect

增加如下脚本内容:

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
set timeout -1
expect "]*"
send "exit\r"

[root@garytao-01 shell]# chmod a+x 3.expect

[root@garytao-01 shell]# ./3.expect root 172.16.111.110 ls
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:39:46 2018 from 172.16.111.100
[root@garytao-02 ~]# ls
123.txt   1_heard.txt  1.txt  aming       anaconda-ks.cfg.1  shell    zabbix-release-3.2-1.el7.noarch.rpm
123.txt~  1_sorft.txt  2.txt  aminglinux  rsync              yum.log

#支持多条参数
[root@garytao-02 ~]# [root@garytao-01 shell]# ./3.expect root 172.16.111.110 "ls;w;vmstat 1"
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:53:39 2018 from 172.16.111.100
[root@garytao-02 ~]# ls;w;vmstat 1
123.txt   1_heard.txt  1.txt  aming       anaconda-ks.cfg.1  shell    zabbix-release-3.2-1.el7.noarch.rpm
123.txt~  1_sorft.txt  2.txt  aminglinux  rsync              yum.log
 10:56:21 up 6 days,  9:04,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172.16.111.1     一18   16:31m  0.00s  0.00s -bash
root     pts/1    172.16.111.100   10:56    0.00s  0.01s  0.01s w
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 204888    876 239148    0    0     0     1   46   14  0  0 100  0  0
 0  0      0 204904    876 239164    0    0     0    21   55  116  0  0 100  0  0
 0  0      0 204904    876 239164    0    0     0     0   55  113  0  0 100  0  0
 0  0      0 204904    876 239164    0    0     0     0   56  112  0  0 100  0  0
 0  0      0 204904    876 239164    0    0     0     0   56  111  0  0 100  0  0
 0  0      0 204904    876 239164    0    0     0     0   58  110  0  0 100  0  0

#因为vmstat 1 是持续运行的。所以脚本最后的exit 就没有办法执行,只能手动终止

expect脚本同步文件

自动同步文件
[root@garytao-01 shell]# vi 4.expect

把另外一台机器的tmp下文件12.txt同步到本地tmp文件

增加如下脚本内容:

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

[root@garytao-01 shell]# chmod a+x 4.expect

#如果机器上没有安装rsync请使用如下命令安装
[root@garytao-02 ~]# yum -y install rsync

查看同步过程分发系统介绍、expect脚本远程执行命令、远程传递参数、脚本同步文件、脚本指定host和要同步的文件、expect构建文件分发系统、批量远程执行命令-LMLPHP


expect脚本指定host和要同步的文件

set timeout 定义超时时间(单位为 秒) -1 为永远不超时

指定host和要同步的文件
[root@garytao-01 shell]# vi 5.expect

增加如下内容:

#!/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

#file变量定义的文件地址,使用时,必须写绝对路径

把文件同步到远程机器上

[root@garytao-01 shell]# cat 5.expect
#!/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
[root@garytao-01 shell]# chmod a+x 5.expect
[root@garytao-01 shell]# ./5.expect 172.16.111.110 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt
[email protected]'s password:
sending incremental file list

sent 31 bytes  received 12 bytes  28.67 bytes/sec
total size is 5  speedup is 0.12
[root@garytao-01 shell]#


expect构建文件分发系统

需求背景

实现思路

核心命令

  • 使用rsync 的 --files参数,可以实现调用文件里面的列表,进行多个文件远程传输,进而实现文件分发

文件分发系统的实现


## 创建rsync.expect执行脚本
[root@garytao-01 shell]# vi 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

同步的路径,需要保证对方机器也有这个相同的路径,如果没有路径,需要使用 -R 创建路径

## file.list内容,为同步的文件路径列表
[root@garytao-01 shell]# vi /tmp/file.list

增加如下需要同步的文件路径:

/tmp/12.txt
/root/shell/1.sh
/root/111/222/lll.txt

因为实现分发系统,肯定是因为需要分发的机器数量过大,所以,定义好了 文件 的 list 列表文件以后, 还需要配置 ip 的列表文件

## ip.list内容,为需要同步的远程机器IP列表
[root@garytao-01 shell]# vi /tmp/ip.list

172.16.111.110
127.0.0.1

创建一个rsync.sh脚本

[root@garytao-01 shell]# vi rsync.sh

#!/bin/bash
for ip in `cat /tmp/ip.list`
do
    ./rsync.expect $ip /tmp/file.list
done

#加权限执行脚本
[root@garytao-01 shell]# chmod a+x rsync.expect
#执行过程
[root@garytao-01 shell]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ ./rsync.expect 172.16.111.110 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / [email protected]:/
[email protected]'s password:
building file list ... rsync: link_stat "/root/shell/1.sh" failed: No such file or directory (2)
done
root/
root/111/
root/111/222/
root/111/222/lll.txt/
tmp/

sent 130 bytes  received 27 bytes  314.00 bytes/sec
total size is 5  speedup is 0.03
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
+ for ip in '`cat /tmp/ip.list`'
+ ./rsync.expect 127.0.0.1 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / [email protected]:/
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is 89:19:99:8c:63:ff:d9:e6:19:0d:81:03:27:54:49:78.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
[email protected]'s password: [root@garytao-01 shell]#

备注:如果不能保证对方机器有相同的路径就加上R,编辑rsync.expect分发系统介绍、expect脚本远程执行命令、远程传递参数、脚本同步文件、脚本指定host和要同步的文件、expect构建文件分发系统、批量远程执行命令-LMLPHP

注意:做分发系统expect脚本的前提是需要保证机器密码一样,这样会有一个问题就是如果密码泄露的话就会有安全隐患,所以可以做密钥认证增加安全。

[root@garytao-01 shell]# passwd
更改用户 root 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@garytao-01 shell]#

批量远程执行命令

批量执行命令脚本
[root@garytao-01 shell]# vim exe.expect


增加如下脚本内容:

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

[root@garytao-01 shell]# chmod a+x exe.expect

## 定义一个exe的sehll脚本
[root@garytao-01 shell]# vim exe.sh

增加如下脚本内容:

#!/bin/bash
for ip in `cat /tmp/ip.list`
do
    ./exe.expect $ip "hostname"
done

##执行脚本
[root@garytao-01 shell]# sh exe.sh
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 11:21:39 2018 from 172.16.111.100
[root@garytao-02 ~]# hostname
garytao-02
[root@garytao-02 ~]# spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 16:52:17 2018 from 172.16.111.1
[root@garytao-01 ~]# hostname
garytao-01
[root@garytao-01 ~]# [root@garytao-01 shell]#

12-30 08:52