importos 导入os包,后面使用这个包来操作文件系统importzipfile 导入zip包,解压importparamiko 导入paramiko这个主要是用来SSHimportsys 目录操作from scp importSCPClient 上传东西进行SSH命交互
unzip= zipfile.ZipFile(“/home/shiyanlou/demo/demo_war.war”)
//创建unzip对象 里面有
“/home/shiyanlou/demo/demo_war.war
for file inunzip.namelist():
display= unzip.extract(file, “/home/shiyanlou/demo/demo”)print(display)
解压文件 并且列出来
ssh = paramiko.SSHClient() 创建对象ssh
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(“127.0.0.1”, 22, “shiyanlou”, “35575794”) 连接 主机信息
ssh.exec_command(“mkdir /home/shiyanlou/Desktop/apache-tomcat-9.0.0.M18/webapps/demo”) 执行命令
def scandir(dirs, upload, ssh): 函数扫描文件夹 主要实现的是文件夹的创建和文件的上传
for f in os.listdir(dirs):
_f = dir + “/” + f
_uplod = uplod + “/” + f
if os.path.isdir(_f):
ssh.exec_command(“mkdir” + _uplod)
scandir(_f, _uplod, ssh)
elif os.path.isfile(_f):
scpclient = SCPClient(ssh.get_transport(), socket_timeout=15.0)
scpclient.put(_f, _uplod)
pass
ssh.exec_command(“/home/shiyanlou/tomcat.sh restart”)
执行重启命令
ssh.close()
关闭ssh