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

Fix to use IPv6 linklocal address as snmp agent address #3215

Merged
merged 11 commits into from
Aug 5, 2024
7 changes: 5 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3235,7 +3235,10 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
"""Add the SNMP agent listening IP:Port%Vrf configuration"""

#Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|<port>|<vrf>
if not clicommon.is_ipaddress(agentip):
# Link local IP address should be provided along with zone id
# <link_local_ip>%<zone_id> for ex fe80::1%eth0
ip_addr = agentip.split('%')[0]
if not clicommon.is_ipaddress(ip_addr):
click.echo("Invalid IP address")
return False
config_db = ctx.obj['db']
Expand All @@ -3245,7 +3248,7 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
click.echo("ManagementVRF is Enabled. Provide vrf.")
return False
found = 0
ip = ipaddress.ip_address(agentip)
ip = ipaddress.ip_address(ip_addr)
for intf in netifaces.interfaces():
ipaddresses = netifaces.ifaddresses(intf)
if ip_family[ip.version] in ipaddresses:
Expand Down
28 changes: 28 additions & 0 deletions tests/config_snmp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,34 @@ def test_config_snmp_community_add_new_community_with_invalid_type_yang_validati
assert result.exit_code != 0
assert 'SNMP community configuration failed' in result.output

@patch('netifaces.interfaces', mock.Mock(return_value=['eth0']))
@patch('netifaces.ifaddresses', mock.Mock(return_value={2:
[{'addr': '10.1.0.32', 'netmask': '255.255.255.0',
'broadcast': '10.1.0.255'}],
10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]}))
@patch('os.system', mock.Mock(return_value=0))
def test_config_snmpagentaddress_add_linklocal(self):
db = Db()
obj = {'db': db.cfgdb}
runner = CliRunner()
runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1%eth0"], obj=obj)
assert ('fe80::1%eth0', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG')
assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0||") == {}

@patch('netifaces.interfaces', mock.Mock(return_value=['eth0']))
@patch('netifaces.ifaddresses', mock.Mock(return_value={2:
[{'addr': '10.1.0.32', 'netmask': '255.255.255.0',
'broadcast': '10.1.0.255'}],
10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]}))
@patch('os.system', mock.Mock(return_value=0))
def test_config_snmpagentaddress_add_ipv4(self):
db = Db()
obj = {'db': db.cfgdb}
runner = CliRunner()
runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["10.1.0.32"], obj=obj)
assert ('10.1.0.32', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG')
assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "i10.1.0.32||") == {}

@classmethod
def teardown_class(cls):
print("TEARDOWN")
Expand Down
Loading