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

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。

梯子

转载于:https://my.oschina.net/fengshuzi/blog/673535

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值