Skip to content

Commit

Permalink
Merge pull request #53 from peletiah/master
Browse files Browse the repository at this point in the history
Added option for SFTP-authentication with public key
  • Loading branch information
jim-easterbrook committed Oct 16, 2017
2 parents 824f629 + 1489729 commit 83da0a6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/contributors/contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Rod Persky
Morten Høybye Frederiksen morten@mfd-consult.dk
Simon Josefsson simon@josefsson.org
Matthew Hilton matthilton2005@gmail.com
Sabine Tobolka oe1yvw@gmail.com
Sabine Tobolka oe1yvw@gmail.com
Markus Birth markus@birth-online.de
Chris Ramsay chris@ramsay-family.net
Christian Benke benkokakao@gmail.com

Translators
-----------
Expand Down
4 changes: 4 additions & 0 deletions src/doc/guides/weather_ini.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ Note that you may need to change the ``port`` value when you change to or from s

``user`` and ``password`` are the FTP site login details. Your web site provider should have provided them to you.

``privkey`` is the path to a private SSH-key_. For SFTP (secure FTP) this can be used for authentication instead of a password, which offers additional benefits in terms of security. When this is used the password-parameter can be left empty.

.. _SSH-key: https://www.ssh.com/ssh/public-key-authentication

``directory`` specifies where on the FTP site (or local file system) the files should be stored. Note that you may have to experiment with this a bit - you might need a '/' character at the start of the path.

.. versionadded:: 13.12.dev1120
Expand Down
29 changes: 25 additions & 4 deletions src/pywws/Upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,46 @@ def close(self):
self.ftp.close()

class _sftp(object):
def __init__(self, logger, site, user, password, directory, port):
def __init__(self, logger, site, user, password, privkey, directory, port):
global paramiko
import paramiko
self.logger = logger
self.site = site
self.user = user
self.password = password
self.privkey = privkey
self.directory = directory
self.port = port

def connect(self):
self.logger.info("Uploading to web site with SFTP")
self.transport = paramiko.Transport((self.site, self.port))
self.transport.connect(username=self.user, password=self.password)
self.transport.start_client()
if self.privkey:
self.get_private_key(self.privkey)
self.transport.auth_publickey(username=self.user, key=self.pkey)
else:
self.transport.auth_password(username=self.user, password=self.password)
self.ftp = paramiko.SFTPClient.from_transport(self.transport)
self.ftp.chdir(self.directory)


def put(self, src, dest):
self.ftp.put(src, dest)

def close(self):
self.ftp.close()
self.transport.close()

def get_private_key(self, privkey):
import StringIO
f = open(privkey, 'r')
s = f.read()
keyfile = StringIO.StringIO(s)
self.pkey = paramiko.RSAKey.from_private_key(keyfile)



class _copy(object):
def __init__(self, logger, directory):
self.logger = logger
Expand Down Expand Up @@ -178,13 +194,18 @@ def __init__(self, params):
# get remote site details
site = self.params.get('ftp', 'site', 'ftp.username.your_isp.co.uk')
user = self.params.get('ftp', 'user', 'username')
password = self.params.get('ftp', 'password', 'secret')
# we don't set a default password, as we might use a private ssh key
if self.params.get('ftp','password'):
password = self.params.get('ftp', 'password')
else:
password = ''
directory = self.params.get(
'ftp', 'directory', 'public_html/weather/data/')
if eval(self.params.get('ftp', 'secure', 'False')):
port = eval(self.params.get('ftp', 'port', '22'))
privkey = self.params.get('ftp', 'privkey')
self.uploader = _sftp(
self.logger, site, user, password, directory, port)
self.logger, site, user, password, privkey, directory, port)
else:
port = eval(self.params.get('ftp', 'port', '21'))
self.uploader = _ftp(
Expand Down

0 comments on commit 83da0a6

Please sign in to comment.