这个脚本有什么作用?

发布于 2024-10-05 13:31:04 字数 1589 浏览 3 评论 0 原文

我对 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 地址(不确定是哪些)并丢弃数据包/或将它们转发到某个地方?

非常感谢您的帮助!

I don't know much about Ruby and I need to understand what this script does. I know it calls ebtables to add rules that configure networks for Virtual machines. But I'm not sure how?

this is the code:

#!/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
}

So apparently it extracts mac adresses (not sure which ones) and drops packet/or forwards them somewhere?

Thanks a lot for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-10-12 13:31:04

该脚本运行 virsh -c qemu:///system dumpxml #{VM_NAME},其中 VM_NAME 是脚本的第一个参数。

它实际上运行了两次,很可能是错误的。第一次运行

nets=`virsh -c qemu:///system dumpxml #{VM_NAME}`

,然后第二次运行,并将 XML 输出放入变量 doc 中,

doc=REXML::Document.new(nets).root

然后循环遍历接口,从元素 中的地址属性获取 MAC 地址。元素。

MAC 通过 : 分割为一个数组,该数组中的最后一个元素更改为“00”,并从该数组创建 net_mac

in_rule 赋值中的 #{net_mac} 将被新构造的 net_mac 替换。等等。

然后使用 sudo ebtables -A #{rule} 命令应用 in_ruleout_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

nets=`virsh -c qemu:///system dumpxml #{VM_NAME}`

then it runs it a second time and places the XML output in the variable doc

doc=REXML::Document.new(nets).root

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 the net_mac is created from that array.

#{net_mac} in the in_rule assignment will be replaced by the newly constructed net_mac. And so on.

Then the in_rule and out_rule are applied with the sudo ebtables -A #{rule} command.

Clear?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文