【问题标题】:Move IP configuration from one interface to another将 IP 配置从一个接口移动到另一个接口
【发布时间】:2012-04-28 12:02:08
【问题描述】:

我正在开发一个 Python 脚本来测试一个网络应用程序。作为测试的一部分,它需要将网络配置(IP 地址、路由...)从一个接口(物理接口)移动到另一个接口(bridge),并且测试完成后,将系统恢复到原始状态。在 Python 中实现此目的最优雅的方法是什么?

我想到的一些想法:

  1. 在测试期间不要从物理接口取消分配IP地址,以免路由丢失。但这意味着相同的 IP 地址将在测试期间在 bridge 上共存。这会是某些特定 Linux 内核的问题吗?虽然,它似乎在我的系统上运行良好......
  2. 将 IP 地址分配给 bridge 并从 物理接口 取消分配。易于在 python 中实现,因为这需要进行简单的ifconfig 调用和解析。但是如果默认路由是通过物理接口,那么当我从物理接口取消分配IP地址时,它会同时消失。
  3. 解析ip route ls 输出并将路由与IP 配置一起移动。这似乎是唯一合理的方法,但需要大量编码。

  4. 也许有更优雅的东西?喜欢iptables-save eth0>eth0_confiptables-restore eth0_conf?还有其他建议吗?

此测试工具必须是可移植的,并且能够在不同的 Linux 内核上运行。

【问题讨论】:

    标签: python linux networking


    【解决方案1】:

    我会建议以下方法:

    1. 确保网桥接口已关闭
    2. 配置网桥接口
    3. 执行ifconfig eth0 down && ifconfig br0 up

    然后恢复:

    1. 执行ifconfig br0 down && ifconfig eth0 up

    现在对于路线,这取决于您拥有什么样的路线。如果您使用显式接口定义静态路由,您唯一的选择似乎是解析 ip route ls 并将它们转换为新接口。

    您还可以玩弄上下命令的顺序以及多个路由表:

    ip route add <whatever> table 2
    ip rule add from br0 table 2
    

    但这可能会变得棘手,所以我的建议是坚持简单的解决方案,即使它包含更多的编码。

    这是另一个来自 xend 的 network-bridge 脚本的示例来实现此目的:

    # Usage: transfer_addrs src dst
    # Copy all IP addresses (including aliases) from device $src to device $dst.
    transfer_addrs () {
        local src=$1
        local dst=$2
        # Don't bother if $dst already has IP addresses.
        if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
            return
        fi
        # Address lines start with 'inet' and have the device in them.
        # Replace 'inet' with 'ip addr add' and change the device name $src
        # to 'dev $src'.
        ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
    s/inet/ip addr add/
    s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
    s/${src}/dev ${dst}/
    " | sh -e
        # Remove automatic routes on destination device
        ip route list | sed -ne "
    /dev ${dst}\( \|$\)/ {
      s/^/ip route del /
      p
    }" | sh -e
    }
    
    # Usage: transfer_routes src dst
    # Get all IP routes to device $src, delete them, and
    # add the same routes to device $dst.
    # The original routes have to be deleted, otherwise adding them
    # for $dst fails (duplicate routes).
    transfer_routes () {
        local src=$1
        local dst=$2
        # List all routes and grep the ones with $src in.
        # Stick 'ip route del' on the front to delete.
        # Change $src to $dst and use 'ip route add' to add.
        ip route list | sed -ne "
    /dev ${src}\( \|$\)/ {
      h
      s/^/ip route del /
      P
      g
      s/${src}/${dst}/
      s/^/ip route add /
      P
      d
    }" | sh -e
    }
    

    【讨论】:

    • 我喜欢你打开/关闭界面以恢复路由的方法。我想这可能对我有用。现在唯一的问题是 - 如何从 eth0->br0 移动路由(我相信两个接口都应该在测试期间启动)。
    • @AnsisAtteka 你能详细说明一下你的设置吗? br0 包含哪些接口? eth0 和 br0 在同一个物理网络上吗?你想达到什么目的?
    • 在同一个子网中有两个物理接口可能会很痛苦,至少在我的经验中是这样。我设法通过为接口使用单独的路由表来绕过它们
    • br0 是桥接器(软件定义的交换机),我实际上插入了 eth0 接口(例如“ovs-vsctl add-port bre0 eth0”或“brctl addif br0 eth0”命令)。
    • 我还看到了 DHCP 的一些潜在问题,如果我要关闭/启动接口以恢复 ip+route 配置。虽然我不希望在现实世界中遇到它们,因为我相信在大多数设置中,当我再次启动接口后,DHCP 服务器会分配相同的 IP 地址。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多