加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python3传文件到linux服务器然后解压

发布时间:2020-12-20 10:38:36 所属栏目:Python 来源:网络整理
导读:#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport paramikoimport timefrom scp import SCPClient#将脚本传到服务器,并解压def transRemote(ip,user,password): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.Auto
#!/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("n")

        # 获取路劲
        path= stdout.read().decode('utf-8').strip("n")

        #查看python版本
        stdin,stderr=ssh.exec_command("python -V")
        print(stdout.read().decode('utf-8'))
        # pythonVsersion=stdout.read().strip("n")
        pythonVsersion = stdout.read().decode('utf-8').strip("n")
        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('%scheck%s'%(current_path,scriptName),'%s/jixianjiancha.tar.gz'%path)
        print("[*]将脚本传送到远程服务器")

        index=0
        script_number=12
        while(index<10):
            stdin,stderr=ssh.exec_command('tar -xvf %s/jixianjiancha.tar.gz'%path)
            time.sleep(2)
            stdin,stderr=ssh.exec_command("ls %s"%(path))
            scripts=len(stdout.read().decode('utf-8').strip("n"))
            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')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读