加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

7月21日

发布时间:2020-12-14 03:20:22 所属栏目:大数据 来源:网络整理
导读:20.31 expect脚本同步文件 expect 脚本同步文件 自动同步文件 : ? #!/usr/bin/expect set passwd "123456" spawn rsync -av [email?protected]:/tmp/12.txt /tmp/? expect { "yes/no" { send "yesr"} "password:" { send "$passwdr" } }? expect eof ? 20
20.31 expect脚本同步文件


expect脚本同步文件

自动同步文件

?

#!/usr/bin/expect

set passwd "123456"

spawn rsync -av [email?protected]:/tmp/12.txt /tmp/?

expect {

"yes/no" { send "yesr"}

"password:" { send "$passwdr" }

}?

expect eof

?

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

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

指定host和要同步的文件:

?

#!/usr/bin/expect?

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av $file [email?protected]$host:$file?

expect eof



expect构建文件分发系统

需求背景

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

实现思路

核心命令
文件分发系统的实现
##?创建rsync.expect执行脚本?[[email?protected]?shell]#?vi?rsync.expect增加如下脚本内容:#!/usr/bin/expectset?passwd?"123456"set?host?[lindex?$argv?0]
set?file?[lindex?$argv?1]
spawn?rsync?-av?--files-from=$file?/?[email?protected]$host:/expect?{"yes/no"?{?send?"yesr"}"password:"?{?send?"$passwdr"?}
}
expect?eof##?file.list内容,为同步的文件路径列表[[email?protected]?shell]#?vi?/tmp/file.list增加如下需要同步的文件路径:

/tmp/12.txt
/root/shell/1.sh
/root/111/222/lll.txt##?ip.list内容,为需要同步的远程机器IP列表[[email?protected]?shell]#?vi?/tmp/ip.list172.16.111.110127.0.0.1##创建一个rsync.sh脚本[[email?protected]?shell]#?vi?rsync.sh#!/bin/bashfor?ip?in?`cat?/tmp/ip.list`do
????./rsync.expect?$ip?/tmp/file.list
done##加权限执行脚本[[email?protected]?shell]#?chmod?a+x?rsync.expect?[[email?protected]?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)??yesWarning:?Permanently?added?'127.0.0.1'?(ECDSA)?to?the?list?of?known?hosts.
[email?protected]'s?password:?[[email?protected]?shell]#

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

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

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

?

20.34 批量远程执行命令

批量远程执行命令

1.批量执行命令脚本
[[email?protected]?shell]#?vim?exe.expect增加如下脚本内容:#!/usr/bin/expectset?host?[lindex?$argv?0]set?passwd?"123456"set?cm?[lindex?$argv?1]
spawn?ssh?roo[email?protected]$hostexpect?{"yes/no"?{?send?"yesr"}"password:"?{?send?"$passwdr"?}
}
expect?"]*"send?"$cmr"expect?"]*"send?"exitr"[[email?protected]?shell]#?chmod?a+x?exe.expect?##?定义一个exe的sehll脚本[[email?protected]?shell]#?vim?exe.sh增加如下脚本内容:#!/bin/bashfor?ip?in?`cat?/tmp/ip.list`do
????./exe.expect?$ip?"hostname"done##执行脚本[[email?protected]?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
[[email?protected]?~]#?hostname
garytao-02
[[email?protected]?~]#?spawn?ssh?[email?protected]
[email?protected]'s?password:?
Last?login:?Tue?Feb?27?16:52:17?2018?from?172.16.111.1?
[[email?protected]?~]#?hostnamegarytao-01
[[email?protected]?~]#?[[email?protected]?shell]#

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读