当前位置:文档之家› 中科大高级计算机网络实验报告

中科大高级计算机网络实验报告

防火墙实验报告
一.实验过程
1.编写Python源代码,命名为pyretic_firewall.py。

保存在
~/pyretic/pyretic/examples中。

2.编写firewall-policies.csv源文件。

保存在~/pyretic/pyretic/examples中。

3.在虚拟机mininet仿真器中创建网络拓扑。

4.在宿主机中连接虚拟机,运行程序。

5.运用tcpdump测试实验结果。

二.pyretic_firewall.py源代码及重要代码解释
from pyretic.lib.corelib import *
from pyretic.lib.std import *
#以上代码引用库函数
# insert the name of the module and policy you want to import
from pyretic.examples.pyretic_switch import act_like_switch
import os
import csv
from csv import DictReader
#以上代码引用自己所需的一些模块
policy_file = "%s/pyretic/pyretic/examples/firewall-policies.csv" %
os.environ[ 'HOME']
#以上代码为指定引用的文件的路径
#以下代码为main函数
def main():
# start with a policy that doesn't match any packets
# 初始化not_allowed变量
not_allowed = none
# and sdd traffic that isn't allowed
# read the firewall-policies.csv
#打开.csv文件并读取文件内容
with open(policy_file, "r") as csvfile:
dictreader = DictReader(csvfile)
#如果d在dictreader中,则添加到not_allowed 中
for d in dictreader:
#对不允许的路由进行匹配
not_allowed = not_allowed +
(match(srcmac=MAC(d['mac_0']))&match(dstmac=MAC(d['mac_1']))) + (match(srcmac=MAC(d['mac_1']))&match(dstmac=MAC(d['mac_0']))) #express allowed traffic in terms of not_allowed - hint use'~'
#将不允许的路由取反即为允许路由
allowed = ~not_allowed
# and only send allowed traffic to the mac learning (act_like_switch) logic # 将允许的路由作为pyretic_switch中act_like_switch()的输入return allowed>>act_like_switch()
三.firewall-policies.csv 文件内容:
四.运行结果截图及结果解释
(1)在虚拟机中运行程序,结果如下所示:
(2)打开Xming,利用Putty远程登录虚拟机:
登录后结果:
(3)创建含有6台Host的网络拓扑,命令及运行结果如下图:
(4)打开6台Host的xterm,结果如图:
(5)在终端4,5,6的xterm中,运行tcpdump:
(6)测试实验结果
①用h1 ping h4 ,测试结果下图所示,h1 ping h4 失败,说明设置的防火墙生效。

②用h2 ping h5 ,测试结果与上图类似,h2 ping h5失败,说明设置的防火墙生效。

③用h2 ping h5 ,测试结果与上图类似,h2 ping h5失败,说明设置的防火墙生效。

④用h1 ping h2 ,测试结果如下图,h1 ping h2 成功,说明不在.csv 文件中的终端可以相互通信。

⑤验证其余不在.csv 文件中的终端,都可以通信。

五. 实验总结
由于实验过程中对于Python语言运用的不熟悉,导致在写pyretic_firewall.py 文件时,出现很多次错误。

不过,最终纠正了错误,完成了实验。

在本次实验中,实现了通过文件调用作为防火墙原则的功能,可为以后防火墙的设置减轻不少工作量。

相关主题