SDN-Postman及python编程对流表的操作_output-node-connector

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注网络安全)
img

正文

<flow>
    <id>#UF$TABLE*0-2</id>
    <flow-statistics xmlns="urn:opendaylight:flow:statistics">
        <packet-count>0</packet-count>
        <duration>
            <nanosecond>145000000</nanosecond>
            <second>264</second>
        </duration>
        <byte-count>0</byte-count>
    </flow-statistics>
    <priority>0</priority>
    <table_id>0</table_id>
    <hard-timeout>0</hard-timeout>
    <match></match>
    <cookie>3098476543630901249</cookie>
    <idle-timeout>0</idle-timeout>
</flow>
<flow>
    <id>L2switch-0</id>
    <flow-statistics xmlns="urn:opendaylight:flow:statistics">
        <packet-count>1</packet-count>
        <duration>
            <nanosecond>652000000</nanosecond>
            <second>258</second>
        </duration>
        <byte-count>70</byte-count>
    </flow-statistics>
    <priority>2</priority>
    <table_id>0</table_id>
    <hard-timeout>0</hard-timeout>
    <match>
        <in-port>openflow:1:2</in-port>
    </match>
    <cookie>3098476543630901248</cookie>
    <instructions>
        <instruction>
            <order>0</order>
            <apply-actions>
                <action>
                    <order>0</order>
                    <output-action>
                        <max-length>65535</max-length>
                        <output-node-connector>1</output-node-connector>
                    </output-action>
                </action>
                <action>
                    <order>2</order>
                    <output-action>
                        <max-length>65535</max-length>
                        <output-node-connector>CONTROLLER</output-node-connector>
                    </output-action>
                </action>
                <action>
                    <order>1</order>
                    <output-action>
                        <max-length>65535</max-length>
                        <output-node-connector>3</output-node-connector>
                    </output-action>
                </action>
            </apply-actions>
        </instruction>
    </instructions>
    <idle-timeout>0</idle-timeout>
</flow>
<flow>
    <id>#UF$TABLE*0-3</id>
    <flow-statistics xmlns="urn:opendaylight:flow:statistics">
        <packet-count>3</packet-count>
        <duration>
            <nanosecond>647000000</nanosecond>
            <second>258</second>
        </duration>
        <byte-count>247</byte-count>
    </flow-statistics>
    <priority>2</priority>
    <table_id>0</table_id>
    <hard-timeout>0</hard-timeout>
    <match>
        <in-port>openflow:1:1</in-port>
    </match>
    <cookie>3098476543630901249</cookie>
    <instructions>
        <instruction>
            <order>0</order>
            <apply-actions>
                <action>
                    <order>0</order>
                    <output-action>
                        <max-length>65535</max-length>
                        <output-node-connector>2</output-node-connector>
                    </output-action>
                </action>
                <action>
                    <order>1</order>
                    <output-action>
                        <max-length>65535</max-length>
                        <output-node-connector>3</output-node-connector>
                    </output-action>
                </action>
            </apply-actions>
        </instruction>
    </instructions>
    <idle-timeout>0</idle-timeout>
</flow>
<flow-table-statistics xmlns="urn:opendaylight:flow:table:statistics">
    <packets-matched>57</packets-matched>
    <active-flows>5</active-flows>
    <packets-looked-up>105</packets-looked-up>
</flow-table-statistics>
```

流表分析

从这个xml中,我们可以看到流表的一些信息,包括流表的id,使用的交换机,一个流表项的入端口等,另外标签中是流表的一个流表项,共5条。

流表中字段含义

字段含义
table_idint,流表的id
idstring,流表项的id
priorityint,流表项的优先级
cookieint,控制器发出的标识符
cookie_maskint,掩码,用于限制必须匹配的cookie位
idle-timeoutint,丢弃前的空闲时间(秒)
hard-timeoutint,丢弃前的最长时间(秒)
matchdict,匹配域
ethernet-matchdict,以太网匹配
ethernet-typedict,以太网类型
typestr,0x0800:使用ip匹配,0x8847:使用mac匹配
ipv4-destinationipv4的目的ip地址
instructionsdict,动作集
instructionlist,动作,指令
orderint,顺序

Postman查看流表项

标题

json格式的流表项

{
    "flow-node-inventory:flow": [
        {
            "id": "L2switch-2",
            "opendaylight-flow-statistics:flow-statistics": {
                "packet-count": 2,
                "duration": {
                    "nanosecond": 81000000,
                    "second": 768
                },
                "byte-count": 140
            },
            "priority": 2,
            "table_id": 0,
            "hard-timeout": 0,
            "match": {
                "in-port": "openflow:1:3"
            },
            "cookie": 3098476543630901250,
            "instructions": {
                "instruction": [
                    {
                        "order": 0,
                        "apply-actions": {
                            "action": [
                                {
                                    "order": 0,
                                    "output-action": {
                                        "max-length": 65535,
                                        "output-node-connector": "2"
                                    }
                                },
                                {
                                    "order": 2,
                                    "output-action": {
                                        "max-length": 65535,
                                        "output-node-connector": "CONTROLLER"
                                    }
                                },
                                {
                                    "order": 1,
                                    "output-action": {
                                        "max-length": 65535,
                                        "output-node-connector": "1"
                                    }
                                }
                            ]
                        }
                    }
                ]
            },
            "idle-timeout": 0
        }
    ]
}

Python代码查看流表项

import urllib.request
from base64 import encodebytes

ip = "192.168.31.174"
node_id = "openflow:1"
table_id = "0"
flow_id = "L2switch-2"
username = "admin"
password = "admin"
url = "http://" + ip + ":8181/restconf/operational/opendaylight-inventory:nodes/node/" + node_id + "/flow-node-inventory:table/" + table_id +"/flow/"+flow_id
print(url)
# 添加请求的url和method
req = urllib.request.Request(url, method="GET")
# 添加header
b64str = encodebytes(bytes('%s:%s' % (username, password), 'utf-8'))[:-1]
req.add_header("Authorization", "Basic %s" % b64str.decode('utf-8'))
returnData = urllib.request.urlopen(req)
res_json = returnData.read().decode('utf-8')
print(res_json)

Postman添加流表项

命令查看流表项

命令删除流表项

使用PUT方式,账号密码依然还是admin,注意url是使用的config,不是operational,选择json格式,点击send。

使用Postman添加流表项

再次查看流表

再次查看流表后发现添加了一条流表项,h1、h2使用的是交换机s1的端口s1-eth1与s1-eth2,所以,h1与h2可以ping通。

Python添加流表项

import urllib.request
from base64 import encodebytes

from django.contrib.sites import requests

ip = "192.168.31.174"
# node_id = "openflow:1"
print("please input name of switch:")
node_id = input()
table_id = "0"
# flow_id = "L2switch-2"
print("please input id of flow:")
flow_id = input()
username = "admin"
password = "admin"
url = "http://" + ip + ":8181/restconf/config/opendaylight-inventory:nodes/node/" + node_id + "/flow-node-inventory:table/" + table_id + "/flow/" + flow_id
print(url)
flow = {
    "flow-node-inventory:flow": [
        {
            "id": "L2switch-2",
            "priority": 2,
            "table_id": 0,
            "hard-timeout": 0,
            "match": {
                "in-port": "openflow:1:3"
            },
            "cookie": 3098476543630901250,
            "instructions": {
                "instruction": [
                    {
                        "order": 0,
                        "apply-actions": {
                            "action": [
                                {
                                    "order": 0,
                                    "output-action": {
                                        "max-length": 65535,
                                        "output-node-connector": "2"
                                    }
                                }
                            ]
                        }
                    }
                ]
            },
            "idle-timeout": 0
        }
    ]
}
flow["flow-node-inventory:flow"][0]["id"] = flow_id
print("please input priority of this flow:")
flow["flow-node-inventory:flow"][0]["priority"] = int(input())
print("please input hard-timeout of this flow:")
flow["flow-node-inventory:flow"][0]["hard-timeout"] = int(input())
print("please input idle-timeout of this flow:")
flow["flow-node-inventory:flow"][0]["idle-timeout"] = int(input())
print("please input in-port of this flow:")
flow["flow-node-inventory:flow"][0]["match"]["in-port"] = node_id + ":" + input()
print("please input output-port of this flow:")
flow["flow-node-inventory:flow"][0]["instructions"]["instruction"][0]["apply-actions"]["action"][0]["output-action"][
    "output-node-connector"] = input()

print("flow_id:" + str(flow["flow-node-inventory:flow"][0]["id"]))
print("priority:" + str(flow["flow-node-inventory:flow"][0]["priority"]))
print("hard-timeout:" + str(flow["flow-node-inventory:flow"][0]["hard-timeout"]))
print("idle-timeout:" + str(flow["flow-node-inventory:flow"][0]["idle-timeout"]))
print("in-port:" + flow["flow-node-inventory:flow"][0]["match"]["in-port"])
print("output-port:" +
      flow["flow-node-inventory:flow"][0]["instructions"]["instruction"][0]["apply-actions"]["action"][0][
          "output-action"]["output-node-connector"])
print(flow)
# 添加请求的url、data、method
req = urllib.request.Request(url=url, data=bytes(str(flow), 'utf-8'), method="PUT")
# 添加header
b64str = encodebytes(bytes('%s:%s' % (username, password), 'utf-8'))[:-1]
req.add_header("Authorization", "Basic %s" % b64str.decode('utf-8'))
req.add_header("Accept", "application/json")
req.add_header("Content-Type", "application/json")
returnData = urllib.request.urlopen(req)
res_json = returnData.read().decode('utf-8')
print(res_json)

实验结果

写在最后

在结束之际,我想重申的是,学习并非如攀登险峻高峰,而是如滴水穿石般的持久累积。尤其当我们步入工作岗位之后,持之以恒的学习变得愈发不易,如同在茫茫大海中独自划舟,稍有松懈便可能被巨浪吞噬。然而,对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。

需要完整版PDF学习资源私我

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。**

需要完整版PDF学习资源私我

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)
[外链图片转存中…(img-VunWXK67-1713401389981)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 12
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值