Python获取远程指定目录下所有文件的绝对路径

import paramiko
import os

# SSH 连接信息
hostname = "your_remote_host"
port = 22
username = "your_username"
password = "your_password"

# 远程目录路径
remote_directory = "/data"

# 建立 SSH 连接
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, port, username, password)

# 执行 ls 命令获取文件列表
command = f"ls {remote_directory}"
stdin, stdout, stderr = ssh.exec_command(command)

# 读取输出结果
file_list = stdout.read().decode().splitlines()

# 获取文件的绝对路径
absolute_paths = [os.path.join(remote_directory, file_name) for file_name in file_list]

# 关闭 SSH 连接
ssh.close()

# 打印文件列表及其绝对路径
print("Files in remote directory with absolute paths:")
for file_path in absolute_paths:
    print(file_path)
posted @   槑孒  阅读(206)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示