自动化数据同步脚本

利用两个脚本完成集群中的所有电脑数据同步,当然也可以选择使用 RSYNC 进行同步

目标:避免重复输入不同的主机名, 不同的密码

 

/root/bin/data_ssh.sh

 

#!/bin/bash

server=`grep -E -v  "localhost|^#|^$" /etc/hosts | awk '{print $2}'`

echo -n "what is password: "

read pass

echo -n   "command is: "

read command

for name in $server

do

          /root/bin/data_auto_ssh.sh $name   $pass $command

done

 

 

/root/bin/data_auto_ssh.sh

 

#!/usr/bin/expect -f

set host [lindex $argv 0 ]

set password [lindex $argv 1 ]

set command [lindex $argv 2 ]

set command1 [lindex $argv 3 ]

set command2 [lindex $argv 4 ]

set command3 [lindex $argv 5 ]

set command4 [lindex $argv 6 ]

set command5 [lindex $argv 7 ]

set command6 [lindex $argv 8 ]

set command7 [lindex $argv 9 ]

set command8 [lindex $argv 10 ]

set command9 [lindex $argv 11 ]

 

set timeout 10

spawn   ssh  root@$host $command  $command1 $command2 $command3 $command4 $command5 $command6 $command7  $command8 $command9

expect {

 "*yes/no" { send  "yes\r"; exp_continue}

 "*password:" { send  "$password\r" }

}

expect eof

 

 

用法

[root@test bin]# ./data_ssh.sh

what is password: 你的密码

command is: ifconfig  eth0 | grep Mask | awk '{print $4}'

spawn ssh root@nginx_a ifconfig eth0 |  grep Mask | awk '{print $4}'

root@nginx_a's password:

Mask:255.255.255.0

 

=================================

/root/bin/data_sync.sh

 

server=`grep -E -v  "localhost|^#|^$" /etc/hosts | awk '{print $2}'`

echo -n   "source file is: "

read source

echo $source

echo -n "dest file is: "

read dest

echo $dest

echo -n "what is password: "

read pass

for name in $server

do

          /root/bin/data_auto_scp.sh $name $pass $source $dest

done

 

 

/root/bin/data_auto_scp.sh

 

#!/usr/bin/expect -f

set host [lindex $argv 0 ]

set password [lindex $argv 1 ]

set source [lindex $argv 2 ]

set dest [lindex $argv 3 ]

set timeout 10

spawn scp -r $source  root@$host:$dest

expect {

 "*yes/no" { send  "yes\r"; exp_continue}

 "*password:" { send  "$password\r" }

}

expect eof

 

用法 (缺点,每次只能够传输一个文件,待解决中, 也可以通过自动生成执行脚本方法解决)

 如要传输整个目录,可以填写目录名字,如 /root/bin

[root@test bin]# ./data_sync.sh

source file is: /root/bin/data_auto_ssh.sh

/root/bin/data_auto_ssh.sh

dest file is: /root/bin/

/root/bin/

what is password: 你的密码

spawn scp /root/bin/data_auto_ssh.sh  root@nginx_a:/root/bin/

root@nginx_a's password:

data_auto_ssh.sh 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Terry_Tsang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值