User Tools

Site Tools


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
Last revisionBoth sides next revision
wiki:interface_renaming_scripts [2018/06/19 11:06] walkeradminwiki:interface_renaming_scripts [2018/07/31 08:26] walkeradmin
Line 40: 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 148: Line 148:
 ===The Scripts=== ===The Scripts===
 \\  \\ 
-store_macaddr_mapping+<color #ed1c24>store_macaddr_mapping</color>
 \\  \\ 
 <file> <file>
Line 200: Line 200:
  
 </file> </file>
-rename_network_interfaces.sh+<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.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1