Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paramiko / scp - check if file exists on remote host

I'm using Python Paramiko and scp to perform some operations on remote machines. Some machines I work on require files to be available locally on their system. When this is the case, I'm using Paramiko and scp to copy the files across. For example:

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()

My question is, how can I check to see if 'localfile' exists on the remote machine before I try the scp?

I'd like to try and use Python commands where possible i.e. not bash

like image 409
Mark Avatar asked Apr 07 '16 22:04

Mark


People also ask

How do I check if a file exists on a remote server using python?

Here are a few options: test -e to see if any file exists (directory or regular file), test -f to see if it exists and is a regular file, or test -d to see if it exists and is a directory. This seems to break for paths including $HOME/test or ~/test .

How do I view a file in SSH?

SSH provides two different commands, which can be used to accomplish this. In order to search for a file location, you can use the find command. Find is a very powerful tool and accepts various arguments allowing you to specify the exact search term (i.e search by name, by type or even by modified time).

What is Paramiko SSHClient ()?

SSH client & key policies class paramiko.client. SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels.


2 Answers

Use paramiko's SFTP client instead. This example program checks for existence before copy.

#!/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")
like image 123
tdelaney Avatar answered Oct 03 '22 21:10

tdelaney


It should be possible to use only paramiko combined with 'test' command to check file existence. This doesn't require SFTP support:

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
like image 39
Roman Avatar answered Oct 03 '22 22:10

Roman