2018.4.26 18周2次课
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018.4.26 18周2次课相关的知识,希望对你有一定的参考价值。
十八周二次课(4月26日)20.31 expect脚本同步文件
20.32 expect脚本指定host和要同步的文件
20.33 构建文件分发系统
20.34 批量远程执行命令
20.31 expect脚本同步文件
自动同步文件
编辑脚本文件:vi 4.expect
#!/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
更改权限:chmod a+x 4.expect
执行脚本文件:./4.expect
[[email protected] /usr/local/sbin]# ./4.expect
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
receiving incremental file list
12.txt
sent 30 bytes received 84 bytes 228.00 bytes/sec
total size is 5 speedup is 0.04
expect: spawn id exp6 not open
while executing
"expect eof"
(file "./4.expect" line 10)
[[email protected] /usr/local/sbin]#
查看12.txt文件:cat /tmp/12.txt
[[email protected] /usr/local/sbin]# cat /tmp/12.txt
1212
如果脚本最后一行被注释掉:# expect eof,那执行脚本时无法继续下去
[[email protected] /usr/local/sbin]# ./4.expect
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
The authenticity of host '192.168.37.100 (192.168.37.100)' can't be established.
ECDSA key fingerprint is SHA256:O/psyoi564EA1rmbUfUBPXikCFUf6bMTmwfAb8wwi7A.
ECDSA key fingerprint is MD5:47:ce:6b:84:7d:4f:e2:5a:cc:bb:0f:4b:61:be:ca:d6.
Are you sure you want to continue connecting (yes/no)? [[email protected] /usr/local/sbin]#
[[email protected] /usr/local/sbin]#
20.32 expect脚本指定host和要同步的文件
timeout设置超时时间,单位是秒,如果设为timeout -1 意为永不超时
编辑脚本文件vi 3.expect
#!/usr/bin/expect
set user [lindex $argv 0] // $argv,参数数组,使用[lindex $argv n]获取
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
set timeout 5 //在执行的命令行:send "$cm\r"下,设置超时5秒
expect "]*"
send "exit\r"
执行脚本:./3.expect root 192.168.37.100 "vmstat 1"
[[email protected] /usr/local/sbin]# ./3.expect root 192.168.37.100 "vmstat 1"
spawn ssh [email protected]
Last login: Wed Apr 25 11:32:33 2018 from 192.168.37.101
[[email protected] ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 3053360 2076 202428 0 0 2 0 27 26 0 0 100 0 0
0 0 0 3053220 2076 202460 0 0 0 0 110 103 0 0 100 0 0
0 0 0 3053220 2076 202460 0 0 0 0 84 85 0 0 100 0 0
0 0 0 3053220 2076 202460 0 0 0 0 84 86 0 0 100 0 0
0 0 0 3053220 2076 202460 0 0 0 0 96 97 0 0 100 0 0
[[email protected] /usr/local/sbin]#
指定host和要同步的文件
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file [email protected]$host:$file //从本机同步到远程。执行时,变量file要写文件的绝对路径。
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
更改权限:chmod a+x 5.expect
执行脚本:./5.expect 192.168.37.100 "/tmp/12.txt"
[[email protected] /usr/local/sbin]# ./5.expect 192.168.37.100 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt
sending incremental file list
12.txt
sent 79 bytes received 31 bytes 220.00 bytes/sec
total size is 5 speedup is 0.05
expect: spawn id exp6 not open
while executing
"expect eof"
(file "./5.expect" line 10)
20.33 构建文件分发系统
需求背景
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令
rsync -av --files-from=list.txt / [email protected]:/
文件分发系统的实现
rsync.expect 内容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / [email protected]$host:/ //如果对方主机没有相同的路径,那rsync要加上-R,会自动创建级联目录
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" } //如果采用密匙认证的方法登陆,那这行就不需要了,可以注释掉
}
expect eof
ip.list内容
192.168.37.100
127.0.0.1
list.txt内容
/tmp/12.txt
/root/shell/1.sh
/root/111/222/111.txt
还要保证2台主机登陆密码要一样,如果不一样就要挨个定义每一台主机的密码:passwd
[[email protected] ~]# passwd
更改用户 root 的密码 。
新的 密码:
用passwd不是很安全。我们可以用密钥认证的方法
rsync.sh 内容
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
echo $ip
./rsync.expect $ip /tmp/list.txt
done
更改权限:chmod a+x rsync.expect
执行脚本:sh -x rsync.sh
[[email protected] /usr/local/sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.37.100
192.168.37.100
+ ./rsync.expect 192.168.37.100 /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/
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/111.txt
tmp/
sent 169 bytes received 43 bytes 424.00 bytes/sec
total size is 5 speedup is 0.02
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
expect: spawn id exp6 not open
while executing
"expect eof"
(file "./rsync.expect" line 10)
脚本执行中有个报错,/root/shell/1.sh文件不存在
查看01主机上有没有111.txt文件:ls /root/111/222/
[[email protected] ~]# ls /root/111/222/
111.txt
20.34 批量远程执行命令
exe.expect 内容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
exe.sh 内容
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
echo $ip
./exe.expect $ip hostname
done更改权限:chmod a+x exe.expect
执行脚本:sh exe.sh
[[email protected] /usr/local/sbin]# sh exe.sh
192.168.37.100
spawn ssh [email protected]
Last login: Thu Apr 26 10:45:41 2018 from 192.168.37.1
[[email protected] ~]# hostname
aming-01
[[email protected] ~]# 127.0.0.1
spawn ssh [email protected]
[email protected]'s password:
Last failed login: Thu Apr 26 12:41:26 CST 2018 from 127.0.0.1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Thu Apr 26 10:45:46 2018 from 192.168.37.1
[[email protected] ~]# hostname
aming-02
以上是关于2018.4.26 18周2次课的主要内容,如果未能解决你的问题,请参考以下文章