IP Address Application Examples - amybuck/SONiC-NAS GitHub Wiki

Configure IP Address using Python

1. Import the CPS object Python library.

import cps_utils

2. Define the ifindex and prefix length of the interface to set the IP address.

ifindex=16 ip_addr="10.0.0.1" 
pfix_len=16 
ip_attributes = {"base-ip/ipv4/ifindex": ifindex,"ip":ip_addr,"prefix-length":pfix_len}

3. Define the attribute type to convert the IP address between string and byte-array format.

cps_utils.add_attr_type('base-ip/ipv4/address/ip',"ipv4") 
cps_obj=cps_utils.CPSObject('base-ip/ipv4/address',data=ip_attributes)

4. Create the CPS transaction.

cps_update = ('create', cps_obj.get())

5. Add the CPS operation and object pair to a new transaction.

transaction = cps_utils.CPSTransaction([cps_update])

6. Commit the transaction.

ret = transaction.commit()

7. Verify the return value.

if not ret:
    raise RuntimeError ("Error configuring IP Address")

See configure-ip-address.py to view the Python application example.

Verify IP Address Configuration using CPS get

# cps_get_oid.py 'base-ip/ipv4/address'

Key: 1.34.2228241.2228246.2228236.2228240.2228228.
base-ip/ipv4/address/prefix-length = 16
base-ip/ipv4/vrf-id = 0
base-ip/ipv4/name = e101-001-0
base-ip/ipv4/ifindex = 16
base-ip/ipv4/address/ip = 0a000001

Verify IP Address Configuring using Linux

$ ip addr show e101-001-0
16: e101-001-0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 500
     link/ether 90:b1:1c:f4:aa:b3 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.1/16 scope global e101-001-0 valid_lft forever preferred_lft forever

Delete IP Address using Python

1. Import the CPS object Python library.

import cps_utils

2. Define the ifindex, IP address, and prefix length of the interface to delete.

idx=16 ip_addr="10.0.0.1" 
pfix_len=16 
ip_attributes = {"base-ip/ipv4/ifindex":idx,"ip":ip_addr,"prefix-length":pfix_len}

3. Define the attribute type to convert the IP address between string and byte-array format.

cps_utils.add_attr_type('base-ip/ipv4/address/ip',"ipv4") 
cps_obj=cps_utils.CPSObject('base-ip/ipv4/address',data=ip_attributes)

4. Create the CPS transaction.

cps_update = ('create', cps_obj.get())

5. Add the object pair to a new CPS transaction.

transaction = cps_utils.CPSTransaction([cps_update])

6. Commit the transaction.

ret = transaction.commit()

7. Verify the return value.

if not ret:
    raise RuntimeError ("Error creating Vlan")

See delete-ip-address.py to view the Python application example.

Verify IP Address Deletion using CPS get

# cps_get_oid.py 'base-ip/ipv4/address'

The return indicates that e101-001-0 has no IP address.

Verify IP Address Deletion using Linux

$ ip addr show e101-001-0
16: e101-001-0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 500
    link/ether 90:b1:1c:f4:aa:b3 brd ff:ff:ff:ff:ff:ff