cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1547
Views
0
Helpful
5
Replies

CREATING MULTIPLE LOOPBACK INTERFACES ON A ROUTER USING PYTHON NOT WORKING

Can someone tell me why this code won't work? Please see the code and error output:

Screenshot (79).pngScreenshot (78).png

5 Replies 5

Hi Piotr,

 

Your problem seems to be that you are using a JSON structure as a string into your request, to give you an idea of how you can fix it by either using a string or a JSON payload, I am going to write down here the code for both options.

 

Both of these options uses a reserved Sandbox from Devnet with IOS-XE code 16.9, you can easily change to another host with the topmost variables 

 

Code using String Payload:

Here the catch is to use a string with the-string-format-method and escape the curly brackets where needed in the final string by doubling them ( { = {{ and } = }} )

 

import requests
import urllib3

urllib3.disable_warnings()

credentials = ("developer","C1sco12345")

headers = {"Accept":"application/yang-data+json","Content-Type":"application/yang-data+json"}

host = "10.10.20.48"
port = "443"
yang_model_ifc = "ietf-interfaces"
container_ifc = "interfaces"
url = f"https://{host}:{port}/restconf/data/{yang_model_ifc}:{container_ifc}"

payload = '''
{{
    "ietf-interfaces:interface": {{
        "name": "{name}",
        "description": "{description}",
        "type": "iana-if-type:softwareLoopback",
        "enabled": true,
        "ietf-ip:ipv4": {{
            "address": [
                {{
                    "ip": "{ip}",
                    "netmask": "255.255.255.255"
                }}
            ]
        }},
        "ietf-ip:ipv6": {{}}
    }}
}}
'''
int_number = 5
for x in range(int_number):
    response = requests.post(url,
            auth=credentials,
            headers=headers,
            data=payload.format(name="Loopback123"+str(x),description="Added with PYTHON",ip="1.2.3."+str(x)),
            verify=False)
    print(f"Status Code: {response.status_code}")
    print(f"Response Text: {response.text}")

 

 

Code using JSON payload

Here payload is a Python dictionary, you can easily reconstruct the format from the previous string payload (they are almost identical). Here the catch is to structure the Python dictionary and then you can modify later the specific values by accessing them by their own keys and then passing the JSON payload into requests.post with the json parameter rather than the data parameter

 

import requests
import urllib3

urllib3.disable_warnings()

credentials = ("developer","C1sco12345")

headers = {"Accept":"application/yang-data+json","Content-Type":"application/yang-data+json"}

host = "10.10.20.48"
port = "443"
yang_model_ifc = "ietf-interfaces"
container_ifc = "interfaces"
url = f"https://{host}:{port}/restconf/data/{yang_model_ifc}:{container_ifc}"

payload = {
  "ietf-interfaces:interface": {
    "name": "",
    "description": "",
    "type": "iana-if-type:softwareLoopback",
    "enabled": True,
    "ietf-ip:ipv4": {
      "address": [
        {
          "ip": "",
          "netmask": "255.255.255.255"
        }
      ]
    },
    "ietf-ip:ipv6": {}
  }
}

int_number = 5
for x in range(int_number):
    payload["ietf-interfaces:interface"]["name"] = "Loopback123"+str(x)
    payload["ietf-interfaces:interface"]["description"] = "Added with PYTHON"
    payload["ietf-interfaces:interface"]["ietf-ip:ipv4"]["address"][0]["ip"] = "1.2.3."+str(x)
    #print(payload.format(name="Loopback123"+str(x),description="Added with PYTHON",ip="1.2.3."+str(x)))
    response = requests.post(url,
            auth=credentials,
            headers=headers,
            json=payload,
            verify=False)
    print(f"Status Code: {response.status_code}")
    print(f"Response Text: {response.text}")

 

I hope this helps you

 

 

 

Hi Giovanni,

My python skills are very low.

Can you use the same sandbox and port as I use and only change the bits in my code that are wrong?

Pointing exactly on the parts of my code that is wrong would be helpful.

Thank you.

if you replace your payload variable with this:

 

payload = '''
{{
    "ietf-interfaces:interface": {{
        "name": "{name}",
        "description": "{description}",
        "type": "iana-if-type:softwareLoopback",
        "enabled": true,
        "ietf-ip:ipv4": {{
            "address": [
                {{
                    "ip": "{ip}",
                    "netmask": "255.255.255.255"
                }}
            ]
        }},
        "ietf-ip:ipv6": {{}}
    }}
}}
'''

and in the requests.request , the data=payload with this:

data=payload.format(name="Loopback123"+str(x),description="Added with PYTHON",ip="1.2.3."+str(x))

Most probably it should work

 

Didn't help, unless I've done something wrong. Please see the output:

Screenshot (84).pngScreenshot (86).png

Probably you just added a character during the copy-paste, I am attaching here the complete python file for you to run.

 

Unzip and just replace host and port variables to your sandbox and it should work (tested with python 3.8)

 

 

Review Cisco Networking for a $25 gift card