Skip to content

Commit

Permalink
Add capability to read resolv.conf for sysname
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish12pant committed Oct 24, 2024
1 parent c5301b2 commit 6225875
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/sonic_ax_impl/mibs/ietf/rfc1213.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,22 @@ def get_sys_name(self):
"""
Subclass update interface information
"""
# If hostname is already set to dot notation
if "." in self.hostname:
return self.hostname

try:
with open('/etc/resolv.conf', 'r') as fd_resolv:
for line in fd_resolv.readlines():
if 'search' in line:
domain = line.split()[1]
return "{}.{}".format(self.hostname, domain)

except Exception:
# If error in reading data from file
mibs.logger.warning("Cannot read domain from /etc/resolv.conf Using"
" {} in sysName".format(self.hostname))

return self.hostname


Expand Down
3 changes: 2 additions & 1 deletion tests/namespace/test_sysname.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_getpdu_sysname(self):
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.name), str(ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))))
self.assertEqual(str(value0.data), 'namespace_hostname')
hostname_value = str(value0.data).split(".")[0]
self.assertEqual(str(hostname_value), 'namespace_hostname')

@classmethod
def tearDownClass(cls):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_sysname.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ def test_getpdu_sysname(self):
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.name), str(ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))))
self.assertEqual(str(value0.data), 'test_hostname')
hostname_value = str(value0.data).split(".")[0]
self.assertEqual(str(hostname_value), 'test_hostname')

0 comments on commit 6225875

Please sign in to comment.