zoukankan      html  css  js  c++  java
  • python3传文件到linux服务器然后解压

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import os
    import paramiko
    import time
    from scp import SCPClient
    
    
    
    #将脚本传到服务器,并解压
    def transRemote(ip,user,password):
    	try:
    
    		ssh = paramiko.SSHClient()
    		ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    		ssh.connect(ip, 22, username=user, password=password, timeout=200)
    		stdin, stdout, stderr=ssh.exec_command("pwd")
    		#path=stdout.read().strip("
    ")
    
    		# 获取路劲
    		path= stdout.read().decode('utf-8').strip("
    ")
    
    		#查看python版本
    		stdin, stdout, stderr=ssh.exec_command("python -V")
    		print(stdout.read().decode('utf-8'))
    		# pythonVsersion=stdout.read().strip("
    ")
    		pythonVsersion = stdout.read().decode('utf-8').strip("
    ")
    		scriptName="jixianjiancha.tar.gz"
    		if(len(pythonVsersion)==0):
    			scriptName="jixianjiancha.tar.gz"
    		else:
    			if(pythonVsersion.split()[1].startswith("3")):
    				scriptName="jixianjiancha2.tar.gz"
    
    		current_path=os.getcwd()
    		#print current_path
    		scpclient = SCPClient(ssh.get_transport(), socket_timeout=15.0)
    		scpclient.put('%s\check\%s'%(current_path,scriptName), '%s/jixianjiancha.tar.gz'%path)
    		print("[*]将脚本传送到远程服务器")
    
    		index=0
    		script_number=12
    		while(index<10):
    			stdin, stdout, stderr=ssh.exec_command('tar -xvf %s/jixianjiancha.tar.gz'%path)
    			time.sleep(2)
    			stdin, stdout, stderr=ssh.exec_command("ls %s"%(path))
    			scripts=len(stdout.read().decode('utf-8').strip("
    "))
    			if(scripts==12):
    				index=11
    			else:
    				index+=1
    		print("[*]在远程服务器上解压脚本")
    		ssh.close()
    		return True
    	except Exception as e:
    		print(e)
    	return False
    
    if __name__ == '__main__':
        transRemote('192.168.221.133','root','toor')
    
  • 相关阅读:
    opencast的docker安装
    编译openwrt_MT7688_hiwooya
    linux中mysql自动同步
    网站服务器迁移
    vtigercrm安装
    ixcache的蜜汁突发故障
    20180628
    pip3 install -r requirements.txt安装超时解决方法
    pytest文档29-allure-pytest
    pytest框架
  • 原文地址:https://www.cnblogs.com/17bdw/p/11559514.html
Copyright © 2011-2022 走看看