linux工具自动输入密码工具实际案列

一键部署测试环境

1.前提条件

Linux服务器需要安装 expect 包,安装命令:

yum install expect

2.脚本替换

使用用方法

  1. 将两个脚本放入当前 /app/data/ 目录下
  2. 修改对应的jar包来源路径 src、dest、username、pwd、host、killword
  • 分别赋予两个脚本可执行权限 chmod 777 deploy.sh(expect_scp.sh)
  • ./deploy.sh 运行

运行效果

1566467990260.jpg

脚本

deploy.sh

#!/bin/sh
#从jenkins拉取的目录
src=/data/jenkins/workspace/dev/core-server/target/core-server.jar
#存放jar目标目录
dest=/app/data
#jenkins服务器用户名
username=xxxx
pwd=xxxx
#jenkins服务器地址
host=192.168.2.121
#jar的名字
killword=xxxx
./expect_scp.sh $host $pwd $src $dest $username

echo "===从$src拉取包结束,scp end==="

echo "============================================开始停止进程start kill process"
ps -ef|grep java |grep $killword|awk '{print $2}'|xargs kill -9
progress_bar=''
for i in {0..100..3}
do
  printf "等待启动进度条:[%-33s]%d%%\r" $progress_bar $i
  sleep 0.1
  progress_bar=#${progress_bar}
done
echo "============================================开始启动应用 start running process"
#这里可以执行jar或war的启动命令,读者自行编写
java -jar xxx.war
echo "============================================启动应用结束 end start"

expect_scp.sh

#! /usr/bin/expect
set host [lindex $argv 0]
set pwd  [lindex $argv 1]
set src  [lindex $argv 2]
set dest [lindex $argv 3]
set username [lindex $argv 4]
spawn scp $username@$host:$src $dest
expect {
"*assword" {
send "$pwd\n"
}
"*yes" {
sleep 5
send user "send yes"
send "yes\n"
}
eof {
sleep 5
send_user "eof\n"
}

}
send "100%"
expect eof


你可能感兴趣的:(linux工具自动输入密码工具实际案列)