Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4166006
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: LINUX

2011-05-03 23:52:30

对于运维来说,同时管理多台机器是很辛苦的事情,特别是CDN运维需要上传一个文件到1000台机器的话,靠人工一个个上传非常费劲,为此我写了一个批量scp文件到多台机器上的小程序。
其中用到了expect:
  Expect在这个程序里就是用来帮助自动输入scp的密码,Expect主要用于把需要人工交互的程序变为程序自动化完成,这个对于运维批量部署系统,批量无人值守安装,批量执行命令,批量上传下载
 现代的Shell对程序提供了最小限度的控制(开始,停止,等等),而把交互的特性留给了用户。 这意味着有些程序,你不能非交互的运行,比如说passwd。 有一些程序可以非交互的运行,但在很大程度上丧失了灵活性,比如说fsck。这表明Unix的工具构造逻辑开始出现问题。Expect恰恰填补了其中的一些裂痕,解决了在Unix环境中长期存在着的一些问题。

  Expect使用Tcl作为语言核心。不仅如此,不管程序是交互和还是非交互的,Expect都能运用。


1.multi_scp_upload.sh的源代码
  1. #用来通过scp批量上传文件或者目录到目标机器的指定目录
  2. #配置文件格式:
  3. #ssh_hosts=("1.1.1.1" "2.2.2.2")
  4. #ssh_ports=("22" "22")
  5. #ssh_users=("root" "root")
  6. #ssh_passwords=("12" "23")
  7. #执行:sh multi_scp.sh conf_file_path file target
  8. if [ -z "$3" ]
  9. then
  10. echo "sh multi_scp.sh conf_file_path file target";
  11. exit;
  12. fi
  13. #upload shell script file path
  14. scp_upload=scp_upload.sh
  15. #configure file path
  16. conf_file=$1
  17. #then upload file path
  18. scp_file=$2
  19. #remote host'target file or dir path
  20. scp_target=$3
  21. #判断conf_file配置文件是存在
  22. if [ ! -e "$conf_file" ]
  23. then
  24. echo "$conf_file is not exists";
  25. exit;
  26. fi
  27. #判断scp_file是文件或者目录
  28. if [ ! -e "$scp_file" ] && [ ! -d "$scp_file" ]
  29. then
  30. echo "$scp_file is not exists";
  31. exit;
  32. fi
  33. #read configure file
  34. source $conf_file
  35. for((i=0;i<${#ssh_hosts[@]};i++))
  36. do
  37. #remote ssh host
  38. ssh_host=${ssh_hosts[$i]};
  39. #remote ssh port
  40. ssh_port=${ssh_ports[$i]};
  41. #remote ssh user
  42. ssh_user=${ssh_users[$i]};
  43. #remote ssh password
  44. ssh_password=${ssh_passwords[$i]};
  45. echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) start"
  46. #scp file or dir
  47. /usr/bin/expect scp_upload.sh "$ssh_host" "$ssh_port" "$ssh_user" "$ssh_password" "$scp_file" "$scp_target"
  48. echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) end"
  49. echo ""
  50. done
2.scp_upload.sh的源代码
  1. #!/usr/bin/expect
  2. #host
  3. set scphost "[lindex $argv 0]"
  4. #ssh端口
  5. set port "[lindex $argv 1]"
  6. #ssh用户名
  7. set scpuser "[lindex $argv 2]"
  8. #ssh密码
  9. set scppw "[lindex $argv 3]"
  10. #要上传的文件名或者目录
  11. set file "[lindex $argv 4]"
  12. #要上传到远程机器的文件名或者目录
  13. set target "[lindex $argv 5]"
  14. spawn scp -r -P $port $file $scpuser@$scphost:$target
  15. #设置超时时间,防止远程机器防火墙没有开,而挂起
  16. set timeout 30
  17. expect {
  18. #respose: "root@1.2.3.4's password:",自动输入密码
  19. "*password*" {
  20. set timeout 30
  21. send "$scppw\r"
  22. }
  23. #the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes"
  24. "*yes*" {
  25. set timeout 30
  26. send "yes\r"
  27. set timeout 30
  28. expect "*password*"
  29. set timeout 30
  30. send "$scppw\r"
  31. }
  32. busy {send_user "\n";exit;}
  33. failed {send_user "\n";exit;}
  34. timeout {send_user "\n";exit;}
  35. }
  36. #Permission denied not try again,回报出错信息
  37. expect {
  38. "*denied*" {
  39. send_user "\n"
  40. exit
  41. }
  42. "*No such file*" {
  43. send_user "\n"
  44. exit
  45. }
  46. busy {send_user "\n";exit;}
  47. failed {send_user "\n";exit;}
  48. timeout {send_user "\n";exit;}
  49. }
  50. exit
3.配置文件格式scp.conf
  1. ssh_hosts=("1.1.1.1" "2.2.2.2")
  2. ssh_ports=("22" "22")
  3. ssh_users=("root" "root")
  4. ssh_passwords=("abc" "efg")
4.运行代码
找一台机器可以和要上传的机器联通,安装好expact
把scp_upload.sh,multi_scp_upload.sh,scp.conf放到同一个目录下,运行multi_scp_upload.sh即可
5.运行效果

《scp批量从多台机器下载文件到本地》:
大家多多支持,下周我会再发《scp批量在多台机器上执行命令》
阅读(12141) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

jiannma2011-05-07 09:37:57

good