Paramiko / scp - 检查远程主机上是否存在文件
Paramiko / scp - check if file exists on remote host
我正在使用 Python Paramiko 和 scp 在远程机器上执行一些操作。我工作的一些机器要求文件在他们的系统上本地可用。在这种情况下,我使用 Paramiko 和 scp 来复制文件。例如:
from paramiko import SSHClient
from scp import SCPClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('192.168.100.1')
scp = SCPClient(ssh.get_transport())
scp.put('localfile', 'remote file')
scp.close()
ssh.close()
我的问题是,在尝试 scp 之前如何检查 'localfile' 是否存在于远程机器上?
我想尽可能使用 Python 命令,即不 bash
改用 paramiko 的 SFTP 客户端。此示例程序在复制前检查是否存在。
#!/usr/bin/env python
import paramiko
import getpass
# make a local test file
open('deleteme.txt', 'w').write('you really should delete this]n')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('localhost', username=getpass.getuser(),
password=getpass.getpass('password: '))
sftp = ssh.open_sftp()
sftp.chdir("/tmp/")
try:
print(sftp.stat('/tmp/deleteme.txt'))
print('file exists')
except IOError:
print('copying file')
sftp.put('deleteme.txt', '/tmp/deleteme.txt')
ssh.close()
except paramiko.SSHException:
print("Connection Error")
应该可以仅使用 paramiko 结合 'test' 命令来检查文件是否存在。这不需要 SFTP 支持:
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
我正在使用 Python Paramiko 和 scp 在远程机器上执行一些操作。我工作的一些机器要求文件在他们的系统上本地可用。在这种情况下,我使用 Paramiko 和 scp 来复制文件。例如:
from paramiko import SSHClient
from scp import SCPClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('192.168.100.1')
scp = SCPClient(ssh.get_transport())
scp.put('localfile', 'remote file')
scp.close()
ssh.close()
我的问题是,在尝试 scp 之前如何检查 'localfile' 是否存在于远程机器上?
我想尽可能使用 Python 命令,即不 bash
改用 paramiko 的 SFTP 客户端。此示例程序在复制前检查是否存在。
#!/usr/bin/env python
import paramiko
import getpass
# make a local test file
open('deleteme.txt', 'w').write('you really should delete this]n')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('localhost', username=getpass.getuser(),
password=getpass.getpass('password: '))
sftp = ssh.open_sftp()
sftp.chdir("/tmp/")
try:
print(sftp.stat('/tmp/deleteme.txt'))
print('file exists')
except IOError:
print('copying file')
sftp.put('deleteme.txt', '/tmp/deleteme.txt')
ssh.close()
except paramiko.SSHException:
print("Connection Error")
应该可以仅使用 paramiko 结合 'test' 命令来检查文件是否存在。这不需要 SFTP 支持:
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