Skip to content

Commit 82ef3ec

Browse files
tapashdasjleveque
authored andcommitted
IP Address validation check (#530)
1 parent 1f326ad commit 82ef3ec

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

config/main.py

+29-18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import syslog
1111

1212
import sonic_device_util
13+
import ipaddress
1314
from swsssdk import ConfigDBConnector
1415
from swsssdk import SonicV2Connector
1516
from minigraph import parse_device_desc_xml
@@ -950,15 +951,20 @@ def add(ctx, interface_name, ip_addr):
950951
if interface_name is None:
951952
ctx.fail("'interface_name' is None!")
952953

953-
if interface_name.startswith("Ethernet"):
954-
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
955-
elif interface_name.startswith("PortChannel"):
956-
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
957-
elif interface_name.startswith("Vlan"):
958-
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
959-
elif interface_name.startswith("Loopback"):
960-
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
961-
954+
try:
955+
ipaddress.ip_network(unicode(ip_addr), strict=False)
956+
if interface_name.startswith("Ethernet"):
957+
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
958+
elif interface_name.startswith("PortChannel"):
959+
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
960+
elif interface_name.startswith("Vlan"):
961+
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
962+
elif interface_name.startswith("Loopback"):
963+
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
964+
else:
965+
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]")
966+
except ValueError:
967+
ctx.fail("'ip_addr' is not valid.")
962968

963969
#
964970
# 'del' subcommand
@@ -976,15 +982,20 @@ def remove(ctx, interface_name, ip_addr):
976982
if interface_name is None:
977983
ctx.fail("'interface_name' is None!")
978984

979-
if interface_name.startswith("Ethernet"):
980-
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
981-
elif interface_name.startswith("PortChannel"):
982-
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
983-
elif interface_name.startswith("Vlan"):
984-
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), None)
985-
elif interface_name.startswith("Loopback"):
986-
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), None)
987-
985+
try:
986+
ipaddress.ip_network(unicode(ip_addr), strict=False)
987+
if interface_name.startswith("Ethernet"):
988+
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
989+
elif interface_name.startswith("PortChannel"):
990+
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
991+
elif interface_name.startswith("Vlan"):
992+
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), None)
993+
elif interface_name.startswith("Loopback"):
994+
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), None)
995+
else:
996+
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]")
997+
except ValueError:
998+
ctx.fail("'ip_addr' is not valid.")
988999
#
9891000
# 'acl' group ('config acl ...')
9901001
#

0 commit comments

Comments
 (0)