Python Forum
Copy directory (+all contents) via SSH
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy directory (+all contents) via SSH
#1
Hello all,

I need to copy a directory, with all the subdirectories and files it contains, to a remote device via SSH. Just like ctrl+c - ctrl+v would do on a local Windows machine.

I came up with this piece of code, which uses pysftp module:

import pysftp

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

with pysftp.Connection(remote_ip, username, password, cnopts=cnopts) as sftp:
    sftp.put_r(local_dir_path, remote_dir_path)
It gives a rather funny result. All the files and directories get copied to a top level directory (remote_dir_path). So the structure is not at all retained. Besides, all the copied files and directories get a prepend  ".\".

Does anyone have any idea how to fix this issue? I am also open to using alternative solutions/modules. However it does have to use SSH.
Thank you!
Best, JC
Reply
#2
I haven't used it, so can't vouch for it one way or the other, but this looks like
it does what you're looking for: https://pypi.python.org/pypi/collective....sync/2.2.2
Reply
#3
Quote:I need to copy a directory, with all the subdirectories and files it contains, to a remote device via SSH.
Does it have to be python? I just use ssh commands to copy to and from everything....including this server

#secure copy from local to server
scp [FILE] [SERVER_USERNAME]@[URL]:/home/[USERNAME]

#secure copy from server to local
scp [FILE] [SERVER_USERNAME]@[URL]:/[SERVER_FILEPATH] [LOCAL_FILEPATH]
this will copy directory_name and all nested from local to server directory /home/username assuming port 80 is opened
ssh -r directory_name [email protected]_site.com:/home/username
Recommended Tutorials:
Reply
#4
Thanks for replies

@Larz60+
Part of description on the site says:

Quote:collective.recipe.rsync assumes you have a UNIX-based operating system and the rsync binary is in your PATH when you run Buildout or the rsync script.

I didn't get around to try it yet, but I'm afraid things will get stuck here, because I want to transfer files from Windows to Linux machine. That is a detail I should have mentioned in the original post already.

@metulburr
Yes, it is going to be a script for processes automation, so I am doing it in Python. So far I used MobaXterm which has a nice file explorer interface. There you can just drag and drop files from Windows, like magic (SSH). So I am wondering how to go about doing that with Python, instead of doing it manually.
Reply
#5
FTP
I can't write a post shorter than 5 characters so I wrote this
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Quote: So I am wondering how to go about doing that with Python, instead of doing it manually.
Fabric(port to 3) or Ansible
There is also a rsync_projec in Fabric.
from fabric.contrib.project import rsync_project

def _deploy_ec2(loc):
   rsync_project(local_dir=loc, remote_dir='/var/www', exclude='.git')
from fabric.api import env
from fabric.operations import run, put

env.hosts = ['host']
def copy():   
    run('mkdir -p /home/bar/tmp') 
    put('testdirectory', '/home/bar/tmp')
For a better cmd use cmder.
Then all stuff like ssh,scp,rsync work out of the box.
Reply
#7
@snippsat
Thanks for introducing Fabric to me. The name sounded vaguely familiar from somewhere, but I never explored the module.
rsync_project looks very promising with what I'm trying to achieve. But currently I am stuck with import errors with both examples you showed. rsync_project import error:

Error:
from fabric.contrib.project import rsync_project Traceback (most recent call last):   File "<input>", line 1, in <module>   File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import     module = self._system_import(name, *args, **kwargs)   File "C:\py\lib\site-packages\fabric\contrib\project.py", line 10, in <module>     from fabric.network import needs_host, key_filenames, normalize   File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import     module = self._system_import(name, *args, **kwargs)   File "C:\py\lib\site-packages\fabric\network.py", line 25     except ImportError, e:                       ^ SyntaxError: invalid syntax
Reply
#8
Obviously, I was being too careless... I installed Fabric instead of Fabric3 (Python 3 port). I will continue playing with the examples and see what I can come up with, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to download the contents of a web directory. Pedroski55 2 1,348 Feb-11-2022, 12:41 PM
Last Post: Pedroski55
  A better way to search the contents of a directory? alloydog 5 2,614 Apr-11-2021, 09:12 AM
Last Post: alloydog
  Copy files from subfolders into same name of subfolders at other directory rathoreanil 1 2,304 Oct-12-2020, 01:30 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020