| 1 |
from __future__ import absolute_import |
| 2 |
import inspect |
| 3 |
|
| 4 |
from scp import SCPClient |
| 5 |
from jnpr.junos.utils.ssh_client import open_ssh_client |
| 6 |
|
| 7 |
""" |
| 8 |
Secure Copy Utility |
| 9 |
""" |
| 10 |
|
| 11 |
|
| 12 |
class SCP(object): |
| 13 |
""" |
| 14 |
The SCP utility is used to conjunction with :class:`jnpr.junos.utils.sw.SW` |
| 15 |
when transferring the Junos image to the device. The :class:`SCP` can be |
| 16 |
used for other secure-copy use-cases as well; it is implemented to support |
| 17 |
the python *context-manager* pattern. For example:: |
| 18 |
|
| 19 |
from jnpr.junos.utils.scp import SCP |
| 20 |
|
| 21 |
with SCP(dev, progress=True) as scp: |
| 22 |
scp.put(package, remote_path) |
| 23 |
|
| 24 |
""" |
| 25 |
|
| 26 |
def __init__(self, junos, **scpargs): |
| 27 |
""" |
| 28 |
Constructor that wraps :py:mod:`paramiko` and :py:mod:`scp` objects. |
| 29 |
|
| 30 |
:param Device junos: the Device object |
| 31 |
:param kvargs scpargs: any additional args to be passed to paramiko SCP |
| 32 |
""" |
| 33 |
self._junos = junos |
| 34 |
if self._junos.__dict__.get("_mode") is not None: |
| 35 |
raise RuntimeError("SCP is not supported with Console mode") |
| 36 |
self._scpargs = scpargs |
| 37 |
self._by10pct = 0 |
| 38 |
self._user_progress = self._scpargs.get("progress") |
| 39 |
self._ssh = None |
| 40 |
if self._user_progress is True: |
| 41 |
self._scpargs["progress"] = self._scp_progress |
| 42 |
elif callable(self._user_progress): |
| 43 |
# User case also define progress with 3 params, the way scp module |
| 44 |
# expects. Function will take path, total size, transferred. |
| 45 |
# https://github.com/jbardin/scp.py/blob/master/scp.py#L97 |
| 46 |
spec = inspect.getfullargspec(self._user_progress) |
| 47 |
if (len(spec.args) == 3 and spec.args[0] != "self") or ( |
| 48 |
len(spec.args) == 4 and spec.args[0] == "self" |
| 49 |
): |
| 50 |
self._scpargs["progress"] = self._user_progress |
| 51 |
else: |
| 52 |
# this will override the function _progress defined for this |
| 53 |
# class to use progress provided by user. |
| 54 |
self._progress = lambda report: self._user_progress(self._junos, report) |
| 55 |
self._scpargs["progress"] = self._scp_progress |
| 56 |
|
| 57 |
def _progress(self, report): |
| 58 |
"""simple progress report function""" |
| 59 |
print(self._junos.hostname + ": " + report) |
| 60 |
|
| 61 |
def _scp_progress(self, _path, _total, _xfrd): |
| 62 |
# calculate current percentage xferd |
| 63 |
pct = int(float(_xfrd) / float(_total) * 100) |
| 64 |
|
| 65 |
# if 10% more has been copied, then print a message |
| 66 |
if 0 == (pct % 10) and pct != self._by10pct: |
| 67 |
self._by10pct = pct |
| 68 |
self._progress("%s: %s / %s (%s%%)" % (_path, _xfrd, _total, str(pct))) |
| 69 |
|
| 70 |
def open(self, **scpargs): |
| 71 |
""" |
| 72 |
Creates an instance of the scp object and return to caller for use. |
| 73 |
|
| 74 |
.. note:: This method uses the same username/password authentication |
| 75 |
credentials as used by :class:`jnpr.junos.device.Device`. |
| 76 |
It can also use ``ssh_private_key_file`` option if provided |
| 77 |
to the :class:`jnpr.junos.device.Device` |
| 78 |
|
| 79 |
:returns: SCPClient object |
| 80 |
""" |
| 81 |
# @@@ should check for multi-calls to connect to ensure we don't keep |
| 82 |
# @@@ opening new connections |
| 83 |
self._ssh = open_ssh_client(dev=self._junos) |
| 84 |
return SCPClient(self._ssh.get_transport(), **scpargs) |
| 85 |
|
| 86 |
def close(self): |
| 87 |
""" |
| 88 |
Closes the ssh/scp connection to the device |
| 89 |
""" |
| 90 |
self._ssh.close() |
| 91 |
|
| 92 |
# ------------------------------------------------------------------------- |
| 93 |
# CONTEXT MANAGER |
| 94 |
# ------------------------------------------------------------------------- |
| 95 |
|
| 96 |
def __enter__(self): |
| 97 |
return self.open(**self._scpargs) |
| 98 |
|
| 99 |
def __exit__(self, exc_ty, exc_val, exc_tb): |
| 100 |
self.close() |