hequn

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
}

 

 

 

分类:

技术点:

相关文章:

  • 2021-10-03
  • 2021-10-21
  • 2021-04-02
  • 2022-12-23
  • 2022-01-08
  • 2022-01-08
  • 2021-08-10
  • 2021-12-01
猜你喜欢
  • 2021-08-16
  • 2022-12-23
  • 2021-12-14
  • 2022-01-17
  • 2021-04-04
  • 2022-01-25
  • 2021-06-05
相关资源
相似解决方案