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