Skip to content

IP address application examples

Amy Buck edited this page Nov 28, 2018 · 10 revisions

This information contains IP address application examples, including verification. base_ip.py contains examples for configuring an IP address, and deleting an IP address (see bin_ip.py).

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. Verify the return value.

ret = transaction.commit()
if not ret:
  raise RuntimeError ("Error configuring IP Address")

See configure_ip_address.py to view the Python application example.

Verify IP address configuring 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 configuration 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. Verify the return value.

ret = transaction.commit()
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
Clone this wiki locally