python3 파일을 linux 서버에 전송하고 압축을 풀기

2161 단어
#!/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')

다음으로 전송:https://www.cnblogs.com/17bdw/p/11559514.html

좋은 웹페이지 즐겨찾기