Configuring /etc/network/interfaces with a bash Script

Follow

While the long term strategy for many data centers is to automate the configuration of Cumulus Networks switches, this is often overkill for smaller environments.

However, manually adding all of the switch ports to the configuration file can be a painfully dreary task. The goal of this article is to show you how to quickly add the switch ports using just the tools available through the native bash shell.

  1. Determine the switch port identifiers on your device using the ip link show command:
    cumulus@switch$ ip link show
    1:  lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN mode DEFAULT
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2:  eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 08:9e:01:f8:9b:ac brd ff:ff:ff:ff:ff:ff
    3:  swp1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
        link/ether 08:9e:01:f8:9b:af brd ff:ff:ff:ff:ff:ff
    ...
    53: swp51: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
        link/ether 08:9e:01:f8:9b:df brd ff:ff:ff:ff:ff:ff
    54: swp52: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
        link/ether 08:9e:01:f8:9b:e0 brd ff:ff:ff:ff:ff:ff
    

    For this device, you will have to configure swp1 through swp52.

  2. Back up the current /etc/network/interfaces file:
    cumulus@switch$ sudo cp /etc/network/interfaces /etc/network/interfaces.orig

    This step is an insurance policy in case you accidentally corrupt the file you are trying to update.

  3. Create the script in the cumulus user home directory. Substitute the highest-numbered swp on your switch for the number on the line starting with "while".
    cumulus@switch$ vi populate.sh
    
    #!/bin/bash
    
    mycount=1
    
    while (( $mycount <= 52 ))
    do
    echo  >> /etc/network/interfaces
    echo auto swp$mycount >> /etc/network/interfaces
    echo iface swp$mycount >> /etc/network/interfaces
    ((mycount++))
    done

    Note: The spacing and special characters must be followed exactly or the script could corrupt your /etc/network/interfaces file. (Aren't you glad you made a copy in step 2?)

  4. Run the script using sudo, then apply the changes:
    cumulus@switch$ chmod 744 populate.sh
    cumulus@switch$ sudo bash populate.sh
    cumulus@switch$ cat /etc/network/interfaces
    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5), ifup(8)
    #
    # Please see /usr/share/doc/python-ifupdown2/examples/ for examples
    #
    #
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto eth0
    iface eth0 inet dhcp
    
    auto swp1
    iface swp1
    ...
    
    auto swp52
    iface swp52
    
    cumulus@switch$ sudo ifreload -a
  5. Verify that the ports are active using the ip link show command:
    cumulus@switch$ ip link show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN mode DEFAULT
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 08:9e:01:f8:9b:ac brd ff:ff:ff:ff:ff:ff
    3: swp1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT qlen 500
       link/ether 08:9e:01:f8:9b:af brd ff:ff:ff:ff:ff:ff
    ...
    53: swp51: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT qlen 500
       link/ether 08:9e:01:f8:9b:df brd ff:ff:ff:ff:ff:ff
    54: swp52: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT qlen 500
       link/ether 08:9e:01:f8:9b:e0 brd ff:ff:ff:ff:ff:ff

Bonus Section: How Does that Script Work?

The script in step 3 will work as written - just copy it onto your switch (adjusting the number of the highest numbered swp) and it just works. But if you want to start learning bash scripting, let’s take a look at the script line by line.

Note: There are multiple ways to accomplish the tasks described in shell scripting. This example uses my preferred methods - consult other resources (e.g. nixCraft) for a more complete discussion of scripting tools.

 #!/bin/bash

This statement tells the script which shell (command interpreter) should be used to run it. Typically the # character denotes a comment but here it actually passes information to the script.

 mycount=1

The script will use a counter to cycle through the swp interfaces. Here we set the variable named mycount with the initial value of 1.

while (( $mycount <= 52 ))

The while statement indicates a loop which will continue to run as long as the specified condition is true. All commands contained between the do and done statements will be performed on each pass of the loop.

The double parenthesis contain a conditional check. Using the $ character substitutes in the current value of the variable it precedes - in this case mycount. This line checks whether the counter is greater than or equal to the highest port ID on our switch.

Note: The white space in this line is critical - the notation used does not work if the spacing is not correct.

echo  >> /etc/network/interfaces
echo auto swp$mycount >> /etc/network/interfaces
echo iface swp$mycount >> /etc/network/interfaces

The echo command typically prints any arguments directly to the screen. The addition of the >> characters redirects that output and appends it to the end of the specified file. Be careful - a single > character will overwrite the file rather than adding to the end of it.

This block of code adds a blank line, followed by the standard auto and iface statements while substituting in the current value of the mycount variable to generate the swp interface name.

((mycount++))

This is the most critical part of this while loop. If the counter is not incremented, the loop will not exit automatically because the conditional check will never change.

Have more questions? Submit a request

Comments

Powered by Zendesk