-
Notifications
You must be signed in to change notification settings - Fork 35
IP address application examples
This information describes how to configure an IP address, and delete an IP address using Python. Verification examples are included.
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.
# 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
$ 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
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.
# cps_get_oid.py 'base-ip/ipv4/address'
The return indicates that e101-001-0 has no IP address.
$ 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
© 2019 OpenSwitch project. All information is contributed to and made available by OPX under the Creative Commons Attribution 4.0 International License (available at http://creativecommons.org/licenses/by/4.0/).
- Home
- System overview
- Software releases
- Hardware support
- FAQs
- Install
- Build
- Configure
- Develop
- Administer
- Troubleshoot
- Software compatibility
- Contribute
- DevOps
- Join