jbardin / scp.py

scp module for paramiko

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle checking multiple locations for file to get?

axfelix opened this issue · comments

Hi,

I have a (very lazy) example of when I might need to check two different places for a file on a server, like so:

dip_path_1 = '/var/archivematica/sharedDirectory/watchedDirectories/uploadedDIPs/' + transfer_name + "*/"
dip_path_2 = '/var/archivematica/sharedDirectory/watchedDirectories/uploadDIP/' + transfer_name + "*/"
desktop_path = (os.path.expanduser("~/Desktop")  + "/" + transfer_name)
with SCPClient(ssh.get_transport(), sanitize=dummy_sanitizer) as scp:
	try:
		scp.get(dip_path_1, desktop_path, recursive=True)
	except scp.SCPException:
		scp.get(dip_path_2, desktop_path, recursive=True)

However, paramiko doesn't seem to like this -- if the first path doesn't work, I get:

Traceback (most recent call last):
  File "dip-retrieve.py", line 29, in <module>
    scp.get(dip_path_2, desktop_path, recursive=True)
  File "/usr/lib/python2.7/site-packages/scp.py", line 216, in get
    b' '.join(remote_path))
  File "/usr/lib/python2.7/site-packages/paramiko/channel.py", line 63, in _check
    return func(self, *args, **kwds)
  File "/usr/lib/python2.7/site-packages/paramiko/channel.py", line 241, in exec_command
    self._wait_for_event()
  File "/usr/lib/python2.7/site-packages/paramiko/channel.py", line 1198, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.

Is there a better way to handle this without having to check for the file on the server through a separate SSH connection?

The channel gets closed after a command, so you have to do:

try:
    with SCPClient(ssh.get_transport(), sanitize=dummy_sanitizer) as scp:
        scp.get(dip_path_1, desktop_path, recursive=True)
except scp.SCPException:
    with SCPClient(ssh.get_transport(), sanitize=dummy_sanitizer) as scp:
        scp.get(dip_path_2, desktop_path, recursive=True)

Fixing this behavior is already tracked at #105.