批量在远程执行命令

对于运维来说,同时管理多台机器是很辛苦的事情,特别是CDN运维需要同时重新启动1000台机器的apache的话或者获取所有机器的状态,靠人工一个个上远程机器上去执行非常费劲,为此我写了一个在一台机器上批量在多台机器上执行shell命令的小程序。

这个程序是顺序在各个远程机器上执行命令,并且把远程执行的输出打印出来。

虽然scp命令也可以执行远程命令,但是这个程序还有一个好处就是有超时时间(30秒),当某条命令执行超过30秒后,这个程序会继续执行下一台机器,而远程的机器上的命令还会继续执行完毕。直接使用scp执行远程命令的话必须等命令执行完毕才能退出。

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

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

 
注意:这个小工具一次只能执行一行shell语句,若是要执行多行语句的话,先把这些语句写到一个文件里(例如exe.sh),用我之前写的批量上传工具把exe.sh上传到指定目录(例如/tmp),然后再用这个工具执行"sh /tmp/exe.sh",即可

1.multi_scp_shell.sh

 

 
 
  1. #!/bin/bash  
  2. #author: yifangyou  
  3. #create time:2011-05-17  
  4. #用来通过scp在目标机器批量执行命令  
  5. #配置文件格式:  
  6. #ssh_hosts=("1.1.1.1" "2.2.2.2")  
  7. #ssh_ports=("22" "22") 这个可以缺省,缺省值为22,或者个数比ssh_hosts少时,使用缺省值  
  8. #ssh_users=("root" "root") 这个可以缺省,缺省值为root,,或者个数比ssh_hosts少时,使用缺省值  
  9. #ssh_passwords=("323" "222") 这个可以缺省,缺省的话需要从命令行输入,或者个数比ssh_hosts少时,使用命令行输入  
  10. #执行:sh multi_scp_shell.sh conf_file_path 'cmd' 
  11. if [ -z "$2" ]  
  12. then 
  13. echo "sh multi_scp_shell.sh conf_file_path 'cmd'";  
  14. exit;  
  15. fi  
  16. default_ssh_user="root" 
  17. default_ssh_port="22";  
  18. #upload shell script file path  
  19. scp_upload=scp_upload.sh  
  20. #configure file path  
  21. conf_file=$1  
  22. #shell command  
  23. scp_cmd=$2  
  24. #判断conf_file配置文件是存在  
  25. if [ ! -e "$conf_file" ]  
  26. then 
  27. echo "$conf_file is not exists";  
  28. exit;  
  29. fi  
  30. #read configure file  
  31. source $conf_file  
  32. #若是没有在配置文件里提供密码,则在命令行输入  
  33. if [ "${#ssh_passwords[@]}" = "0" ] || [ "${#ssh_passwords[@]}" -lt "${#ssh_hosts[@]}" ]  
  34. then 
  35. read -p "please input password:" -s default_ssh_password  
  36. fi  
  37. success_hosts="";  
  38. fail_hosts="";  
  39. for((i=0;i<${#ssh_hosts[@]};i++))  
  40. do  
  41. #remote ssh host  
  42. ssh_host=${ssh_hosts[$i]};  
  43. if [ "$ssh_host" != "" ]  
  44. then 
  45. #remote ssh port  
  46. ssh_port=${ssh_ports[$i]};  
  47. if [ "$ssh_port" = "" ]  
  48. then 
  49. ssh_port=$default_ssh_port; #use default value  
  50. fi  
  51. #remote ssh user 
  52. ssh_user=${ssh_users[$i]};  
  53. if [ "$ssh_user" = "" ]  
  54. then 
  55. ssh_user=$default_ssh_user; #use default value  
  56. fi  
  57. #remote ssh password 
  58. ssh_password=${ssh_passwords[$i]};  
  59. if [ "$ssh_password" = "" ]  
  60. then 
  61. ssh_password=$default_ssh_password; #use default value  
  62. fi  
  63. echo "["`date +"%F %T"`"] (scp -r $ssh_user@$ssh_host:$ssh_port exe '$scp_cmd') start" 
  64. #scp file or dir  
  65. echo "######################################$ssh_host output start############################################################" 
  66. /usr/bin/expect scp_shell.sh "$ssh_host" "$ssh_port" "$ssh_user" "$ssh_password" "$scp_cmd" 
  67. if [ "$?" -eq "0" ]  
  68. then 
  69. success_hosts="$success_hosts,$ssh_host" 
  70. else 
  71. fail_hosts="$fail_hosts,$ssh_host" 
  72. fi  
  73. echo "######################################$ssh_host output end############################################################" 
  74. echo "["`date +"%F %T"`"] (scp -r $ssh_user@$ssh_host:$ssh_port exe '$scp_cmd') end" 
  75. echo "" 
  76. else 
  77. echo "ssh_host[$i]=null" 
  78. fi  
  79. done  
  80. echo "success_hosts=[$success_hosts]" 
  81. echo "fail_hosts=[$fail_hosts]" 

2.scp_shell.sh的源代码

 

 
 
  1. #!/usr/bin/expect  
  2. #author: yifangyou  
  3. #create time:2011-05-17  
  4. set scphost "[lindex $argv 0]" 
  5. set port "[lindex $argv 1]" 
  6. set scpuser "[lindex $argv 2]" 
  7. set scppw "[lindex $argv 3]" 
  8. #要执行的shell命令  
  9. set cmd "[lindex $argv 4]" 
  10. spawn ssh -p $port $scpuser@$scphost "$cmd" 
  11. set timeout 30  
  12. expect {  
  13. #respose: "root@1.2.3.4's password:" 
  14. "*password*" {  
  15. set timeout 30  
  16. send "$scppw\r" 
  17. }  
  18. #the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes" 
  19. "*yes*" {  
  20. set timeout 30  
  21. send "yes\r" 
  22. set timeout 30  
  23. expect "*password*" 
  24. set timeout 30  
  25. send "$scppw\r" 
  26. }  
  27. busy {send_user "\n<error:busy>";exit 1;}  
  28. failed {send_user "\n<error:failed>";exit 2;}  
  29. timeout {send_user "\n<error:timeout>";exit 3;}  
  30. }  
  31. #Permission denied not try again  
  32. expect {  
  33. "*denied*" {  
  34. send_user "\n<error:Permission denied>" 
  35. exit 4  
  36. }  
  37. busy {send_user "\n<error:busy>";exit 5;}  
  38. failed {send_user "\n<error:failed>";exit 6;}  
  39. timeout {send_user "\n<error:timeout>";exit 7;}  
  40. }  
  41. exit 0 

3.配置文件格式scp.conf

 

 
 
  1. #ssh_hosts=("1.1.1.1" "2.2.2.2")  
  2. #ssh_ports=("22" "22") #wheen port_num < host_num use default=22,or ssh_ports is undefined use 22 as default value  
  3. #ssh_users=("root" "root") #wheen user_num < host_num use default=root,or ssh_users is undefined use root as default value  
  4. #ssh_passwords=("323" "222") #wheen password_num < host_num use default=input password,or ssh_users is undefined use input password 

4.运行代码
找一台机器可以和要执行命令的机器联通,安装好expect(可以用expect命令测试是否已经安装过了)
把scp_shell.sh,multi_scp_shell.sh,scp.conf放到同一个目录下,运行multi_scp_shell.sh即可
5.运行效果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSH(Secure Shell)是一种网络协议,可以安全地远程登录到其他计算机,并在远程计算机上执行命令或脚本。使用SSH批量登录远程执行脚本可以方便地在多台远程服务器上执行相同或类似的任务。 要实现SSH批量登录远程执行脚本,可以按照以下步骤进行操作: 1. 创建一个包含远程服务器IP地址、用户名和密码的配置文件,如YAML或CSV格式。这些配置文件应包括每台服务器的连接信息,以及要执行的脚本的路径。 2. 使用一个脚本或编程语言(如Python)读取配置文件,并循环遍历每个服务器的连接信息。 3. 对于每个服务器,打开SSH连接,在远程服务器上执行命令或脚本。可以使用SSH客户端程序(如OpenSSH)进行连接,并使用命令行参数传递用户名、密码和远程服务器的IP地址。 4. 执行完需要的任务后,关闭SSH连接。 5. 循环遍历配置文件中的其他服务器,重复上述过程,直到所有服务器上的任务都执行完毕。 在编程语言中,可以使用SSH相关的库和模块来简化连接和执行远程命令的过程,如paramiko库。它提供了一些简便的函数和方法来实现SSH连接,并通过执行命令和获取返回结果来执行远程脚本。 总的来说,SSH批量登录远程执行脚本可以通过编写一个脚本或使用编程语言的SSH库来实现。这样可以提高效率,减少手动操作的时间和工作量,适用于需要在多台远程服务器上执行相同操作或脚本的场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值