容器中实现拉取其它服务器的jar包程序

缘由:在做接口自动化测试时,若业务场景有一个前置仓库,在该仓库内完成一系列的场景测试,一旦某一场景测试失败,脏数据对环境造成影响则需要清理;
1、我容器的内核系统为Debian GNU/Linux 10,更换容器的下载源,换成阿里的镜像(原因:后面步骤会apt-get install tcl tk expect,在安装except会提示Unable to locate packet就是无法找到包嘛,那还不赶紧sudo apt-get update下!官网更新很慢,所以需要更新来更换,更换镜像也有区别,其实Ubuntu18.04版之前的任一版更改apt源为国内源方法早就有了,内容大同小异,我们应当掌握其规律了,其实每一版内容不同的地方就是版本号(或者官方一点的说:系统代号),所以我们先了解下新版本的系统代号,命令lsb_release -c,具体连接:https://blog.csdn.net/zhangjiahao14/article/details/80554616)
debian10更换阿里源:vim /etc/apt/sources.list (清空,或注释原来的配置,在配置中加入以下阿里源,保存退出)

deb https://mirrors.aliyun.com/debian stable main contrib non-free
deb https://mirrors.aliyun.com/debian stable-updates main contrib non-free

执行
apt-get clean
apt-get update
链接:https://blog.csdn.net/weixin_45784720/article/details/109040954

2、shell_agv.sh脚本如下
`#!/usr/bin/expect
set timeout 20s
spawn ssh root@172.31.236.15
expect {
"yes/no" { send "yes "; exp_continue}
"
password:" { send "zhang19941206 " }
}

expect "#*"
send "pwd "

send "cd /opt/docker "

send "PYTHONIOENCODING=utf-8 python3 python_subprocess_agv.py "

expect eof
exit
`
在python3.7.1中执行直接python3 python_subprocess_agv.py,在python3.6.8中提示UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
我查看了编码也是utf-8,链接:https://blog.csdn.net/qq_18863573/article/details/102478506

3、python_subprocess_agv.py脚本
`
import subprocess
import time
def excuteCommand(com):
ex = subprocess.Popen(com, stdout=subprocess.PIPE, shell=True)
out, err = ex.communicate()
status = ex.wait()
print("cmd in:", com)
print("cmd out: ", out.decode())
return out.decode()

def excuteCommands(com):
sub = subprocess.Popen(com, stdout=subprocess.PIPE, shell=True)

while sub.poll() is None:
    time.sleep(0.1)

return str(sub.returncode)

def test_one():
print("哈哈,开始执行")

two = time.time()
a = excuteCommands("/opt/docker/restart_agv.sh")
print("{} end ,please checking".format(a))

print("结束time: {}".format(time.time()-two))
print("yes/no")

test_one()
4、restart_agv.sh脚本:
cd /opt/docker
ls
docker-compose stop evo-rcs
pid=ps -ef|grep simu|grep -v grep|awk '{print $2}'
if [ -n "$pid" ]
then
kill -9 $pid
echo "shutdown simulation"
fi
echo "waiting 20s"
sleep 5s
echo "shell restart agv"
cd /opt/docker/sim/
sh simulationStart.sh
`
等jar包或服务都起来了之后,就可以执行接口访问等等了

ps:第一步可以采用高级版本,采用python中的paramiko模块来操作ssh后的操作,真的牛逼啊,直接上脚本

import paramiko
import time

def paramiko_interact(x):
trans = paramiko.Transport(('12345678', 22)) # 【坑1】 如果你使用 paramiko.SSHClient() cd后会回到连接的初始状态
trans.start_client()
# 用户名密码方式
trans.auth_password(username='root', password='11111')
# 打开一个通道
channel = trans.open_session()
channel.settimeout(300)
# 获取一个终端
channel.get_pty()
# 激活器
channel.invoke_shell()
cmd = 'cd /opt/docker '
# 发送要执行的命令
channel.send(cmd)
cmd = x # 【坑2】 如果你使用 sh ./study_shell.sh 可能会出现 [: 11: y: unexpected operator 错误
# 回显很长的命令可能执行较久,通过循环分批次取回回显
channel.send(cmd)
while True:
time.sleep(0.2)
rst = channel.recv(1024)
rst = rst.decode('utf-8')
print(rst)
# 通过命令执行提示符来判断命令是否执行完成
if 'yes/no' in rst:
#channel.send('yes ') # 【坑3】 如果你使用绝对路径,则会在home路径建立文件夹导致与预期不符
#time.sleep(0.5)
# ret = channel.recv(1024)
# ret = ret.decode('utf-8')
# print(ret)
break

channel.close()
trans.close()  # channel.invoke_shell()

if name == 'main':
#paramiko_interact(x='python3 python_subprocess_rcs.py ')
paramiko_interact(x='python3 python_subprocess_rcs.py ')

链接1:https://www.cnblogs.com/zhengna/p/10496746.html
链接2:https://blog.csdn.net/weiran2009/article/details/86719737

原文地址:https://www.cnblogs.com/peng1206/p/13819068.html