1. 程式人生 > >2018.4.26 18周2次課

2018.4.26 18周2次課

Linux學習

十八周二次課(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

[root@aming-02 /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)

[root@aming-02 /usr/local/sbin]#

查看12.txt文件:cat /tmp/12.txt

[root@aming-02 /usr/local/sbin]# cat /tmp/12.txt

1212

如果腳本最後一行被註釋掉:# expect eof,那執行腳本時無法繼續下去

[root@aming-02 /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)? [root@aming-02 /usr/local/sbin]#

[root@aming-02 /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 $user@$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"

[root@aming-02 /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

[root@aming-01 ~]# 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

[root@aming-02 /usr/local/sbin]#

指定host和要同步的文件

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av $file root@$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"

[root@aming-02 /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 / root@host:/

文件分發系統的實現

rsync.expect 內容

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -avR --files-from=$file / root@$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

[root@aming-01 ~]# 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

[root@aming-02 /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/

[root@aming-01 ~]# 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 root@$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

[root@aming-02 /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

[root@aming-01 ~]# hostname

aming-01

[root@aming-01 ~]# 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

[root@aming-02 ~]# hostname

aming-02


2018.4.26 18周2次課