Page 1 of 1

ReST API creating nodes and interfaces

Posted: Tue Jul 04, 2017 1:41 pm
by icedewd
Hello all,
I am interested in utilizing the ReST API to generate topologies via a python script. I am at this point able to create the folders, lab, and nodes but I am unable to understand how to connect two nodes. What is the best way to find help for this topic?

Here is what I have learned so far, I have created a user called api with the password eve for these examples. This allows me to be logged into the Web interface with the admin account and see my progress.

# Login and store a cookie, next requests will use the cookie stored for authentication.
curl -c cookie -b cookie -X POST -H "Content-type: application/json" -d '{"username":"api","password":"eve"}' http://172.16.5.131/api/auth/login

# List Folders
curl -b cookie -X GET http://172.16.5.131/api/folders/

# Create a lab
curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs -d '
{
"path":"/HomeLab",
"name":"Security",
"version":"1",
"author":"Mike",
"description":"Security Lab",
"lock":"0"
}
'

#Create a node
curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs/HomeLab/Security.unl/nodes -d '
{
"type":"iol",
"template":"iol",
"image":"L3-ADVENTERPRISEK9-M-15.4-2T.bin",
"name":"R1",
"icon":"Router.png",
"nvram":"1024",
"ram":"256",
"ethernet":"1",
"serial":"0",
"delay":"0",
"config":"0",
"left":"300",
"top":"200"
}
'

# Create a second node
curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs/HomeLab/Security.unl/nodes -d '
{
"type":"iol",
"template":"iol",
"image":"L3-ADVENTERPRISEK9-M-15.4-2T.bin",
"name":"R2",
"icon":"Router.png",
"nvram":"1024",
"ram":"256",
"ethernet":"1",
"serial":"0",
"delay":"0",
"config":"0",
"left":"350",
"top":"200"
}
'



Thanks to http://www.802101.com/unetlab-rest-api/ for getting me this far.

Any help with connecting two nodes via the API would be greatly appreciated.

Thanks all,
Mike

Re: ReST API creating nodes and interfaces

Posted: Tue Jul 04, 2017 3:41 pm
by Uldis (UD)
to be honest, i do not understand why need such cli stuff if we have perfect GUI:)

Re: ReST API creating nodes and interfaces

Posted: Tue Jul 04, 2017 4:12 pm
by icedewd
You are correct the GUI is excellent and works great for building topologies. My goal is to create a script that will take my production network configs and create a eve topology with the configs. This will allow me to have the current production network(mainly for routing policies, not interfaces) reproduced and validate configs prior to implementation. Being able to do this via a script will save numerous hours in recreating topologies and configs. I am able to complete this with the output producing a .zip ready for import, but if I can directly interact with EVE this would make the process much smoother.


Thanks for the assistance.

Mike

Re: ReST API creating nodes and interfaces

Posted: Wed Jul 05, 2017 2:35 pm
by ecze
You need at first to create a bridge for interconnection
The you need to define for each node how to connect one interface to the bridge


so something like:

Code: Select all

curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs/HomeLab/Security.unl/networks -d '
{
"count":1,
"name":"Net-R2iface_0",
"type":"bridge",
"left":465,
"top":91,
"visibility":1,
"postfix":0
}

you will receive network id ...

use it for next commands... ( we assume node ids are 1 and 2 and network id is 1 )

Code: Select all

curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs/HomeLab/Security.unl/nodes/1/interfaces -d '
{
"0":1
}

curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs/HomeLab/Security.unl/nodes/2/interfaces -d '
{
"0":1
}
Finally make the network invisible:

Code: Select all

curl -X POST -H "Content-type: application/json" -b cookie http://172.16.5.131/api/labs/HomeLab/Security.unl/networks/1 -d'
{
"visibility":0
}
E.

Re: ReST API creating nodes and interfaces

Posted: Wed Jul 05, 2017 4:27 pm
by icedewd
I have figured it out with the help of WireShark. With Wireshark i was able to capture my GUI calls and translate that to CURL requests.
For this to work I created a user called api with the password eve.

Step 1. Create the cookie that will be used for this session

Code: Select all

curl -c cookie -b cookie -X POST -H "Content-type: application/json" -d '{"username":"api","password":"eve"}' http://192.168.1.202/api/auth/login
Step 2. Create a folder call HomeLab

Code: Select all

curl -X POST  -H "Content-type: application/json" -b cookie  http://192.168.1.202/api/folders -d '
{
"name":"HomeLab",
"path":""
}
'

Step 3. Create a Lab

Code: Select all

curl -X POST  -H "Content-type: application/json" -b cookie  http://192.168.1.202/api/labs -d '
{
"path":"/HomeLab",
"name":"Security",
"version":"1",
"author":"Mike",
"description":"Security Lab",
"lock":"0"
}
'

This is my first attempt but it seems very promising!

Mike


Step 4. Create 4 nodes in a square topology.

Code: Select all

curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes -d '
 {
 "type":"iol",
 "template":"iol",
 "image":"L3-ADVENTERPRISEK9-M-15.4-2T.bin",
 "name":"R1",
 "icon":"Router.png",
 "nvram":"1024",
 "ram":"256",
 "ethernet":"1",
 "serial":"0",
 "delay":"0",
 "config":"0",
 "left":"100",
 "top":"200"
 }
 '
curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes -d '
 {
 "type":"iol",
 "template":"iol",
 "image":"L3-ADVENTERPRISEK9-M-15.4-2T.bin",
 "name":"R2",
 "icon":"Router.png",
 "nvram":"1024",
 "ram":"256",
 "ethernet":"1",
 "serial":"0",
 "delay":"0",
 "config":"0",
 "left":"400",
 "top":"200"
 }
 '
curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes -d '
 {
 "type":"iol",
 "template":"iol",
 "image":"L3-ADVENTERPRISEK9-M-15.4-2T.bin",
 "name":"R3",
 "icon":"Router.png",
 "nvram":"1024",
 "ram":"256",
 "ethernet":"1",
 "serial":"0",
 "delay":"0",
 "config":"0",
 "left":"100",
 "top":"400"
 }
 '
curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes -d '
 {
 "type":"iol",
 "template":"iol",
 "image":"L3-ADVENTERPRISEK9-M-15.4-2T.bin",
 "name":"R4",
 "icon":"Router.png",
 "nvram":"1024",
 "ram":"256",
 "ethernet":"1",
 "serial":"0",
 "delay":"0",
 "config":"0",
 "left":"400",
 "top":"400"
 }
 '
Step 5. Create 4 networks

Code: Select all

curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks -d '
 {
 "count":"1",
 "id":"2",
 "type":"bridge",
 "name":"Net1",
 "left":"100",
 "top":"300",
 "visibility":"1"
 }
 '

curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks -d '
 {
 "id":"2",
 "type":"bridge",
 "name":"Net2",
 "left":"200",
 "top":"300",
 "visibility":"1"
 }
 '
curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks -d '
 {
 "id":"3",
 "type":"bridge",
 "name":"Net3",
 "left":"300",
 "top":"300",
 "visibility":"1"
 }
 '
curl -X POST -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks -d '
 {
 "id":"4",
 "type":"bridge",
 "name":"Net4",
 "left":"400",
 "top":"300",
 "visibility":"1"
 }
 '

Step 6. Connect nodes to the network

Code: Select all

curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes/1/interfaces -d '
{
 "0":"1",
 "16":"2"
}'

#Node 2 Network connections
curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes/2/interfaces -d '
{
 "0":"1",
 "16":"4"
}'

#Node 3 Network connections
curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes/3/interfaces -d '
{
 "0":"3",
 "16":"2"
}'

#Node 4 Network connections
curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/nodes/4/interfaces -d '
{
 "0":"3",
 "16":"4"
}'

Step 7. Make the networks invisible

Code: Select all

curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks/1 -d '
 {
 "visibility":"0"
 }
 '
curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks/2 -d '
 {
 "visibility":"0"
 }
 '
curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks/3 -d '
 {
 "visibility":"0"
 }
 '
curl -X PUT -b cookie http://192.168.1.202/api/labs/HomeLab/Security.unl/networks/4 -d '
 {
 "visibility":"0"
 }
 '

Re: ReST API creating nodes and interfaces

Posted: Wed Dec 19, 2018 11:11 pm
by lewo
Hi All,

Usinng the APIs above I'm able to create nodes etc, however when I create a node, the following URL is returned instead of the ip:port combo.

Code: Select all

{
    "code": 200,
    "status": "success",
    "message": "Successfully listed node (60025).",
    "data": {
        "console": "",
        "config": "0",
        "delay": 0,
        "left": 324,
        "icon": "Switch.png",
        "image": "i86bi-linux-l2-ipbasek9-15.1g.bin",
        "name": "S2",
        "status": 0,
        "template": "iol",
        "type": "iol",
        "top": 72,
       "url": "html5/#/client/unknowntoken",
        "ethernet": 4,
        "nvram": 1024,
        "ram": 256,
        "serial": 0
    }
}
Anyone know how to obtain the ip and port number for the telnet session?

Thanks

Re: ReST API creating nodes and interfaces

Posted: Thu Dec 20, 2018 12:44 am
by lewo
Never mind, just required the login rest call to set "html5": "-1"