Score:0

Preferred way to copy route configuration to different host?

in flag

I'm replacing a CentOS server with an Ubuntu 20 one. I need to duplicate the older servers routing configuration. The outputs of ip route show and route are not directly usable as inputs.

I'm a bit reluctant to hand-translate the output into route commands, because errors always creep in. Is there a better way?

Nmath avatar
ng flag
What commands? What errors? Edit your question and include details.
pLumo avatar
in flag
Yes, you could give an example what is not working with translating.
in flag
I merely avoid hand-copying or cut-pasting data to avoid user-error. I don't know the idiosyncrasies of `route` nor `ip route`. And network stuff is super-vulnerable to typos.
guiverc avatar
cn flag
Do you realize Ubuntu has different products, the main *deb* based products are *year.month* in format, eg. Ubuntu 20.04 LTS Server, but there are also *snap* only products which use the *year* format, eg. Ubuntu Core 20 (Server). 20 is a different product to the 20.04 (it's based on the more powerful 20.04 system, but is smaller making it better suited for devices, appliances & cloud use..). Be precise with details.
Score:-1
in flag

I'm going to try a powershell script. route shows the current routing tables as a table with these columns: Destination,Gateway,Genmask,Flags,Metric,Ref,Use,Iface

So I saved that output to a file, and copied it to the new host. I converted the raw output to a csv file by removing the first line, and replacing the spaces with a comma. I also replaced the device names with ones that matched on the new system. So my test route table might look like this:

Destination,Gateway,Genmask,Flags,Metric,Ref,Use,Iface
default,gateway,0.0.0.0,UG,0,0,0,enp5s0
default,toblerone.mydomain.org,0.0.0.0,UG,100,0,0,eno1
192.168.0.0,0.0.0.0,255.255.255.0,U,100,0,0,eno1
192.168.1.0,0.0.0.0,255.255.255.0,U,0,0,0,enp5s0
192.168.1.0,gateway,255.255.255.0,UG,0,0,0,enp5s0

The route command can produce unexpected errors when the route already exists. I also don't want the generated commands to run directly. So my PowerShell script looks like this:

#!/usr/bin/env pwsh

Set-StrictMode -Version Latest

$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['Set-*:ErrorAction'] = "Stop"

$routeDataCSV = Import-CSV "./route_data.csv"
# A route should only be deleted once per run.
$routeDeleted = @{}

foreach ($routeIn in $routeDataCSV) {
    if (!$routeDeleted[$routeIn.Destination] -or (!($routeDeleted[$routeIn.Destination] -like "true"))) {
        Write-Output "route del -net $($routeIn.Destination)"
        $routeDeleted[$routeIn.Destination] = "true"
    }
    Write-Output "route add -net $($routeIn.Destination) gw $($routeIn.Gateway) netmask $($routeIn.Genmask) metric $($routeIn.Metric) dev $($routeIn.Iface)"
}

The generated commands are then:

route del -net default
route add -net default gw gateway netmask 0.0.0.0 metric 0 dev enp5s0
route add -net default gw toblerone.mydomain.org netmask 0.0.0.0 metric 100 dev eno1
route del -net 192.168.0.0
route add -net 192.168.0.0 gw 0.0.0.0 netmask 255.255.255.0 metric 100 dev eno1
route del -net 192.168.1.0
route add -net 192.168.1.0 gw 0.0.0.0 netmask 255.255.255.0 metric 0 dev enp5s0
route add -net 192.168.1.0 gw gateway netmask 255.255.255.0 metric 0 dev enp5s0
in flag
This "works", but as I feared, there is a lot to learn about the `route` command. All the above commands fail when the '-net' option is present, even though the targets are all networks. With that correction, the few routes I actually needed worked, but the rest of the commands failed. All the above commands that have "gw 0.0.0.0" are pointless, and will fail with errors, because those networks are local, and do not need routes. There might be more problems, but the routes I want work, so I'm done.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.