python sprur ssh检查文件是否存在

2024-05-19 17:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python中的spur来ssh到linux服务器,它正在做我想让它做的一切,除了,我不确定使用spur ssh到linux服务器并检查该服务器上是否存在文件的最佳方法。我现在是这样做的,但是速度很慢,有没有更快的方法?在

import spur
shell = spur.SshShell(hostname="192.168.0.22", username="username", password="password", missing_host_key=spur.ssh.MissingHostKey.accept)
inFile = "/home/myFolder/Desktop/fileNmae.avi"
try:
    result = shell.run(["ls", inFile])
    print "Pass"
except:
    print "Fail"

Tags: 文件方法import服务器linuxusernamepasswordshell
1条回答
网友
1楼 · 发布于 2024-05-19 17:38:58

您可以使用我们开发的spurplus,因为我们发现paramiko和{a2}都有点太低了。以下是如何检查文件是否存在的示例:

import spurplus

with spurplus.connect_with_retries(
        hostname='some-machine.example.com', username='devop') as shell:
    file_exists = shell.exists("/path/to/some/file")

    pth = pathlib.Path("/path/to/another/file")
    another_file_exists = shell.exists(pth)

相关问题 更多 >