10
10
import syslog
11
11
12
12
import sonic_device_util
13
+ import ipaddress
13
14
from swsssdk import ConfigDBConnector
14
15
from swsssdk import SonicV2Connector
15
16
from minigraph import parse_device_desc_xml
@@ -950,15 +951,20 @@ def add(ctx, interface_name, ip_addr):
950
951
if interface_name is None :
951
952
ctx .fail ("'interface_name' is None!" )
952
953
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." )
962
968
963
969
#
964
970
# 'del' subcommand
@@ -976,15 +982,20 @@ def remove(ctx, interface_name, ip_addr):
976
982
if interface_name is None :
977
983
ctx .fail ("'interface_name' is None!" )
978
984
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." )
988
999
#
989
1000
# 'acl' group ('config acl ...')
990
1001
#
0 commit comments