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

本文详细介绍如何使用expect脚本实现自动化运维操作,包括自动登录远程机器、远程执行命令、文件同步及构建文件分发系统等关键步骤。通过具体示例展示如何批量执行命令和文件分发,提高运维效率。

分发系统介绍

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


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 root@172.16.111.110
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.
root@172.16.111.110'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 $user@192.168.133.132

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 root@172.16.111.110
root@172.16.111.110'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 root@172.16.111.110
root@172.16.111.110'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 root@172.16.111.110
root@172.16.111.110'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 root@172.16.111.110
root@172.16.111.110'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 root@172.16.111.110:/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和要同步的文件

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 root@172.16.111.110:/tmp/12.txt
root@172.16.111.110'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构建文件分发系统

需求背景

对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

实现思路

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

核心命令

rsync -av --files-from=list.txt  /  root@host:/

  • 使用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 / root@172.16.111.110:/
root@172.16.111.110'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 / root@127.0.0.1:/
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.
root@127.0.0.1's password: [root@garytao-01 shell]# 

备注:如果不能保证对方机器有相同的路径就加上R,编辑rsync.expect

注意:做分发系统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 root@172.16.111.110
root@172.16.111.110'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 root@127.0.0.1
root@127.0.0.1'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]# 

转载于:https://my.oschina.net/u/3708153/blog/2995301

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值