擅长:python、mysql、java
<p>应该可以只使用paramiko和'test'命令来检查文件是否存在。这不需要SFTP支持:</p>
<pre><code>from paramiko import SSHClient
ip = '127.0.0.1'
file_to_check = '/tmp/some_file.txt'
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(ip)
stdin, stdout, stderr = ssh.exec_command('test -e {0} && echo exists'.format(file_to_check))
errs = stderr.read()
if errs:
raise Exception('Failed to check existence of {0}: {1}'.format(file_to_check, errs))
file_exits = stdout.read().strip() == 'exists'
print file_exits
</code></pre>