打包成tar包,使用scp命令传递包
import paramiko
import time
# 项目打包服务器参数信息
psrc_host = "192.168.12.132"
psrc_port = 22
psrc_usrn = "bosec"
psrc_pass = "1234"
# 是否直接用该账号可以打包
psrc_strt = False
# 提权密码
psrc_ropa = "mia"
# 项目发布服务器参数信息
ptar_host = "192.168.12.131"
ptar_port = 22
ptar_usrn = "root"
ptar_pass = "1234"
# 提权密码
ptar_ropa = "1234"
# 获取回显
def getraceback(session):
time.sleep(0.5)
result = session.recv(8192).decode('utf-8')
return result
def tarpackage(projectname):
# global psrc_host,psrc_port,psrc_usrn,psrc_pass,psrc_ropa
sshclient = paramiko.SSHClient()
# 连接信息的加载与设置
sshclient.load_system_host_keys()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 配置连接信息,包括主机地址,端口,用户名,密码
sshclient.connect(hostname=psrc_host, port=psrc_port, username=psrc_usrn, password=psrc_pass)
# 测试执行pwd命令
stdin,stdout,stderr = sshclient.exec_command('pwd')
# 打印pwd命令执行结果
print(stdout.read().decode('utf-8'))
# 打包前检查,如果有包则先输出MD5,没包则输出空,用于最后打包验证
stdin,stdout,stderr = sshclient.exec_command('md5sum /home/mia/'+projectname+'.tar.gz\n')
stdmd = stdout.read().decode('utf-8')
if stdmd != "":
index = stdmd.index(projectname+'.tar.gz')
mdfiv = stdmd[:index-14]
else:
mdfiv = ""
sshishell = sshclient.invoke_shell()
# 是否直接用该账号可以打包
psrc_strt = False
if psrc_strt:
stdin,stdout,stderr = sshclient.exec_command('tar -zcPf /home/mia/'+projectname+'.tar.gz -C /home/mia '+projectname)
result = stderr.read()
else:
# 使用root账号进行打包操作
sshishell.send('su - \n')
getraceback(sshishell)
# 输入提权账号密码
sshishell.send(psrc_ropa+"\n")
getraceback(sshishell)
sshishell.send('tar -zcPf /home/mia/'+projectname+'.tar.gz -C /home/mia '+projectname+'\n')
print(getraceback(sshishell))
# 打包验证,输出是否成功
stdin,stdout,stderr = sshclient.exec_command('md5sum /home/mia/'+projectname+'.tar.gz\n')
if stderr.read().decode('utf-8') == "":
mdfis = stdout.read().decode('utf-8')
indei = mdfis.index(projectname+'.tar.gz')
mdfix = mdfis[:indei-14]
if mdfix != mdfiv:
message = "打包成功"
else:
message = "打包失败-MD5是旧的"
else:
message = "打包失败-MD5校验失败"
mdfix = "none"
sshclient.close()
return message,mdfix
# print(tarpackage("marine-service"))
def scppackage(projectname,mdfsum):
global psrc_host,psrc_port,psrc_usrn,psrc_pass
global ptar_host,ptar_port,ptar_usrn,ptar_pass
transpt = paramiko.Transport((psrc_host, psrc_port))
transpt.start_client()
transpt.auth_password(username=psrc_usrn, password=psrc_pass)
session = transpt.open_session()
# 这个过期时间是回显超时时间,如果超过这个时间没有回显则会抛出异常
session.settimeout(360)
session.get_pty()
session.invoke_shell()
session.send('scp /home/mia/'+projectname+'.tar.gz '+ptar_usrn+'@'+ptar_host+':/etc/ansible/package/\r')
# 回显很长的命令可能执行较久,通过循环分批次取回回显
while True:
rst = getraceback(session)
# 通过命令执行提示符来判断命令是否执行完成
if 'yes/no' in rst:
session.send('yes\r')
ret = getraceback(session)
session.send('1234\r')
rst = getraceback(session)
break
elif 'password' in rst:
session.send('1234\r')
rst = getraceback(session)
break
session.close()
transpt.close()
# 传包验证,验证传完的包是否是之前打包好的
sshclient = paramiko.SSHClient()
# 连接信息的加载与设置
sshclient.load_system_host_keys()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshclient.connect(hostname=ptar_host, port=ptar_port, username=ptar_usrn, password=ptar_pass)
# 传包检查,与打包完成的MD5进行比对,相同则成功
stdin,stdout,stderr = sshclient.exec_command('md5sum /etc/ansible/package/'+projectname+'.tar.gz\n')
if stderr.read().decode('utf-8') == "":
mdfsp = stdout.read().decode('utf-8')
index = mdfsp.index(projectname+'.tar.gz')
mdfis = mdfsp[:index-23]
if mdfis == mdfsum:
message = "传包成功"
else:
message = "传包失败-MD5不匹配"
else:
message = "传包失败-没有项目包"
return message
def pubpackage(projectname):
global ptar_host,ptar_port,ptar_usrn,ptar_pass,ptar_ropa
transpt = paramiko.Transport((ptar_host, ptar_port))
transpt.start_client()
transpt.auth_password(username=ptar_usrn, password=ptar_pass)
session = transpt.open_session()
# 设置回显的超时时间,如果超过这个时间Session没有反应则中断
session.settimeout(2)
session.get_pty()
# 新加一个invoke_shell,用于交互式输入
session.invoke_shell()
session.send('su - \n')
getraceback(session)
session.send('cd /etc/ansible\n')
getraceback(session)
session.send('ansible-playbook '+projectname+'.yaml\n')
rvcache = ""
# 循环不断读取回显缓冲区的数据
while True:
# 超过时间会中断,所以此处故意设置Try
try:
receive = session.recv(8192)
rvcache = rvcache+receive.decode('utf-8')
time.sleep(1)
continue
except Exception as e:
break
session.close()
transpt.close()
return rvcache
# print(pubpackage("portagency"))