File: sftpclient.py

package info (click to toggle)
libmina-sshd-java 2.13.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,428 kB
  • sloc: java: 136,607; xml: 4,544; sh: 917; python: 239; makefile: 2
file content (407 lines) | stat: -rw-r--r-- 11,690 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

'''
Simple wrapper for Paramiko SFTP client (see http://www.paramiko.org/)
'''

import getpass
import json
import os
import signal
import sys

import paramiko


VERSION='1.0'

# -----------------------------------------------------------------------------------------

def die(msg=None,rc=1):
    """
    Cleanly exits the program with an error message
    """

    if msg:
        print(msg)

    sys.exit(rc)

# ----------------------------------------------------------------------------

def isEmpty(s):
    if (s is None) or (len(s) <= 0):
        return True
    else:
        return False

# ----------------------------------------------------------------------------

def isNumberString(value):
    """
    Checks if value is a string that has only digits - possibly with leading '+' or '-'
    """
    if not value:
        return False

    sign = value[0]
    if (sign == '+') or (sign == '-'):
        if len(value) <= 1:
            return False

        absValue = value[1:]
        return absValue.isdigit()
    else:
        if len(value) <= 0:
            return False
        else:
            return value.isdigit()

def isNumberValue(value):
    return isinstance(value, (int, float))

# ----------------------------------------------------------------------------

def isFloatingPointString(value):
    """
    Checks if value is a string that has only digits - possibly with leading '+' or '-' - AND a single dot
    """
    if isEmpty(value):
        return False

    sign = value[0]
    if (sign == '+') or (sign == '-'):
        if len(value) <= 1:
            return False

        absValue = value[1:]
    else:
        absValue = value

    dotPos = absValue.find('.')
    # Must have a dot and it cannot be the last character
    if (dotPos < 0) or (dotPos == (len(absValue) - 1)):
        return False

    # Must have EXACTLY one dot
    dotCount = absValue.count('.')
    if dotCount != 1:
        return False

    # Make sure both sides of the dot are integer numbers
    intPart = absValue[0:dotPos]
    if not isNumberString(intPart):
        return False

    facPart = absValue[dotPos + 1:]
    # Do not allow 123.-5
    sign = facPart[0]
    if (sign == '+') or (sign == '-'):
        return False

    if not isNumberString(facPart):
        return False

    return True

# ----------------------------------------------------------------------------

def normalizeValue(value):
    """
    Checks if value is 'True', 'False' or all numeric and converts it accordingly
    Otherwise it just returns it

    Args:
        value (str) - String value
    """

    if not value:
        return value

    loCase = value.lower()
    if loCase == "none":
        return None
    elif loCase == "true":
        return True
    elif loCase == "false":
        return False
    elif isNumberString(loCase):
        return int(loCase)
    else:
        return value

# ----------------------------------------------------------------------------

def parseCommandLineArguments(args):
    """
    Parses an array of arguments having the format: --name=value. If
    only --name is provided then it is assumed to a TRUE boolean value.
    If the value is all digits, then it is assumed to be a number.

    If the same key is specified more than once, then a list of
    the accumulated values is created. The result is a dictionary
    with the names as the keys and value as their mapped values

    Args:
        args (str[]) - The command line arguments to parse
    """

    valsMap = {}
    if len(args) <= 0:
        return valsMap

    for item in args:
        if not item.startswith("--"):
            raise Exception("Missing option identifier: %s" % item)

        propPair = item[2:]     # strip the prefix
        sepPos = propPair.find('=')

        if sepPos == 0:
            raise Exception("Missing name: %s" % item)
        if sepPos >= (len(propPair) - 1):
            raise Exception("Missing value: %s" % item)

        propName = propPair
        propValue = None
        if sepPos < 0:
            propValue = True
        else:
            propName = propPair[0:sepPos]
            propValue = normalizeValue(propPair[sepPos + 1:])

        if propName in valsMap:
            curValue = valsMap[propName]
            if not isinstance(curValue, list):
                curValue = [ curValue ]
            curValue.append(propValue)
            valsMap[propName] = curValue
        else:
            valsMap[propName] = propValue

    return valsMap

# ----------------------------------------------------------------------------

def resolvePathVariables(path):
    """
    Expands ~/xxx and ${XXX} variables
    """
    if isEmpty(path):
        return path

    path = os.path.expanduser(path)
    path = os.path.expandvars(path)
    return path

# ----------------------------------------------------------------------------

def _decode_list(data):
    # can happen for internal sub-lists of objects
    if isinstance(data, dict):
        return _decode_dict(data)

    rv = []
    for item in data:
        if isinstance(item, list):
            item = _decode_list(item)
        elif isinstance(item, dict):
            item = _decode_dict(item)
        rv.append(item)
    return rv

# ----------------------------------------------------------------------------

def _decode_dict(data):
    # can happen for internal sub-lists of objects
    if isinstance(data, list):
        return _decode_list(data)

    rv = {}
    for key, value in data.items():
        if isinstance(value, list):
            value = _decode_list(value)
        elif isinstance(value, dict):
            value = _decode_dict(value)
        rv[key] = value

    return rv

# ----------------------------------------------------------------------------

def loadJsonFile(configFile):
    if isEmpty(configFile):
        return {}

    with open(configFile) as config_file:
        return json.load(config_file, object_hook=_decode_dict);

# ----------------------------------------------------------------------------

def createSftpClient(args):
    host = args.get("host", "localhost")
    port = args.get("port", 22)
    username = args.get("username", None)
    if isEmpty(username):
        username = getpass.getuser()
    password = args.get("password", None)
    keyfile = args.get("keyFile", None)
    keytype = args.get("keyType", "RSA")

    sftp = None
    transport = None
    try:
        key = None
        if keyfile is not None:
            # Get private key used to authenticate user.
            if keytype == 'DSA':
                # The private key is a DSA type key.
                key = paramiko.DSSKey.from_private_key_file(keyfile)
            else:
                # The private key is a RSA type key.
                key = paramiko.RSAKey.from_private_key(keyfile)

        # Create Transport object using supplied method of authentication.
        transport = paramiko.Transport((host, port))
        transport.connect(None, username, password, key)
 
        sftp = paramiko.SFTPClient.from_transport(transport)
        return sftp
    except Exception as e:
        print('An error occurred creating SFTP client: %s: %s' % (e.__class__, e))

        if sftp is not None:
            try:
                sftp.close()
            except Exception as err:
                print('Failed to close SFTP client: %s: %s' % (err.__class__, err))

        if transport is not None:
            try:
                transport.close()
            except Exception as err:
                print('Failed to close transport: %s: %s' % (err.__class__, err))

        raise e
    
# =========================================================================================

def doList(sftp, curdir, argsList):
    dirPath = curdir;
    if not isEmpty(argsList):
        dirPath = argsList.pop(0)
        dirPath = dirPath.strip()
        dirPath = os.path.join(curdir, dirPath)

    # Also available: listdir_attr, listdir
    dirlist = sftp.listdir_iter(path=dirPath)
    for row in dirlist:
        # see https://docs.paramiko.org/en/2.6/api/sftp.html#paramiko.sftp_attr.SFTPAttributes
        print("    %s" % str(row))

def doChdir(sftp, homedir, curdir, argsList):
    dirPath = homedir
    if not isEmpty(argsList):
        dirPath = argsList.pop(0)
        dirPath = dirPath.strip()
        dirPath = os.path.join(curdir, dirPath)
    sftp.chdir(dirPath)

# ----------------------------------------------------------------------------

# see https://github.com/paramiko/paramiko/blob/master/demos/demo_sftp.py
# see https://docs.paramiko.org/en/2.6/api/sftp.html
def doSftp(sftp):
    homedir = sftp.normalize('.')
    sftp.chdir(homedir)
    
    while True:
        curdir = sftp.getcwd()
        sys.stdout.write("%s > " % curdir)
        sys.stdout.flush()
        l = sys.stdin.readline()
        l = l.strip()
    
        if isEmpty(l):
            continue
        
        argsList = l.split(' ')
        op = argsList.pop(0)
        
        if (op == "quit") or (op == "exit") or (op == "bye"):
            break
        elif (op == "ls") or (op == "list"):
            doList(sftp, curdir, argsList)
        elif (op == "cd"):
            doChdir(sftp, homedir, curdir, argsList)
        # TODO get_channel()
        #    show info using get_transport() on it
        #    get_security_options() on transport
        else:
            print("Unknown command: %s" % l)

def doMain(args):
    sftp = createSftpClient(args)
    try:
        doSftp(sftp);
    except Exception as e:
        print('An error occurred using the SFTP client: %s: %s' % (e.__class__, e))
        raise e
    finally:
        sftp.close()

#
# Usage: python3 sftpclient.py --arg1=value1 --arg2=value2 ...
#
# Where available arguments are:
#
#    * host - default=localhost
#    * port - default=22
#    * username - the login user - default=currently logged in user
#    * password - the password - can be omitted if key file used
#    * keyFile - path to key file
#    * keyType - type of key in file (RSA/DSA) - default=RSA
def main(args):
    if len(args) > 0:
        subArgs = parseCommandLineArguments(args)
    else:
        subArgs = {}
    doMain(subArgs)
    sys.exit(0)
    
# ----------------------------------------------------------------------------

def signal_handler(signal, frame):
    die('Exit due to Control+C')

if __name__ == "__main__":
    pyVersion = sys.version_info
    if pyVersion.major != 3:
        die("Major Python version must be 3.x: %s" % str(pyVersion))
    if pyVersion.minor < 0:
        print("Warning: minor Python version %s should be at least 3.0+" % str(pyVersion))

    signal.signal(signal.SIGINT, signal_handler)
    if os.name == 'nt':
        print("Use Ctrl+Break to stop the script")
    else:
        print("Use Ctrl+C to stop the script")
    main(sys.argv[1:])