Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bind_addr parameter in Device() API #1340

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,10 @@ def __init__(self, *vargs, **kvargs):
*OPTIONAL* To disable public key authentication.
default is ``None``.

:param str bind_addr:
*OPTIONAL* To use (local) source IP address.
default is ``None``.

:param bool hostkey_verify:
*OPTIONAL* To enable ssh_known hostkey verify
default is ``False``.
Expand All @@ -1239,6 +1243,7 @@ def __init__(self, *vargs, **kvargs):
self._huge_tree = kvargs.get("huge_tree", False)
self._conn_open_timeout = kvargs.get("conn_open_timeout", 30)
self._look_for_keys = kvargs.get("look_for_keys", None)
self._bind_addr = kvargs.get("bind_addr", None)
self._hostkey_verify = kvargs.get("hostkey_verify", False)
if self._fact_style != "new":
warnings.warn(
Expand Down Expand Up @@ -1393,6 +1398,7 @@ def open(self, *vargs, **kvargs):
allow_agent=allow_agent,
look_for_keys=look_for_keys,
ssh_config=self._sshconf_lkup(),
bind_addr=self._bind_addr,
timeout=self._conn_open_timeout,
device_params={
"name": "junos",
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,23 @@ def test_device_open(self, mock_connect, mock_execute):
self.dev2.open()
self.assertEqual(self.dev2.connected, True)

@patch("ncclient.manager.connect")
@patch("jnpr.junos.Device.execute")
def test_device_open_with_bind_addr(self, mock_connect, mock_execute):
with patch("jnpr.junos.utils.fs.FS.cat") as mock_cat:
mock_cat.return_value = """

domain jls.net

"""
mock_connect.side_effect = self._mock_manager
mock_execute.side_effect = self._mock_manager
self.dev2 = Device(
host="2.2.2.2", user="test", password="password123", bind_addr="1.1.1.1"
)
self.dev2.open()
self.assertEqual(self.dev2.connected, True)

@patch("ncclient.manager.connect")
@patch("jnpr.junos.Device.execute")
def test_device_open_with_look_for_keys_False(self, mock_connect, mock_execute):
Expand Down Expand Up @@ -496,6 +513,7 @@ def test_device_open_with_look_for_keys_True(self, mock_connect, mock_execute):
)
self.dev2.open()
self.assertEqual(self.dev2.connected, True)

@patch("ncclient.manager.connect")
@patch("jnpr.junos.Device.execute")
def test_device_open_with_hostkey_verify_True(self, mock_connect, mock_execute):
Expand Down Expand Up @@ -525,7 +543,10 @@ def test_device_open_with_hostkey_verify_False(self, mock_connect, mock_execute)
mock_connect.side_effect = self._mock_manager
mock_execute.side_effect = self._mock_manager
self.dev2 = Device(
host="2.2.2.2", user="test", password="password123", hostkey_verify=False
host="2.2.2.2",
user="test",
password="password123",
hostkey_verify=False,
)
self.dev2.open()
self.assertEqual(self.dev2.connected, True)
Expand Down
Loading