scp批量上传文件到多台机器上(升级版)

简介:

对之前的版本进行升级,增加支持密码从命令行输入方式,增加提示那些机器上传成功或者失败提示

对于运维来说,同时管理多台机器是很辛苦的事情,特别是CDN运维需要上传一个文件到1000台机器的话,靠人工一个个上传非常费劲,为此我写了一个批量scp文件到多台机器上的小程序。

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

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

 
1.multi_scp_upload.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.sh conf_file_path file target  
  11. if [ -z "$3" ]  
  12. then 
  13. echo "sh multi_scp.sh conf_file_path file target";  
  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. #then upload file path  
  23. scp_file=$2  
  24. #remote host'target file or dir path  
  25. scp_target=$3  
  26. #判断conf_file配置文件是存在  
  27. if [ ! -e "$conf_file" ]  
  28. then 
  29. echo "$conf_file is not exists";  
  30. exit;  
  31. fi  
  32. #判断scp_file是文件或者目录  
  33. if [ ! -e "$scp_file" ] && [ ! -d "$scp_file" ]  
  34. then 
  35. echo "$scp_file is not exists";  
  36. exit;  
  37. fi  
  38. #read configure file  
  39. source $conf_file  
  40. #若是没有在配置文件里提供密码,则在命令行输入  
  41. if [ "${#ssh_passwords[@]}" = "0" ] || [ "${#ssh_passwords[@]}" -lt "${#ssh_hosts[@]}" ]  
  42. then 
  43. read -p "please input password:" -s default_ssh_password  
  44. fi  
  45. success_hosts="";  
  46. fail_hosts="";  
  47. for((i=0;i<${#ssh_hosts[@]};i++))  
  48. do  
  49. #remote ssh host  
  50. ssh_host=${ssh_hosts[$i]};  
  51. #remote ssh port  
  52. ssh_port=${ssh_ports[$i]};  
  53. if [ "$ssh_port" = "" ]  
  54. then 
  55. ssh_port=$default_ssh_port;  
  56. fi  
  57. #remote ssh user 
  58. ssh_user=${ssh_users[$i]};  
  59. if [ "$ssh_user" = "" ]  
  60. then 
  61. ssh_user=$default_ssh_user;  
  62. fi  
  63. #remote ssh password 
  64. ssh_password=${ssh_passwords[$i]};  
  65. if [ "$ssh_password" = "" ]  
  66. then 
  67. ssh_password=$default_ssh_password;  
  68. fi  
  69. echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) start" 
  70. #scp file or dir  
  71. /usr/bin/expect scp_upload.sh "$ssh_host" "$ssh_port" "$ssh_user" "$ssh_password" "$scp_file" "$scp_target" 
  72. if [ "$?" -eq "0" ]  
  73. then 
  74. success_hosts="$success_hosts,$ssh_host" 
  75. else 
  76. fail_hosts="$fail_hosts,$ssh_host" 
  77. fi  
  78. echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) end" 
  79. echo "" 
  80. done  
  81. echo "success_hosts=[$success_hosts]" 
  82. echo "fail_hosts=[$fail_hosts]" 

2.scp_upload.sh的源代码

 


 
 
  1. #!/usr/bin/expect  
  2. #author: yifangyou  
  3. #create time:2011-05-17  
  4. #host  
  5. set scphost "[lindex $argv 0]" 
  6. #ssh端口  
  7. set port "[lindex $argv 1]" 
  8. #ssh用户名  
  9. set scpuser "[lindex $argv 2]" 
  10. #ssh密码  
  11. set scppw "[lindex $argv 3]" 
  12. #要上传的文件名或者目录  
  13. set file "[lindex $argv 4]" 
  14. #要上传到远程机器的文件名或者目录  
  15. set target "[lindex $argv 5]" 
  16. spawn scp -r -P $port $file $scpuser@$scphost:$target  
  17. #设置超时时间,防止远程机器防火墙没有开,而挂起  
  18. set timeout 30  
  19. expect {  
  20. #respose: "root@1.2.3.4's password:",自动输入密码  
  21. "*password*" {  
  22. set timeout 30  
  23. send "$scppw\r" 
  24. }  
  25. #the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes" 
  26. "*yes*" {  
  27. set timeout 30  
  28. send "yes\r" 
  29. set timeout 30  
  30. expect "*password*" 
  31. set timeout 30  
  32. send "$scppw\r" 
  33. }  
  34. busy {send_user "\n<error:busy>";exit 1;}  
  35. failed {send_user "\n<error:failed>";exit 2;}  
  36. timeout {send_user "\n<error:timeout>";exit 3;}  
  37. }  
  38. #Permission denied not try again,回报出错信息  
  39. set timeout 30  
  40. expect {  
  41. "*denied*" {  
  42. send_user "\n<error:Permission denied>" 
  43. exit 4  
  44. }  
  45. "*No such file*" {  
  46. send_user "\n<error:No such file>" 
  47. exit 5  
  48. }  
  49. busy {send_user "\n<error:busy>";exit 6;}  
  50. failed {send_user "\n<error:failed>";exit 7;}  
  51. timeout {send_user "\n<error:timeout>";exit 8;}  
  52. }  
  53. 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_upload.sh,multi_scp_upload.sh,scp.conf放到同一个目录下,运行multi_scp_upload.sh即可
5.运行效果

 



     本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/610305,如需转载请自行联系原作者



相关文章
|
Shell Linux
Linux使用Shell脚本SCP批量传输脚本
Linux使用Shell脚本SCP批量传输脚本
1035 0
|
1月前
|
人工智能 安全 前端开发
老金发现个4277星的Claude Code企业神器,看完我直接跪了
老金拆解GitHub高星项目,教你用Claude Code打造自动化开发系统:Skills规范代码、Agents智能审查、Commands一键操作、Hooks自动触发、GitHub Actions定时巡检、MCP集成外部工具。4277星实战验证,企业级AI编程提效方案,文末附免费开源知识库。
668 12
|
安全 PHP
攻防世界-file_include(convert.iconv的使用)
攻防世界-file_include(convert.iconv的使用)
601 0
|
机器学习/深度学习 人工智能 数据可视化
何恺明CV课程 | AI大咖说
麻省理工学院(MIT)电气工程与计算机科学系(EECS)副教授何恺明开设了两门精彩课程:“Advance in Computer Vision”和“Deep Generative Models”。何恺明是计算机视觉和深度学习领域的杰出科学家,曾提出深度残差网络(ResNet)等重要成果。这两门课程不仅涵盖了最新的研究前沿,还由何恺明亲自授课,内容涉及卷积神经网络、生成对抗网络、变分自编码器等,是学习计算机视觉和生成模型的宝贵资源。
668 8
|
人工智能 API 开发者
阿里云CTO周靖人:通义开源模型下载量破2000万,百炼实现150%增长!
阿里云CTO周靖人:通义开源模型下载量破2000万,百炼实现150%增长!
1371 1
|
机器学习/深度学习 编解码 自然语言处理
【文献学习】An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
本文介绍了如何使用纯Transformer模型进行图像识别,并讨论了模型的结构、训练策略及其在多个图像识别基准上的性能。
929 3
IBSS、BSS和ESS之间的区别
【8月更文挑战第23天】
1568 0
|
网络协议 算法 网络性能优化
计算机网络 第五章 网络层(习题)
计算机网络 第五章 网络层(习题)
703 1
npm报错:npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED,npm ERR! npm ERR! If you are behind a
npm报错:npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED,npm ERR! npm ERR! If you are behind a
467 0
|
Java C# 开发工具
9个最好用的在线编译/调试工具
电脑没有C/C++的开发环境了,只能找找在线的编译器。。IDEone不错。。。 本文要推荐9个最好用的在线编译器,以下顺序不按排名先后: 1、ideone 可以在线编译、调试C/C++,JAVA,PHP,Python,Perl,以及其他40多种编程语言。
3999 0