这里最简单的方法是压缩文件并使用 sftp.put 将文件放在远程机器上并在远程机器上解压:
tar = tarfile.open("Tests.tar.gz", "w:gz")
path_me = os.path.dirname(os.path.realpath(sys.argv[0]))
tar.add(path_me, arcname="Tests")
tar.close()
transport = paramiko.Transport((SERVER_IP, 22))
transport.connect(username = 'root', password = 'xxxxx')
sftp = paramiko.SFTPClient.from_transport(transport)
remotepath='/home/Tests.tar.gz'
localpath=os.path.join(path_me+"/Tests.tar.gz")
#print(localpath)
sftp.put(localpath, remotepath)
sftp.close()
transport.close()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(SERVER_IP, username='root', password='philips')
stdin, stdout, stderr = ssh.exec_command("tar -C /home -xzf /home/Tests.tar.gz")