Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589765
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2009-04-11 07:45:10

I complete the short series of scripting lessons with a web-loader (that I have used in my day to day work), which utilizes expect for controlling multiple computers, each running a wget script for loading a specific site. You should first read my articles on expect and wget before this one, for the background details.

To simplify the application, I have created two scripts. One script, named "run_agent" is used to login to a slave computer and run the wget loading script (the exact script from my previous column).  A second script, named "run_all_agents" simply runs the first in a loop, one for each slave computer.

In a top-down fashion, assuming we have 3 slave computers, named loader1, loader2 and loader3, the script would look like this:

run_all_agents

 # ‬This script gets one parameter‭, ‬the number of processes to fork on each slave
if‭ [[ $# < ‬1‭ ]]; ‬then‭  # ‬check the number of command line arguments
‭        ‬echo‭ "‬Usage‭: ‬run_all_agents‭ #‬processes(eg‭. ‬1000‭)"‬
‭        ‬exit
fi

start‭=‬`date‭ +%‬H‭:%‬M‭:%‬S`
printf‭ "%‬s‭ " $‬1‭ $‬start
# Slave names are hard-coded for simplicity
‭./‬run_agent loader1‭ $‬1‭ > /‬dev/null‭ ‬&
‭./‬run_agent loader2‭ $‬1‭ > /‬dev/null‭ ‬&
‭./‬run_agent loader3‭ $‬1‭ > /‬dev/null‭ ‬&
wait
end‭=‬`date‭ +%‬H‭:%‬M‭:%‬S`
printf‭ "%‬s‭ " $‬end
echo

The script "run_agent" assumes that the wget loading script is in a fixed location on all slaves ( here /root/Wget/Scripts) Also, that they all have the same ssh login and password, hard-coded here as "root" and "12345".

run_agent

 #!/usr/bin/expect 
if {$argc<2 } { 
   send_user "usage: $argv0 hostname_of_loader no_of_processes\n" 
   exit 
} 
log_user 1 
set loader [lindex $argv 0] 
set nprocs [lindex $argv 1] 
set timeout ­1 
spawn ssh root@$loader 
expect "word:" 
send 12345\r 
expect "#" 
send "cd /root/Wget/Scripts\r" 
expect "#" 
send "bash ./wload $nprocs\r" 
expect "#" { 
    set Output $expect_out(buffer) 
    send_user $Output 
} 
exit

So, here we have create a scalable loader in less that 100 lines of code by taking advantage of the wonderful Unix philosophy of easy gluing together of multiple dedicated and efficient facilities, as opposed to writing a single monolithic program.

阅读(696) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~