wiki:interface_renaming_scripts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
wiki:interface_renaming_scripts [2018/06/19 10:57] walkeradminwiki:interface_renaming_scripts [2023/03/09 22:35] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Interface Renaming Scripts ====== ====== Interface Renaming Scripts ======
 <color darkorange>June 2018</color> <color darkorange>June 2018</color>
-\\  
 \\  \\ 
 \\  \\ 
  
 ---- ----
 +<color #ed1c24>Please see the end of this page for the two sripts</color>
 +----
 +\\ 
 +\\ 
 For anyone that has used CentOS/Red Hat prior to version 7, or indeed other versions of Linux, the norm for network interface naming is to use **eth0, eth1, eth2** etc. For anyone that has used CentOS/Red Hat prior to version 7, or indeed other versions of Linux, the norm for network interface naming is to use **eth0, eth1, eth2** etc.
 \\  \\ 
Line 37: Line 40:
 Now execute the file using the following syntax: Now execute the file using the following syntax:
 \\  \\ 
-    ./store_macaddr_mapping.sh network_mapping.csv+    ./store_macaddr_mapping.sh --output network_mapping.csv
 \\  \\ 
 \\  \\ 
Line 67: Line 70:
 \\  \\ 
 Both of these are easy to fix. Firsly you can manually edit the file, so that the interface names are correct, but more importantly, you need to get the MAC address order correct. Both of these are easy to fix. Firsly you can manually edit the file, so that the interface names are correct, but more importantly, you need to get the MAC address order correct.
 +\\ 
 \\  \\ 
 Use <color #ed1c24>ip link show</color> to show interfaces and MACS Use <color #ed1c24>ip link show</color> to show interfaces and MACS
Line 101: Line 105:
  
 ---- ----
 +===rename_network_interfaces.sh===
 +\\ 
 +The next part is much simpler. Copy the rename_network_interfaces.sh to your server, and give it execute privileges using the following syntax:
 +\\ 
 +    chmod 777 rename_network_interfaces.sh
 +\\ 
 +Now execute the script using the following syntax:
 +\\ 
 +    ./rename_network_interfaces.sh --csv network_mapping.csv
 +\\ 
 +\\ 
 +If successful the following output will be observed (depending on what interface scripts already existed).
 +\\ 
 +<file>
 +*** Getting macaddr to register ***
 +*** Using network_mapping.csv for network interface mapping ***
 +        Backup of /etc/sysconfig/network-scripts/ifcfg-ens513f3 in /etc/sysconfig/network-scripts/backup/ifcfg-ens513f3
 +        Backup of /etc/sysconfig/network-scripts/ifcfg-ens513f1 in /etc/sysconfig/network-scripts/backup/ifcfg-ens513f1
 +        Backup of /etc/sysconfig/network-scripts/ifcfg-enp4s0f0 in /etc/sysconfig/network-scripts/backup/ifcfg-enp4s0f0
 +        Backup of /etc/sysconfig/network-scripts/ifcfg-enp4s0f3 in /etc/sysconfig/network-scripts/backup/ifcfg-enp4s0f3
 +        Backup of /etc/sysconfig/network-scripts/ifcfg-ens513f2 in /etc/sysconfig/network-scripts/backup/ifcfg-ens513f2
 +        Backup of /etc/sysconfig/network-scripts/ifcfg-ens513f0 in /etc/sysconfig/network-scripts/backup/ifcfg-ens513f0
 +        Generating file /etc/sysconfig/network-scripts/ifcfg-eth0
 +        Generating file /etc/sysconfig/network-scripts/ifcfg-eth1
 +        Generating file /etc/sysconfig/network-scripts/ifcfg-eth2
 +        Generating file /etc/sysconfig/network-scripts/ifcfg-eth3
 +        Generating file /etc/sysconfig/network-scripts/ifcfg-eth4
 +        Generating file /etc/sysconfig/network-scripts/ifcfg-eth5
 +</file>
 +\\ 
 +Check the /etc/sysconfig/network-scripts location to see the new files.
 +\\ 
 +Note: No configuration information is copied from the old interface scripts, so before you reboot, you need to re-add all of your IP/Netmask/Gateway/DNS information.
 +\\ 
 +\\ 
 +Reboot the server to check the interfaces have been renamed, and the mapping is correct.
 +\\ 
 +\\ 
 +
 +----
 +
 +===The Scripts===
 +\\ 
 +<color #ed1c24>store_macaddr_mapping</color>
 +\\ 
 +<file>
 +#!/bin/bash
 +
 +# this script is based on infos got from blog page: 
 +# http://ask.xmodulo.com/change-network-interface-name-centos7.html
 +
 +# exit at first error
 +set -o errexit
 +
 +export IP_INTERFACES="ip addr"
 +export INTERFACE_ANCHOR="state UP"
 +export MACADDR_ANCHOR="link/ether"
 +
 +export OUTPUT_FILE
 +
 +function show_help() {
 +cat <<- EOF
 +Generate csv file with interface name; mac addr. Output file is a mandatory parameter.
 +    --output                                    Output file path where mac addre mapping will be stored
 +    --help|-h                                   [Optional] Display this help and exit
 +
 +EOF
 +}
 +
 +while [ $# -gt 0 ] ; do
 +    case $1 in
 +        --output) OUTPUT_FILE="$2";shift 2;;
 +        --help|-h) show_help;exit 0;;
 +        *) if ! [ -z "$1" ] ; then echo "Unknown parameter $1" ; show_help ; exit 1 ; else  shift 1; fi ;;
 +    esac
 +done
 +
 +[ -z "$OUTPUT_FILE" ] && show_help && exit 1
 +
 +rm -f "$OUTPUT_FILE" > /dev/null 2>&1
 +
 +echo "Generating $OUTPUT_FILE"
 +interface_name_list=$($IP_INTERFACES | grep "$INTERFACE_ANCHOR" | awk '{print $2}' | sed 's|:||')
 +interface_name_list=($interface_name_list)
 +macaddr_list=$($IP_INTERFACES | grep "$MACADDR_ANCHOR" | awk '{print $2}' | tr '[:upper:]' '[:lower:]')
 +macaddr_list=($macaddr_list)
 +cnt=0
 +arraySize="${#macaddr_list[@]}"
 +
 +while [ "$cnt" != "$arraySize" ] ; do 
 +    echo "${interface_name_list[$cnt]};${macaddr_list[$cnt]}" >> "$OUTPUT_FILE"
 +    cnt=$(( cnt + 1 ))
 +done
 +
 +</file>
 +<color #ed1c24>rename_network_interfaces.sh</color>
 +\\ 
 +<file>
 +#!/bin/bash
 +
 +# this script is based on infos got from blog page: 
 +# http://ask.xmodulo.com/change-network-interface-name-centos7.html
 +
 +# exit at first error
 +set -o errexit
 +
 +IP_INTERFACES="ip addr"
 +MACADDR_ANCHOR="link/ether"
 +NET_NAME_TEMPLATE="eth"
 +
 +CSV_FILE=
 +
 +NETWORK_SCRIPT_DIR="/etc/sysconfig/network-scripts"
 +NETWORK_BACKUP_DIR="/etc/sysconfig/network-scripts/backup"
 +NETWORK_SCRIPT_TEMPLATE='NAME="<network_name>"
 +DEVICE="<network_name>"
 +ONBOOT=yes
 +NETBOOT=yes
 +BOOTPROTO=dhcp
 +TYPE=Ethernet
 +# HWADDR key is mandatory to use custom interface name: do not delete it
 +HWADDR=<mac_addr>
 +'
 +
 +function show_help() {
 +cat <<- EOF
 +Update network interfaces names. A csv file with the expected mapping can be given as arg.
 +    --csv                                       Csv file containing <interface name>;<mac addr> as they should be defined
 +    --help|-h                                   [Optional] Display this help and exit
 +
 +EOF
 +}
 +
 +while [ $# -gt 0 ] ; do
 +    case $1 in
 +        --csv) CSV_FILE="$2";shift 2;;
 +        --help|-h) show_help;exit 0;;
 +        *) if ! [ -z "$1" ] ; then echo "Unknown parameter $1" ; show_help ; exit 1 ; else  shift 1; fi ;;
 +    esac
 +done
 +
 +echo "*** Getting macaddr to register ***"
 +macaddr_list=$($IP_INTERFACES | grep "$MACADDR_ANCHOR" | awk '{print $2}')
 +
 +if ! [ -z "$CSV_FILE" ] && [ -f "$CSV_FILE" ] ; then
 +    echo "*** Using $CSV_FILE for network interface mapping ***"
 +    ! [ -d "$NETWORK_BACKUP_DIR" ] && mkdir -p "$NETWORK_BACKUP_DIR"
 +    for old_script_file in $(find $NETWORK_SCRIPT_DIR -maxdepth 1 -name "ifcfg-*" -a -not -name "ifcfg-lo" -a -not -name "ifcfg-*\.*"); do 
 +        echo -e "\tBackup of $old_script_file in $NETWORK_BACKUP_DIR/$(basename "$old_script_file")"
 +        mv -f "$old_script_file" $NETWORK_BACKUP_DIR/.
 +    done
 +    while read interface_data
 +    do
 +        [ -z "$interface_data" ] && continue 
 +        interface_name=$(echo "$interface_data" | cut -d \; -f1)
 +        mac_addr=$(echo "$interface_data" | cut -d \; -f2 | tr '[:upper:]' '[:lower:]')
 +        [ -z "$interface_name" ] && continue  
 +        [ -z "$mac_addr" ] && continue  
 +        
 +        # check that given mac addr exist
 +        if ! echo "$macaddr_list" | grep "$mac_addr" > /dev/null 2>&1 ; then
 +            echo "Unable to find $mac_addr in available network interfaces, please review file content of $CSV_FILE"
 +            exit 1
 +        fi
 +        echo -e "\tGenerating file $NETWORK_SCRIPT_DIR/ifcfg-$interface_name"
 +        echo "$NETWORK_SCRIPT_TEMPLATE" > $NETWORK_SCRIPT_DIR/ifcfg-"$interface_name"
 +        sed -i "s|<network_name>|$interface_name|g" $NETWORK_SCRIPT_DIR/ifcfg-"$interface_name"
 +        sed -i "s|<mac_addr>|$mac_addr|g" $NETWORK_SCRIPT_DIR/ifcfg-"$interface_name"
 +    done < "$CSV_FILE"
 +
 +else
 +    echo "*** Using default naming (${NET_NAME_TEMPLATE}x) for network interface mapping ***"
 +    ! [ -d "$NETWORK_BACKUP_DIR" ] && mkdir -p "$NETWORK_BACKUP_DIR"
 +    cnt=0
 +    for old_script_file in $(find $NETWORK_SCRIPT_DIR -maxdepth 1 -name "ifcfg-*" -a -not -name "ifcfg-lo" -a -not -name "ifcfg-*\.*"); do 
 +        echo -e "\tBackup of $old_script_file in $NETWORK_BACKUP_DIR/$(basename "$old_script_file")"
 +        mv -f "$old_script_file" $NETWORK_BACKUP_DIR/.
 +    done
 +    for mac_addr in ${macaddr_list[*]}; do 
 +        mac_addr=$(echo "$mac_addr" | tr '[:upper:]' '[:lower:]')
 +        echo -e "\tGenerating file $NETWORK_SCRIPT_DIR/ifcfg-$NET_NAME_TEMPLATE$cnt"
 +        echo "$NETWORK_SCRIPT_TEMPLATE" > $NETWORK_SCRIPT_DIR/ifcfg-$NET_NAME_TEMPLATE$cnt
 +        sed -i "s|<network_name>|$NET_NAME_TEMPLATE$cnt|g" $NETWORK_SCRIPT_DIR/ifcfg-$NET_NAME_TEMPLATE$cnt
 +        sed -i "s|<mac_addr>|$mac_addr|g" $NETWORK_SCRIPT_DIR/ifcfg-$NET_NAME_TEMPLATE$cnt
 +        cnt=$(( cnt + 1 ))
 +    done
 +fi
 +
 +echo "*** Please check consistency of generated files in directory $NETWORK_SCRIPT_DIR ***"
 +
 +
 +</file>
 +\\ 
 +\\ 
  
  
wiki/interface_renaming_scripts.1529402246.txt.gz · Last modified: 2023/03/09 22:35 (external edit)