最近在发布版本时,需要多个台主机之间来回scp,而且每次需要数据密码,操作比较麻烦。因此,写个脚本一键发布。
前提需要ssh免密钥登录,具体操作详解ssh免密钥登录。

点击(此处)折叠或打开

  1. #!/bin/bash

  2. dest_ip_arr=(10.99.11.23)
  3. dest_lib_dir="/instance/filesevice/devstat_lib"
  4. dest_tomcat_dir="/instacne/filesevice/tomcat-file-show"
  5. src_jar="/instance/filesevice/devstat/devstat.war"


  6. port=22
  7. jar_xvf_cmd="cd $dest_lib_dir; /software/jdk1.7.0_65/bin/jar -xvf $dest_lib_dir/devstat.war"
  8. start_cmd="cd $dest_tomcat_dir/bin; /bin/sh startup.sh"
  9. stop_cmd="cd $dest_tomcat_dir/bin; /bin/sh shutdown.sh"
  10. get_pid_cmd="ps -ef |grep -w \"$dest_tomcat_dir\" |grep -v 'grep' |awk '{print $2}'"
  11. user=work


  12. function start() {
  13.   for ip in ${dest_ip_arr[@]};
  14.   do
  15.     echo "start $ip $dest_tomcat_dir now"
  16.     ssh -l$user -p$port $ip "$start_cmd"
  17.     pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
  18.     test "$pid" != '' && echo "start $ip $dest_tomcat_dir ok" || echo "start $ip $dest_tomcat_dir failed!"
  19.     sleep 1
  20.   done
  21. }


  22. function stop() {
  23.   for ip in ${dest_ip_arr[@]};
  24.   do
  25.     echo "stop $ip $dest_tomcat_dir now"
  26.     ssh -l$user -p$port $ip "$stop_cmd"
  27.     pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
  28.     test "$pid" != ''&& echo "stop $ip $dest_tomcat_dir ok!" || echo "stop $ip $dest_tomcat_dir ok!"
  29.     sleep 1
  30.   done
  31. }




  32. function status() {
  33.   for ip in ${dest_ip_arr[@]};
  34.   do
  35.    pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
  36.    test "$pid" != '' && echo "$ip $dest_tomcat_dir is running, and the pid is $pid" || echo "can not detect the pid of $ip $dest_tomcat_dir is !"
  37.   done
  38. }


  39. function restart() {
  40.   for ip in ${dest_ip_arr[@]};
  41.   do
  42.     echo "restart $ip $dest_tomcat_dir now"
  43.     ssh -l$user -p$port $ip "$stop_cmd"
  44.     pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
  45.     test "$pid" == '' && echo "stop $ip $dest_tomcat_dir ok!"
  46.     sleep 1
  47.     ssh -l$user -p$port $ip "$start_cmd"
  48.     pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
  49.     test "$pid" != '' && echo "restart $ip $dest_tomcat_dir ok" || echo "restart $ip $dest_tomcat_dir failed!"
  50.   done
  51. }


  52. function scp_and_unzip(){
  53.   for ip in ${dest_ip_arr[@]};
  54.   do
  55.    echo "scp jar/war package to $ip $dest_lib_dir start"
  56.    scp $src_jar $user@$ip:$dest_lib_dir
  57.    echo "scp jar/war package to $ip $dest_lib_dir end"
  58.    ssh -l$user -p$port $ip "$jar_xvf_cmd"
  59.    echo "unzip $ip $dest_lib_dir package"
  60.   done
  61. }


  62. function publish_restart(){
  63.   scp_and_unzip
  64.   restart
  65. }


  66. function publish_start(){
  67.   scp_and_unzip
  68.   start
  69. }


  70. case "$1" in
  71.   start)
  72.     start
  73.   ;;
  74.   stop)
  75.     stop
  76.   ;;
  77.   status)
  78.     status
  79.   ;;
  80.   restart)
  81.      restart
  82.   ;;
  83.   prestart)
  84.      publish_restart
  85.   ;;
  86.   pstart)
  87.      publish_start
  88.   ;;
  89.   * )
  90.     echo "usage: $0 {start|pstart|stop|status|prestart|restart}"
  91.     exit 1
  92.   ;;
  93. esac

09-28 07:50