免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 无风之谷
打印 上一主题 下一主题

[其他] 讨论:如何同步上百/千台服务器的某一个配置文件?(获奖名单已公布-2012-9-6) [复制链接]

论坛徽章:
0
81 [报告]
发表于 2012-07-10 17:55 |只看该作者
回复 80# busyant


欢迎分享~!

论坛徽章:
0
82 [报告]
发表于 2012-07-10 18:03 |只看该作者
如果单纯考虑这个命题: ”如何同步上百/千台服务器的某一个配置文件?“

有两种方式:
推 和 拉

推,就用脚本,scp,rsync 都OK,多线程的脚本最好,可以保证及时性。而且因为是主动推送,安全性上面可以不那么严格
拉,适用于不怎么紧急的情况,定期检查中心服务器。
    http方式会增加中心服务器的负载,因为要考虑安全性,必须https+authentication,但是如果crontab里面如果加个random<15分钟的延时,应该问题不大。
    svn 和 http 方式类似,都需要考虑安全性。
    rsync 配合ssh,或者scp 也都可以实现。

论坛徽章:
0
83 [报告]
发表于 2012-07-10 18:31 |只看该作者
回复 80# busyant


发的帖子少,是因为别的问题太复杂,正好这个问题,前一段时间遇到过一次。
本来打算cf,搞配置人员的也一直用cf,但是用cf的话,太慢(前提是我不会用cf,不知道怎么提升效率)
因为我的这个需求在修改snmp时候,监控的机器中存取的snmp信息也要同步修改,并且修改完之后,所用时间不能大于一分钟。
否则在两个监控周期范围内会出现n多的报警。
只好写脚本来实现了。







   

论坛徽章:
0
84 [报告]
发表于 2012-07-11 00:17 |只看该作者
自己用c写个,

论坛徽章:
0
85 [报告]
发表于 2012-07-11 07:15 |只看该作者
方法还蛮多的,学习了

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
86 [报告]
发表于 2012-07-11 11:08 |只看该作者
看了大家的讨论了解了很多的工具,发现自己很是孤陋寡闻。为了奖品,为了讨论,整理了早先自己写的一个 expect 脚本工具到一个文档上,请笑纳哈:http://blog.chinaunix.net/uid-20583518-id-3270152.html

论坛徽章:
0
87 [报告]
发表于 2012-07-11 12:20 |只看该作者
围观等待解决方案,

论坛徽章:
0
88 [报告]
发表于 2012-07-11 14:21 |只看该作者
回复 86# seesea2517


    我建议你能直接贴出来,不要跳转到博客。这样大家会更喜欢

论坛徽章:
1
2015七夕节徽章
日期:2015-08-21 17:58:43
89 [报告]
发表于 2012-07-11 16:34 |只看该作者
fabric 也可以,不过不支持多线程

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
90 [报告]
发表于 2012-07-11 20:23 |只看该作者
本帖最后由 seesea2517 于 2012-07-11 20:24 编辑

这不格式不好控制嘛,再说整理成一个文档,也方便多年以后检索,总是比在一个回复中找到内容来的容易些。

果然格式很难弄,找到一个取巧的办法,用代码格式,原样过来,哈哈。

回复 88# 无风之谷
  1. 前言
  2. 为了参与[url]http://bbs.chinaunix.net/thread-3757201-9-1.html[/url]的这个讨论,整理一下以前写的脚本。现在看来,这个脚本写的很简陋,并发操作也只是分批的控制而不是完全的并行,不过工作起来还算简单稳定。

  3. 概述
  4. 此脚本使用 expect 来对需要交互输入用户名和密码等操作进行自动化,并有对于主机提示自动加入信任列表、操作失败记录日志等功能。

  5. 正文
  6. 这个脚本分三个文件,逐一附录如下:

  7. 1. 主脚本:batch_ssh.sh

  8.    1. #!/bin/bash
  9.    2. # batch_scp.sh
  10.    3. # 作者:亚丹
  11.    4. # 时间:2009-05-27
  12.    5. # seesea2517#gmail*com
  13.    6. # [url]http://seesea.blog.chinaunix.net[/url]
  14.    7. # [url]http://blog.csdn.net/nicenight[/url]
  15.    8. #
  16.    9. # 功能:批量无交互scp操作
  17.   10. #
  18.   11. # 配置文件格式:
  19.   12. # IP xxxx ID/Name
  20.   13. # IP xxxx ID/Name
  21.   14. # IP xxxx ID/Name
  22.   15. # IP xxxx ID/Name
  23.   16. # IP xxxx ID/Name
  24.   17.

  25.   18. server_list=$1
  26.   19. cmd=$2
  27.   20. if [ -z "$server_list" ] || [ -z "$cmd" ]
  28.   21. then
  29.   22.     echo "Usage: $0 server_list command"
  30.   23.     exit 1
  31.   24. fi
  32.   25.

  33.   26. if [ ! -f "$server_list" ]
  34.   27. then
  35.   28.     echo -e "File \"$server_list\" not exist!"
  36.   29.     exit 1
  37.   30. fi
  38.   31.

  39.   32. # The result directory
  40.   33. res_dir="result"
  41.   34. if ! [ -d $res_dir ]
  42.   35. then
  43.   36.     mkdir $res_dir
  44.   37. fi
  45.   38.

  46.   39. readonly MAX_THREAD=300 ;# The max number of the sub threads can be forked
  47.   40. readonly OLD_IFS=${IFS} ;# Save the current IFS
  48.   41. IFS=\n' ;# Set the IFS to '\n"
  49.   42.

  50.   43. usr="ssh_user"
  51.   44. port=22
  52.   45. read -sp "Input the password for $usr: " psw
  53.   46.

  54.   47. echo "\nGo!\n"
  55.   48.

  56.   49. (( i = 0 ))
  57.   50.

  58.   51. for line in `cat $server_list`
  59.   52. do
  60.   53.     if [ -z "$line" ]
  61.   54.     then
  62.   55.         continue
  63.   56.     fi
  64.   57.

  65.   58.     echo "$line" | grep '^#' > /dev/null
  66.   59.     if [ $? = 0 ]
  67.   60.     then
  68.   61.         continue
  69.   62.     fi
  70.   63.

  71.   64.     ip=`echo $line | awk '{print $1}'`
  72.   65.     id=`echo $line | awk '{print $3}'`
  73.   66.

  74.           if [ "$ip" == "" ]
  75.           then
  76.               continue
  77.           fi

  78.           ./remote_exec.exp $usr $ip $port $psw $cmd >> "$res_dir/${id}.txt" &

  79.           # If the sub thread numbers upto the MAX_THREAD, then wait for all done
  80.           if (( ++i % $MAX_THREAD == 0 ))
  81.           then
  82.               echo "wait....."
  83.               wait
  84.           fi

  85.           echo -ne "\rProcessing $i."
  86.       done

  87.       IFS=${OLD_IFS}
  88.       wait
  89.       echo "Total $i done."


  90. 2. 主 expect 调用脚本 remote_exec.exp

  91.    1. #!/usr/bin/expect
  92.    2. # remote_exec.exp
  93.    3. # 作者:亚丹
  94.    4. # 时间:2009-05-27
  95.    5. # seesea2517#gmail*com
  96.    6. # [url]http://seesea.blog.chinaunix.net[/url]
  97.    7. # [url]http://blog.csdn.net/nicenight[/url]
  98.    8. #
  99.    9. # 功能:实现无交互远程操作
  100.   10.

  101.   11. source func.exp
  102.   12.

  103.   13. if {$argc < 5 } {
  104.   14.     puts "remote_exec needs 5 parameters."
  105.   15.     exit 1
  106.   16. }
  107.   17.

  108.   18. log_user 0
  109.   19. log_file log.txt
  110.   20.

  111.   21. set in_user [lindex $argv 0]
  112.   22. set in_ip [lindex $argv 1]
  113.   23. set in_port [lindex $argv 2]
  114.   24. set in_psw [lindex $argv 3]
  115.   25. set in_cmd [lindex $argv 4]
  116.   26.

  117.   27. set out_res ""
  118.   28.

  119.   29. set ret [remote_exec $in_user $in_ip $in_port $in_psw $in_cmd out_res]
  120.   30. if {$ret < 0} {
  121.   31.     puts "remote_exec failed (return code: $ret, return str: $out_res)."
  122.   32.     exit 1
  123.   33. }
  124.   34.

  125.   35. puts $out_res


  126. 3. expect 的函数库文件:func.exp,现在看来,也没有再新加过函数……不过这个远大蓝图还是曾经存在过的哈

  127.    1. #!/usr/bin/expect
  128.    2. # func.exp
  129.    3. # 作者:亚丹
  130.    4. # 时间:2009-05-27
  131.    5. # seesea2517#gmail*com
  132.    6. # [url]http://seesea.blog.chinaunix.net[/url]
  133.    7. # [url]http://blog.csdn.net/nicenight[/url]
  134.    8. #
  135.    9. # 功能:基本函数库文件
  136.   10.

  137.   11. # -----------------------------------
  138.   12. # Function:
  139.   13. # Execute the command on the host in_ip
  140.   14. #
  141.   15. # Return:
  142.   16. # If successfully executed, return 0
  143.   17. # otherwise return the error code:
  144.   18. # -1: Connect timeout
  145.   19. # -2: Wrong password
  146.   20. # -3: Wrong password
  147.   21. # -4: Checking password timeout
  148.   22. #
  149.   23. # Input args:
  150.   24. # in_user: Specifys the user to login
  151.   25. # in_ip : Specifys the host for user to login
  152.   26. # in_psw : Specifys the password for user to login to the host
  153.   27. # in_cmd : Specifys the command(s) to execute
  154.   28. #
  155.   29. # Output args:
  156.   30. # out_res: Returns the result if executed successfully
  157.   31. # returns the error description if unsuccessful
  158.   32. # -----------------------------------
  159.   33. proc remote_exec {in_user in_ip in_port in_psw in_cmd out_res} {
  160.   34.     upvar $out_res response
  161.   35.

  162.   36.     # The for loop is used to do the login action
  163.   37.     set timeout 9999
  164.   38.     for {set i 1} {$i < 3} {incr i} {
  165.   39.

  166.   40.         # Last "////" is used as an end symbol
  167.   41.         # spawn -noecho ssh $in_user@$in_ip ls -ogt --time-style=+'%Y%m%d%H%M%S' | grep ^- && echo ////
  168.   42.         spawn -noecho ssh $in_user@$in_ip -p $in_port $in_cmd && echo ////
  169.   43.

  170.   44.         expect {
  171.   45.             -nocase "Password:"
  172.   46.             {
  173.   47.                 send "$in_psw\n"
  174.   48.                 break
  175.   49.             }
  176.   50.

  177.   51.             -nocase "(yes/no)"
  178.   52.             {
  179.   53.                 send "yes\n"
  180.   54.                 continue
  181.   55.             }
  182.   56.

  183.   57.             timeout
  184.   58.             {
  185.   59.                 set response "Can't connect to host $in_ip\n"
  186.   60.                 return -1
  187.   61.             }
  188.   62.         }
  189.   63.     sleep 1
  190.   64.     }
  191.   65.

  192.   66.     expect {
  193.   67.         -nocase "password:"
  194.   68.         {
  195.   69.             set response "Wrong password inputed for $in_user@$in_ip.\n"
  196.   70.             return -2
  197.   71.         }
  198.   72.

  199.   73.         -nocase "denied"
  200.   74.         {
  201.   75.             set response "Wrong password inputed for $in_user@$in_ip.\n"
  202.   76.             return -3
  203.   77.         }
  204.   78.

  205.   79.         timeout
  206.   80.         {
  207.   81.             set response "Timeout while checking password for $in_user@$in_ip.\n"
  208.   82.             return -4
  209.   83.         }
  210.   84.

  211.   85.         -re ".*////"
  212.   86.         {
  213.   87.             set response $expect_out(0,string)
  214.   88.             set response [string trim $response "\r\n /"]
  215.   89.             return 0
  216.   90.         }
  217.   91.     }
  218.   92. }

  219. --------------------------------------------------------------------------------------
  220. 对于常用的 scp 操作,有对上述脚本做了针对性的调整,以更加方便的使用:
  221. 1. 主脚本 batch_scp.sh

  222.    1. #!/bin/bash
  223.    2. # batch_scp.sh
  224.    3. # 作者:亚丹
  225.    4. # 时间:2009-05-27
  226.    5. # seesea2517#gmail*com
  227.    6. # [url]http://seesea.blog.chinaunix.net[/url]
  228.    7. # [url]http://blog.csdn.net/nicenight[/url]
  229.    8. #
  230.    9. # 功能:批量无交互scp操作
  231.   10. #
  232.   11. # 配置文件格式:
  233.   12. # IP xxxx ID/Name
  234.   13. # IP xxxx ID/Name
  235.   14. # IP xxxx ID/Name
  236.   15. # IP xxxx ID/Name
  237.   16. # IP xxxx ID/Name
  238.   17.

  239.   18. # para1 is the server list file
  240.   19. server_list=$1
  241.   20. if [ -z "$server_list" ]
  242.   21. then
  243.   22.     echo "Usage: $0 server_list remote_dir local_dir"
  244.   23.     exit 1
  245.   24. fi
  246.   25.

  247.   26. # If server list file not exists, then exit
  248.   27. if [ ! -f "$server_list" ]
  249.   28. then
  250.   29.     echo -e "File \"$server_list\" not exist!"
  251.   30.     exit 1
  252.   31. fi
  253.   32.

  254.   33. # para2 is the remote directory for scp
  255.   34. # if not inputed, then set to "/"
  256.   35. rdir=$2
  257.   36. if [ -z "$rdir" ]
  258.   37. then
  259.   38.     rdir="/"
  260.   39. fi
  261.   40.

  262.   41. # para3 is the local directory for scp
  263.   42. # if not inputed, then set to "./"
  264.   43. ldir=$3
  265.   44. if [ -z "$ldir" ]
  266.   45. then
  267.   46.     ldir="./"
  268.   47. fi
  269.   48.

  270.   49. # The result directory
  271.   50. res_dir="result"
  272.   51. if ! [ -d $res_dir ]
  273.   52. then
  274.   53.     mkdir $res_dir
  275.   54. fi
  276.   55.

  277.   56. readonly MAX_THREAD=2 ;# The max number of the sub threads can be forked
  278.   57. readonly OLD_IFS=${IFS} ;# Save the current IFS
  279.   58. IFS=\n' ;# Set the IFS to '\n"
  280.   59.

  281.   60. usr="ssh_user"
  282.   61. port=22
  283.   62. read -sp "Input the password for $usr: " psw
  284.   63.

  285.   64. (( i = 0 ))
  286.   65.

  287.   66. for line in `cat $server_list`
  288.   67. do
  289.   68.     if [ -z "$line" ]
  290.   69.     then
  291.   70.         continue
  292.   71.     fi
  293.   72.

  294.   73.     echo "$line" | grep '^#' > /dev/null
  295.   74.     if [ $? = 0 ]
  296.   75.     then
  297.   76.         continue
  298.   77.     fi
  299.   78.

  300.   79.     ip=`echo $line | awk '{print $1}'`
  301.   80.     id=`echo $line | awk '{print $3}'`
  302.   81.    if [ "$ip" == "" ]
  303.           then
  304.               continue
  305.           fi

  306.           # ./remote_exec.exp $usr $ip $port $psw $rdir $ldir >> "$res_dir/${id}.txt" &

  307.           # make a local sub-dir for scp, use the id info
  308.           lsub_dir="$ldir/${id}"
  309.           if [ ! -d "$lsub_dir" ]
  310.           then
  311.               mkdir "$lsub_dir"
  312.           fi

  313.           # download and delete the file for release the space of the disk
  314.           # (./remote_exec.exp $usr $ip $port $psw $rdir "$lsub_dir" >> "$res_dir/${id}.txt"; sz "$lsub_dir/*"; rm -R "$lsub_dir";) &

  315.           # for test, not delete the local sub dir
  316.           (./remote_exec.exp $usr $ip $port $psw $rdir "$lsub_dir" >> "$res_dir/${id}.txt"; sz "$lsub_dir/*";) &

  317.           # If the sub thread numbers upto the MAX_THREAD, then wait for all done
  318.           if (( ++i % $MAX_THREAD == 0 ))
  319.           then
  320.               echo "wait....."
  321.               wait
  322.           fi
  323.       done

  324.       IFS=${OLD_IFS}

  325.       wait

  326.       echo "Total $i done."



  327. 2. remote_exec.exp

  328.    1. #!/usr/bin/expect
  329.    2. # remote_exec.exp
  330.    3. # 作者:亚丹
  331.    4. # 时间:2009-05-27
  332.    5. # seesea2517#gmail*com
  333.    6. # [url]http://seesea.blog.chinaunix.net[/url]
  334.    7. # [url]http://blog.csdn.net/nicenight[/url]
  335.    8. #
  336.    9. # 功能:实现无交互远程操作
  337.   10.

  338.   11. source func.exp
  339.   12.

  340.   13. if {$argc < 6 } {
  341.   14.     puts "remote_exec needs 6 parameters."
  342.   15.     exit 1
  343.   16. }
  344.   17.

  345.   18. log_user 0
  346.   19. log_file log.txt
  347.   20.

  348.   21. set in_user [lindex $argv 0]
  349.   22. set in_ip [lindex $argv 1]
  350.   23. set in_port [lindex $argv 2]
  351.   24. set in_psw [lindex $argv 3]
  352.   25. set in_rdir [lindex $argv 4]
  353.   26. set in_ldir [lindex $argv 5]
  354.   27.

  355.   28. set out_res ""
  356.   29.

  357.   30. set ret [remote_exec $in_user $in_ip $in_port $in_psw $in_rdir $in_ldir out_res]
  358.   31. if {$ret < 0} {
  359.   32.     puts "remote_exec failed (return code: $ret, return str: $out_res)."
  360.   33.     exit 1
  361.   34. }
  362.   35.

  363.   36. puts $out_res


  364. 3. func.exp

  365.    1. #!/usr/bin/expect
  366.    2. # func.exp
  367.    3. # 作者:亚丹
  368.    4. # 时间:2009-05-27
  369.    5. # seesea2517#gmail*com
  370.    6. # [url]http://seesea.blog.chinaunix.net[/url]
  371.    7. # [url]http://blog.csdn.net/nicenight[/url]
  372.    8. #
  373.    9. # 功能:基本函数库文件
  374.   10.

  375.   11. # -----------------------------------
  376.   12. # Function:
  377.   13. # Execute the command on the host in_ip
  378.   14. #
  379.   15. # Return:
  380.   16. # If successfully executed, return 0
  381.   17. # otherwise return the error code:
  382.   18. # -1: Connect timeout
  383.   19. # -2: Wrong password
  384.   20. # -3: Wrong password
  385.   21. # -4: Checking password timeout
  386.   22. #
  387.   23. # Input args:
  388.   24. # in_user: Specifys the user to login
  389.   25. # in_ip : Specifys the host for user to login
  390.   26. # in_psw : Specifys the password for user to login to the host
  391.   27. # in_rdir: Specifys the remote directory for scp
  392.   28. # in_ldir: Specifys the local directory for scp
  393.   29. #
  394.   30. # Output args:
  395.   31. # out_res: Returns the result if executed successfully
  396.   32. # returns the error description if unsuccessful
  397.   33. # -----------------------------------
  398.   34. proc remote_exec {in_user in_ip in_port in_psw in_rdir in_ldir out_res} {
  399.   35.     upvar $out_res response
  400.   36.

  401.   37.     # The for loop is used to do the login action
  402.   38.     set timeout 10
  403.   39.

  404.   40.     # Last "////" is used as an end symbol
  405.   41.     # spawn -noecho ssh $in_user@$in_ip ls -ogt --time-style=+'%Y%m%d%H%M%S' | grep ^- && echo ////
  406.   42.     spawn -noecho scp -p $in_port $in_user@$in_ip:$in_rdir $in_ldir && echo ////
  407.   43.

  408.   44.     expect {
  409.   45.         -nocase "Password:"
  410.   46.         {
  411.   47.             send "$in_psw\n"
  412.   48.         }
  413.   49.

  414.   50.         -nocase "(yes/no)"
  415.   51.         {
  416.   52.             send "yes\n"
  417.   53.             exp_continue
  418.   54.         }
  419.   55.

  420.   56.         timeout
  421.   57.         {
  422.   58.             set response "Can't connect to host $in_ip\n"
  423.   59.             return -1
  424.   60.         }
  425.   61.     }
  426.   62.

  427.   63.     expect {
  428.   64.         -nocase "password:"
  429.   65.         {
  430.   66.             set response "Wrong password inputed for $in_user@$in_ip.\n"
  431.   67.             return -2
  432.   68.         }
  433.   69.

  434.   70.         -nocase "denied"
  435.   71.         {
  436.   72.             set response "Wrong password inputed for $in_user@$in_ip.\n"
  437.   73.             send "\003"
  438.   74.             exec kill [exp_pid]
  439.   75.             close
  440.   76.             wait
  441.   77.             return -3
  442.   78.         }
  443.   79.

  444.   80.         timeout
  445.   81.         {
  446.   82.             set response "Timeout while checking password for $in_user@$in_ip.\n"
  447.   83.             return -4
  448.   84.         }
  449.   85.

  450.   86.         -re "\[^/]+////"
  451.   87.         {
  452.   88.             set response $expect_out(0,string)
  453.   89.             set response [string trim $response "\r\n /"]
  454.   90.             return 0
  455.   91.         }
  456.   92.     }
  457.   93. }


复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP