1818
1919from napalm import get_network_driver
2020from napalm .base .exceptions import ConnectionException , CommandErrorException
21+ import netaddr
22+ from netaddr .core import AddrFormatError
2123
2224from django .conf import settings
2325from django .utils .text import slugify
@@ -198,6 +200,38 @@ def get_platform_object_from_netbox(platform_slug):
198200
199201 return platform
200202
203+ def check_ip (self ):
204+ """Method to check if the IP address form field was an IP address.
205+
206+ If it is a DNS name, attempt to resolve the DNS address and assign the IP address to the
207+ name.
208+
209+ Returns:
210+ (bool): True if the IP address is an IP address, or a DNS entry was found and
211+ reassignment of the ot.ip_address was done.
212+ False if unable to find a device IP (error)
213+
214+ """
215+ # Assign checked_ip to None for error handling
216+ checked_ip = None
217+ try :
218+ # If successful, this is an IP address and can pass
219+ checked_ip = netaddr .IPNetwork (self .ot .ip_address )
220+ return True
221+ except AddrFormatError :
222+ pass
223+
224+ # An IP address was not detected in the IP address field, attempt to find DNS
225+ if checked_ip is None :
226+ try :
227+ # Do a lookup of name
228+ checked_ip = socket .gethostbyname (self .ot .ip_address )
229+ self .ot .ip_address = checked_ip
230+ return True
231+ except socket .gaierror :
232+ # DNS Lookup has failed, not a DNS, do not do anything
233+ return False
234+
201235 def get_required_info (
202236 self ,
203237 default_mgmt_if = PLUGIN_SETTINGS ["default_management_interface" ],
@@ -215,6 +249,8 @@ def get_required_info(
215249 OnboardException('fail-general'):
216250 Any other unexpected device comms failure.
217251 """
252+ # Check to see if the IP address entered was an IP address or a DNS entry, get the IP address
253+ self .check_ip ()
218254 self .check_reachability ()
219255 mgmt_ipaddr = self .ot .ip_address
220256
0 commit comments