How I can list files from REMOTE HOST directory using Python?(如何使用 Python 列出远程主机目录中的文件?)
问题描述
我需要从远程主机目录获取文件列表,在我的本地机器上运行代码.
I need get the list of files from a remote host directory, running the code in my local machine.
类似于 远程主机 上的 os.listdir(),而不是运行的本地机器上的 os.lisdir()蟒蛇代码.
Is something like os.listdir() at remote host machine, NOT is os.lisdir() in the local machine that runs the python code.
在 bash 中这个命令有效ssh user@host "find/remote/path/-name "pattern*" -mmin -15" >/local/path/last_files.txt
In bash this command works
ssh user@host "find /remote/path/ -name "pattern*" -mmin -15" > /local/path/last_files.txt
推荐答案
在远程机器上运行命令的最佳选择是通过 ssh 和 paramiko.
Your best option for running commands on a remote machine is via ssh with paramiko.
几个如何使用该库并向远程系统发出命令的示例:
A couple of examples of how to use the library and issue a command to the remote system:
import base64
import paramiko
# Let's assign an RSA SSH key to the 'key' variable
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
# And create a client instance.
client = paramiko.SSHClient()
# Create an object to store our key
host_keys = client.get_host_keys()
# Add our key to 'host_keys'
host_keys.add('ssh.example.com', 'ssh-rsa', key)
# Connect to our client; you will need
# to know/use for the remote account:
#
# IP/Hostname of target
# A username
# A password
client.connect('IP_HOSTNAME', username='THE_USER', password='THE_PASSWORD')
# Assign our input, output and error variables to
# to a command we will be issuing to the remote
# system
stdin, stdout, stderr = client.exec_command(
'find /path/data/ -name "pattern*" -mmin -15'
)
# We iterate over stdout
for line in stdout:
print('... ' + line.strip('
'))
# And finally we close the connection to our client
client.close()
正如 OP 所指出的,如果我们在本地已经有一个已知的 hosts 文件,我们可以做一些稍微不同的事情:
As pointed out by the OP, if we already have a known hosts file locally we can do things slightly different:
import base64
import paramiko
# And create a client instance.
client = paramiko.SSHClient()
# Create a 'host_keys' object and load
# our local known hosts
host_keys = client.load_system_host_keys()
# Connect to our client; you will need
# to know/use for the remote account:
#
# IP/Hostname of target
# A username
# A password
client.connect('IP_HOSTNAME', username='THE_USER', password='THE_PASSWORD')
# Assign our input, output and error variables to
# to a command we will be issuing to the remote
# system
stdin, stdout, stderr = client.exec_command(
'find /path/data/ -name "pattern*" -mmin -15'
)
# We iterate over stdout
for line in stdout:
print('... ' + line.strip('
'))
# And finally we close the connection to our client
client.close()
这篇关于如何使用 Python 列出远程主机目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Python 列出远程主机目录中的文件?
基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
