import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='test1234', password='test')
path = ['/home/test/*.txt', '/home/test1/*.file', '/home/check/*.xml']
for i in path:
for j in glob.glob(i):
print j
client.close()
我正在尝试使用 glob.glob
列出远程服务器上的通配 rune 件。但是 glob.glob()
不工作。
使用 Python 2.6。
远程服务器包含这些文件:/home/test1/check.file
, /home/test1/validate.file
, /home/test1/vali .文件
任何人都可以帮助解决这个问题。
最佳答案
glob
不会神奇地开始与远程服务器一起工作,只是因为您之前已经实例化了 SSHClient
。
您必须使用 Paramiko API 来列出文件,例如 SFTPClient.listdir
:
import fnmatch
sftp = client.open_sftp()
for filename in sftp.listdir('/home/test'):
if fnmatch.fnmatch(filename, "*.txt"):
print filename
如果更适合您的需要,您也可以使用正则表达式进行匹配。参见 Using wildcard in remote path using Paramiko's SFTPClient .
旁注:请勿使用 AutoAddPolicy
。你
这样做会失去安全感。参见 Paramiko "Unknown Server" .
关于python - 使用 Paramiko 在 Python 中列出与通配符匹配的 SFTP 服务器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49381899/