linux集群环境下脚本分发文件

集群的管理经常要用到文件或者软件的分发,虽然scp命令可以解决问题,但是当集群节点数较大,分发操作频繁时,手动一条一条地分发显然不是很方便,所以要用脚本来完成。

expect自动交互脚本可以解决访问登录的问题,但是expect用的是tcl语法(tool command language),不是shell语法,所以接收参数的方式和bash脚本的方式不太一样。bash通过$0 ... $n 这种方式,expect则通过[lindex $argv 0]获取参数。具体参考下面的脚本代码。

1.分发文件:

#!/usr/bin/expect -f
set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.93:[lindex $argv 1]
expect "root@192.168.178.93's password:"
send "hequnhequn\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.94:[lindex $argv 1]
expect "root@192.168.178.94's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.95:[lindex $argv 1]
expect "root@192.168.178.95's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.96:[lindex $argv 1]
expect "root@192.168.178.96's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.97:[lindex $argv 1]
expect "root@192.168.178.97's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.98:[lindex $argv 1]
expect "root@192.168.178.98's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.99:[lindex $argv 1]
expect "root@192.168.178.99's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.100:[lindex $argv 1]
expect "root@192.168.178.100's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.101:[lindex $argv 1]
expect "root@192.168.178.101's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.102:[lindex $argv 1]
expect "root@192.168.178.102's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp [lindex $argv 0] root@192.168.178.104:[lindex $argv 1]
expect "root@192.168.178.104's password:"
send "hzdz123\r"
interact

2.分发文件夹:

#!/usr/bin/expect -f
set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.93:[lindex $argv 1]
expect "root@192.168.178.93's password:"
send "hequnhequn\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.94:[lindex $argv 1]
expect "root@192.168.178.94's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.95:[lindex $argv 1]
expect "root@192.168.178.95's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.96:[lindex $argv 1]
expect "root@192.168.178.96's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.97:[lindex $argv 1]
expect "root@192.168.178.97's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.98:[lindex $argv 1]
expect "root@192.168.178.98's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.99:[lindex $argv 1]
expect "root@192.168.178.99's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.100:[lindex $argv 1]
expect "root@192.168.178.100's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.101:[lindex $argv 1]
expect "root@192.168.178.101's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.102:[lindex $argv 1]
expect "root@192.168.178.102's password:"
send "hzdz123\r"
interact

set timeout 30
spawn scp -r [lindex $argv 0] root@192.168.178.104:[lindex $argv 1]
expect "root@192.168.178.104's password:"
send "hzdz123\r"
interact

 3.将分发文件写成循环的形式

#!/usr/bin/expect -f

set i 93
while {$i < 105} {
        if {$i == 103} {
               incr i
               continue
        }
	set timeout 30
	spawn scp [lindex $argv 0] root@192.168.178.$i:[lindex $argv 1]
	expect "root@192.168.178.$i's password:"
        if {$i > 93} {
               send "hzdz123\r"
        }
        if {$i == 93} {
               send "hequnhequn\r"
        }
	interact
        incr i
}

 

 

 

posted on 2014-03-08 21:44  hequn8128  阅读(1061)  评论(0编辑  收藏  举报

导航