这个脚本有什么作用?
我对 Ruby 不太了解,我需要了解这个脚本的作用。我知道它调用 ebtables 添加为虚拟机配置网络的规则。但我不确定如何?
这是代码:
#!/usr/bin/env ruby
require 'pp'
require 'rexml/document'
VM_NAME=ARGV[0]
# Uncomment to act only on the listed bridges.
#FILTERED_BRIDGES = ['beth0']
def activate(rule)
system "sudo ebtables -A #{rule}"
end
def get_bridges
bridges = Hash.new
brctl_exit=`brctl show`
cur_bridge = ""
brctl_exit.split("\n")[1..-1].each do |l|
l = l.split
if l.length > 1
cur_bridge = l[0]
bridges[cur_bridge] = Array.new
bridges[cur_bridge] << l[3]
else
bridges[cur_bridge] << l[0]
end
end
bridges
end
def get_interfaces
bridges = get_bridges
if defined? FILTERED_BRIDGES
FILTERED_BRIDGES.collect {|k,v| bridges[k]}.flatten
else
bridges.values.flatten
end
end
nets=`virsh -c qemu:///system dumpxml #{VM_NAME}`
doc=REXML::Document.new(nets).root
interfaces = get_interfaces()
doc.elements.each('/domain/devices/interface') {|net|
tap=net.elements['target'].attributes['dev']
if interfaces.include? tap
iface_mac=net.elements['mac'].attributes['address']
mac=iface_mac.split(':')
mac[-1]='00'
net_mac=mac.join(':')
in_rule="FORWARD -s ! #{net_mac}/ff:ff:ff:ff:ff:00 -o #{tap} -j DROP"
out_rule="FORWARD -s ! #{iface_mac} -i #{tap} -j DROP"
activate(in_rule)
activate(out_rule)
end
}
所以显然它提取了 mac 地址(不确定是哪些)并丢弃数据包/或将它们转发到某个地方?
非常感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该脚本运行
virsh -c qemu:///system dumpxml #{VM_NAME}
,其中 VM_NAME 是脚本的第一个参数。它实际上运行了两次,很可能是错误的。第一次运行
,然后第二次运行,并将 XML 输出放入变量 doc 中,
然后循环遍历接口,从元素 中的地址属性获取 MAC 地址。元素。
MAC 通过
:
分割为一个数组,该数组中的最后一个元素更改为“00”,并从该数组创建net_mac
。in_rule 赋值中的
#{net_mac}
将被新构造的net_mac
替换。等等。然后使用
sudo ebtables -A #{rule}
命令应用in_rule
和out_rule
。清除?
The script runs
virsh -c qemu:///system dumpxml #{VM_NAME}
where VM_NAME is the first parameter to the script.It actually runs it twice, most certainly by mistake. The first run
then it runs it a second time and places the XML output in the variable
doc
It then loops over the interfaces, getting the value from attribute dev in the element <target. If that value is in the result from the
brctl show
command it gets the MAC address from the address attribute in the <mac> element.The MAC is split to an array by
:
and the last element in that array is changed to '00' and thenet_mac
is created from that array.#{net_mac}
in the in_rule assignment will be replaced by the newly constructednet_mac
. And so on.Then the
in_rule
andout_rule
are applied with thesudo ebtables -A #{rule}
command.Clear?