自动化数据同步脚本
利用两个脚本完成集群中的所有电脑数据同步,当然也可以选择使用 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 |
最后更新:2017-04-03 16:48:56