LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2021, 12:08 PM   #1
charlweed
Member
 
Registered: Jan 2005
Posts: 46

Rep: Reputation: 15
Question Preferred way to copy route configuration to different host?


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?
 
Old 09-12-2021, 02:07 PM   #2
charlweed
Member
 
Registered: Jan 2005
Posts: 46

Original Poster
Rep: Reputation: 15
I'm going to try a 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, replacing the spaces with a comma. I also replaced the device names with ones that matched on the new system. So sample data 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 rout 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

Last edited by charlweed; 09-12-2021 at 02:08 PM.
 
Old 07-03-2023, 05:28 AM   #3
fraxflax
LQ Newbie
 
Registered: Jan 2018
Location: Sweden
Distribution: Debian
Posts: 24

Rep: Reputation: 5
A oneliner :-)

`ip route show` shows each route entry in the main table exactly as it can be used as input to 'ip route add' ... so just prefix each line in the output with with 'ip route add ', e.g. like this:

Code:
ip route show | while read x; do echo ip route add $x; done
the output of the above can be executed exactly as is in any shell
(but of course you need to change ifc names if they are changed on your new setup)

Last edited by fraxflax; 07-03-2023 at 05:30 AM. Reason: typo
 
1 members found this post helpful.
Old 07-08-2023, 12:28 PM   #4
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 415
Blog Entries: 2

Rep: Reputation: Disabled
Why not look at the network setup scripts on Centos and translate to netplan? It is almost 1-1 setup.
 
  


Reply

Tags
routing



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Packet going out of wrong interface due to OS adding cache route - OS trying to route through default route msr1981 Linux - Networking 2 10-11-2017 02:58 PM
Packet going out of wrong interface due to OS adding cache route - OS trying to route through default route msr1981 Red Hat 1 10-09-2017 05:45 AM
[SOLVED] How to copy file from remote host to local host then delete from remote host legendmac Linux - Newbie 13 05-27-2015 03:47 PM
I am not able to add a new route to my route table using route command prashanth s j Linux - Networking 2 09-03-2005 04:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration