Posts: 1,150
Threads: 42
Joined: Sep 2016
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
Posts: 12,023
Threads: 484
Joined: Sep 2016
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
Posts: 5,151
Threads: 396
Joined: Sep 2016
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:
Posts: 1,150
Threads: 42
Joined: Sep 2016
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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
FTP
I can't write a post shorter than 5 characters so I wrote this
Posts: 7,312
Threads: 123
Joined: Sep 2016
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.
Posts: 1,150
Threads: 42
Joined: Sep 2016
@ 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
Posts: 1,150
Threads: 42
Joined: Sep 2016
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!
|