2018-06-07 Linux学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-06-07 Linux学习相关的知识,希望对你有一定的参考价值。
20.31 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
操作过程
[[email protected] sbin]# vim 4.expect
写入上面 自动同步文件 内容
[[email protected] sbin]# chmod a+x 4.expect
[[email protected] sbin]# ./4.expect
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
[email protected]‘s password:
receiving incremental file list
12.txt
sent 30 bytes received 84 bytes 20.73 bytes/sec
total size is 5 speedup is 0.04
20.32 expect脚本指定host和要同步的文件
指定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
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof
操作过程
[[email protected] sbin]# vim 5.expect
写入上面的内容 指定host和要同步的文件
[[email protected] sbin]# chmod a+x 5.expect
[[email protected] sbin]# ./5.expect 192.168.106.165 /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 7.82 bytes/sec
total size is 5 speedup is 0.12
[[email protected] sbin]# ./5.expect 192.168.106.165 "/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 7.82 bytes/sec
total size is 5 speedup is 0.12
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 -av --files-from=$file / [email protected]$host:/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof
ip.list内容
192.168.133.132
192.168.133.133
......
rsync.sh 内容
#!/bin/bash
for ip in cat /tmp/ip.list
do
./rsync.expect $ip /tmp/file.list
done
操作过程
[[email protected] sbin]# vim 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:/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof
[[email protected] sbin]# chmod a+x rsync.expect
[[email protected] sbin]# vim /tmp/file.list
/tmp/12.txt
/root/shell/1.sh
/root/111/222/123.txt
[[email protected] sbin]# vim /tmp/ip.list
192.168.106.165
[[email protected] sbin]# vim rsync.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
./rsync.expect $ip /tmp/file.list
done
[[email protected] sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in ‘`cat /tmp/ip.list`‘
+ ./rsync.expect 192.168.106.165 /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/123.txt
tmp/
tmp/12.txt
sent 221 bytes received 62 bytes 43.54 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]
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 ip.list
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
操作过程
[[email protected] sbin]# vim exe.expect
写入上面 exe.expect 内容
[[email protected] sbin]# chmod a+x exe.expect
[[email protected] sbin]# vim exe.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
[[email protected] sbin]# sh exe.sh
192.168.106.165
spawn ssh [email protected]
[email protected]‘s password:
Last login: Tue Apr 24 17:47:27 2018 from 192.168.106.160
[[email protected] ~]# w;free -m;ls /tmp
17:48:41 up 1:51, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.106.1 15:57 15:13 0.04s 0.04s -bash
root pts/1 192.168.106.160 17:48 1.00s 0.02s 0.01s w
total used free shared buff/cache available
Mem: 1822 159 1460 8 202 1473
Swap: 4095 0 4095
12.txt systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vgauthd.service-QH8NC0
d6z systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vmtoolsd.service-fDRGcr
etc
以上是关于2018-06-07 Linux学习的主要内容,如果未能解决你的问题,请参考以下文章