expect自动登录以及远程脚本执行

♀尐吖头ヾ 提交于 2019-12-02 03:09:23

0、前言

场景描述:现有192.168.5.51~192.168.5.55需要发布不同观点项目,希望达到的效果是ssh登录到192.168.5.117执行一个脚本一键发布所有项目到51,52,53,54,55.

1、remote.sh

此脚本放置在本地终端


#!/bin/sh
/usr/bin/expect <<EOF
#!/usr/bin/expect




if { "$1" == "174" } {
    set timeout -1
    spawn /usr/local/nginx/sbin/remote1.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.174's password:" {send "passowrd\r";exp_continue;}
    }
}

if { "$1" == "soa-55" } {
    spawn /usr/local/nginx/sbin/remote_soa_55.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.55's password:" {send "passowrd\r";exp_continue;}
    }
}

if { "$1" == "soa-54" } {
    set timeout -1
    spawn /usr/local/nginx/sbin/remote_soa_54.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.54's password:" {send "passowrd\r";exp_continue;}
    }
}

if { "$1" == "api" } {
    set timeout -1
    spawn /usr/local/nginx/sbin/remote_api.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.51's password:" {send "passowrd\r";exp_continue;}
    }
}

if { "$1" == "cos-63" } {
    set timeout -1
    spawn /usr/local/nginx/sbin/remote_cos_63.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.63's password:" {send "passowrd\r";exp_continue;}
    }
}

if { "$1" == "cos-cms-53" } {
    set timeout -1
    spawn /usr/local/nginx/sbin/remote_cos_cms_53.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.53's password:" {send "passowrd\r";exp_continue;}
    }
}


if { "$1" == "cms-52" } {
    set timeout -1
    spawn /usr/local/nginx/sbin/remote_cms_52.sh
    expect {
    "Are you sure you want to continue connecting (yes/no)?" {send "yes\r";exp_continue;}
    "root@192.168.5.52's password:" {send "passowrd\r";exp_continue;}
    }
}

if { "$1" == "all" } {
}





EOF

 

2、remote_api.sh

调用的脚本

 

#!/bin/bash
set timeout 1000
/usr/bin/ssh root@192.168.5.174  " rm -rf  /usr/local/tomcat_9090/deploy/*.war"
scp /usr/local/nginx/sbin/code-soa.war  root@192.168.5.174:/usr/local/tomcat_9090/deploy/
/usr/bin/ssh root@192.168.5.174  "source /etc/profile"
/usr/bin/ssh root@192.168.5.174  "sh /usr/local/tomcat_9090/deploy/remote_deploy.sh"

3、deploy_remote.sh

放在目标机器的脚本

rm  /usr/local/tomcat-cms/webapps/code-* -rf;
kill -9   `netstat -tpln|grep ":9090"|awk '{printf $7}'|cut -d/ -f1`;
cp /usr/local/tomcat-cms/deploy/code-cms.war /usr/local/tomcat-cms/webapps/
source /etc/profile
sh /usr/local/tomcat-cms/bin/startup.sh

 

4、deploy_all.sh

#!/bin/bash
sh remote117.sh api;
sh remote117.sh soa-55;
sh remote117.sh soa-54;
sh remote117.sh cos-63;
sh remote117.sh cos-cms-53;
sh remote117.sh cms-52;

5、执行效果

 

6、思考

既然可以一键发布,那么是否可以做到从svn下载下代码后自动构建maven包然后一键部署到tomcat。

梯子

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!