From fa8fc12e07c8f3544f8cc41d969696a0441da355 Mon Sep 17 00:00:00 2001 From: Stacy Smith Date: Mon, 13 Feb 2017 19:54:21 -0700 Subject: [PATCH 1/4] Implement SSH keepalives, with a default 30-second interval, for NETCONF over SSH sessions. Without SSH keepalives, a NAT or stateful firewall along the network path between the PyEZ host and the target Junos device, may timeout an inactive TCP flow and cause the NETCONF over SSH session to hang. Sending SSH keepalives avoids this situation. The default value is 30 seconds. Setting this parameter to a value of 0 disables SSH keepalives. Note: This is a different situation than Issue #663 in which the target Junos device is timing out the NETCONF over SSH session due to a configured idle-timeout on the system login class. --- lib/jnpr/junos/device.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index 136cbe1a0..952d74c32 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -866,6 +866,16 @@ def __init__(self, *vargs, **kvargs): This can be used to load SSH information from a configuration file. By default ~/.ssh/config is queried. + :param int ssh_keepalives: + *OPTIONAL* The interval, in seconds, at which to send SSH + keepalives for the NETCONF over SSH session. Without SSH + keepalives, a NAT or stateful firewall along the network + path between the PyEZ host and the target Junos device, + may timeout an inactive TCP flow and cause the NETCONF over SSH + session to hang. Sending SSH keepalives avoids this situation. The + default value is 30 seconds. Setting this parameter to a value of 0 + disables SSH keepalives. + :param bool normalize: *OPTIONAL* default is ``False``. If ``True`` then the XML returned by :meth:`execute` will have whitespace normalized @@ -897,6 +907,7 @@ def __init__(self, *vargs, **kvargs): self._hostname = 'localhost' self._ssh_private_key_file = None self._ssh_config = None + self._ssh_keepalives = None else: # -------------------------- # making a remote connection @@ -910,6 +921,7 @@ def __init__(self, *vargs, **kvargs): self._conf_ssh_private_key_file = None # user can get updated by ssh_config self._ssh_config = kvargs.get('ssh_config') + self._ssh_keepalives = kvargs.get('ssh_keepalives', 30) self._sshconf_lkup() # but if user or private key is explicit from call, then use it. self._auth_user = kvargs.get('user') or self._conf_auth_user or \ @@ -1008,6 +1020,8 @@ def open(self, *vargs, **kvargs): ssh_config=self._sshconf_lkup(), device_params={'name': 'junos', 'local': False}) + self._conn._session.transport.set_keepalive(self._ssh_keepalives) + except NcErrors.AuthenticationError as err: # bad authentication credentials raise EzErrors.ConnectAuthError(self) From fbfeda0c42bfa3cb143c47235f48ccfcf2a03ae8 Mon Sep 17 00:00:00 2001 From: Stacy Smith Date: Mon, 13 Feb 2017 20:01:42 -0700 Subject: [PATCH 2/4] Ensure the value passed to Paramiko is an integer. --- lib/jnpr/junos/device.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index 952d74c32..19030999b 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -1020,7 +1020,8 @@ def open(self, *vargs, **kvargs): ssh_config=self._sshconf_lkup(), device_params={'name': 'junos', 'local': False}) - self._conn._session.transport.set_keepalive(self._ssh_keepalives) + self._conn._session.transport.set_keepalive( + int(self._ssh_keepalives)) except NcErrors.AuthenticationError as err: # bad authentication credentials From 4e012651be41726f358e60bb642faf57cddfd621 Mon Sep 17 00:00:00 2001 From: rahkumar651991 <58289625+rahkumar651991@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:45:56 +0530 Subject: [PATCH 3/4] format file for black tool changes. --- lib/jnpr/junos/device.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index 41229d91a..a15a061df 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -1375,13 +1375,10 @@ def open(self, *vargs, **kvargs): }, ) self._conn._session.add_listener(DeviceSessionListener(self)) - if isinstance( - self._conn, - ncclient.manager.Manager) and isinstance( - self._conn._session, - ncclient.transport.ssh.SSHSession): - self._conn._session.transport.set_keepalive( - int(self._ssh_keepalives)) + if isinstance(self._conn, ncclient.manager.Manager) and isinstance( + self._conn._session, ncclient.transport.ssh.SSHSession + ): + self._conn._session.transport.set_keepalive(int(self._ssh_keepalives)) except NcErrors.AuthenticationError as err: # bad authentication credentials raise EzErrors.ConnectAuthError(self) From 6d5f00032e70fa02eba58cffd333e58ba2310c05 Mon Sep 17 00:00:00 2001 From: rahkumar651991 <58289625+rahkumar651991@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:51:13 +0530 Subject: [PATCH 4/4] format file for black tool recommendation --- lib/jnpr/junos/device.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index a15a061df..0dbcbf4b3 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -1259,8 +1259,8 @@ def __init__(self, *vargs, **kvargs): self._conf_auth_user = None self._conf_ssh_private_key_file = None # user can get updated by ssh_config - self._ssh_config = kvargs.get('ssh_config') - self._ssh_keepalives = kvargs.get('ssh_keepalives', 30) + self._ssh_config = kvargs.get("ssh_config") + self._ssh_keepalives = kvargs.get("ssh_keepalives", 30) self._sshconf_lkup() # but if user or private key is explicit from call, then use it.