应用场景:
脚本需求:
以10.0.50.104为中心,向周边各个网段内的所有服务器分发目录或文件,每台服务器上都有Tomcat-0~13等一共14个应用项目,每个应用各自独立固定。
可能服务器A上仅仅只有Tomcat0-3在运行项目,有的服务器B上仅仅只有Tomcat-4-7在运行项目。需要使用脚本可以灵活批量复制项目目录到生产环境做发布。
解决思路:
scp -r /usr/local/tomcat-0/webapps/* root@目标IP:/usr/local/tomcat-0/webapps/
其中可以看见有Tomcat-0和目标IP两个地方需要做处理,而且IP跨度的网段很多,很难做到统一。故此选择使用Select语句来选择,并将IP做传参处理。请提前做好SSH免秘钥认证
例如:scp.sh 192.168.100.101 192.168.100.102 ...
脚本案例:
#!/bin/bash PS3="Select a number:" select Tomcat in {Tomcat-{0,1,2,3,4,5,6,7,8,9,10,11,12,13},exit};do case $Tomcat in Tomcat-0) for p in $* do scp -r /usr/local/tomcat-0/webapps/bison-sc-finance/WEB-INF/classes/com root@$p:/usr/local/tomcat-0/webapps/bison-sc-finance/WEB-INF/classes/ done break ;; Tomcat-1) for p in $* do scp -r /usr/local/tomcat-1/webapps/bison-sc-advertising/WEB-INF/classes/com root@$p:/usr/local/tomcat-1/webapps/bison-sc-advertising/WEB-INF/classes/ done break ;; Tomcat-2) for p in $* do scp -r /usr/local/tomcat-2/webapps/bison-conversion/WEB-INF/classes/com root@$p:/usr/local/tomcat-2/webapps/bison-conversion/WEB-INF/classes/ done break ;; Tomcat-3) for p in $* do scp -r /usr/local/tomcat-3/webapps/bison-product/WEB-INF/classes/com root@$p:/usr/local/tomcat-3/webapps/bison-product/WEB-INF/classes/ done break ;; Tomcat-4) for p in $* do scp -r /usr/local/tomcat-4/webapps/bison-order/WEB-INF/classes/com root@$p:/usr/local/tomcat-4/webapps/bison-order/WEB-INF/classes/ done break ;; Tomcat-5) for p in $* do scp -r /usr/local/tomcat-5/webapps/bison-classify/WEB-INF/classes/com root@$p:/usr/local/tomcat-5/webapps/bison-classify/WEB-INF/classes/ done break ;; Tomcat-6) for p in $* do scp -r /usr/local/tomcat-6/webapps/bison-invoice/WEB-INF/classes/com root@$p:/usr/local/tomcat-6/webapps/bison-invoice/WEB-INF/classes/ done break ;; Tomcat-7) for p in $* do scp -r /usr/local/tomcat-7/webapps/bison-mail-reply/WEB-INF/classes/com root@$p:/usr/local/tomcat-7/webapps/bison-mail-reply/WEB-INF/classes/ done break ;; Tomcat-8) for p in $* do scp -r /usr/local/tomcat-8/webapps/bison-counterfeit/WEB-INF/classes/com root@$p:/usr/local/tomcat-8/webapps/bison-counterfeit/WEB-INF/classes/ done break ;; Tomcat-9) for p in $* do scp -r /usr/local/tomcat-9/webapps/bison-sc-good-review/WEB-INF/classes/com root@$p:/usr/local/tomcat-9/webapps/bison-sc-good-review/WEB-INF/classes/ done break ;; Tomcat-10) for p in $* do scp -r /usr/local/tomcat-10/webapps/bison-advertising-sync/WEB-INF/classes/com root@$p:/usr/local/tomcat-10/webapps/bison-advertising-sync/WEB-INF/classes/ done break ;; Tomcat-11) for p in $* do scp -r /usr/local/tomcat-11/webapps/bisn-advertising-adjust/WEB-INF/classes/com root@$p:/usr/local/tomcat-11/webapps/bisn-advertising-adjust/WEB-INF/classes/ done break ;; Tomcat-12) for p in $* do scp -r /usr/local/tomcat-12/webapps/bison-keyword-api/WEB-INF/classes/com root@$p:/usr/local/tomcat-12/webapps/bison-keyword-api/WEB-INF/classes/ done break ;; Tomcat-13) for p in $* do scp -r /usr/local/tomcat-13/webapps/bison-keyword-ranking/WEB-INF/classes/com root@$p:/usr/local/tomcat-13/webapps/bison-keyword-ranking/WEB-INF/classes/ done break ;; exit) exit ;; *) echo "Error,Please enter again!" esac done