• A simple GNU sed example on EOS

 
 
Print Friendly, PDF & Email

Hopefully by now you are aware that Arista EOS (Extensible Operating System), which is the operating system that runs on Arista switches, is based on Linux. From the CLI you can drop to the Bash shell by just typing bash.

Given that EOS is based on Linux you already have access to many of the helpful utilities seen in many Linux distributions. Let’s pretend that you have a configuration file that was copied over from another very similar configuration and that the only thing that needs to change is every occurrence of IP addresses that look like 10.0.x.y. This is a true real-world example where my customer designed a cookie-cutter datacenter which is identical in almost every way from datacenter to datacenter. Only the beginning byte or two of the IP addresses needed to change. Any IP address that begins with “10.0” needs to change to an IP address that now begins with “11.4”. Everything else stays the same because everything connected to the network is identical with similar IP addressing including all the devices connected to the Arista switches. You may run into a situation where you need to take an existing config that’s used as a template in other environments and, perhaps, you only need to change IP addresses. This is a nice task that one could use sed for.

Let’s pretend that I have a configuration file (in /mnt/flash/temp – accessible from the Bash shell) called my.config. Below is a snippet of the configuration:

[admin@eos temp]$ cat my.config
interface Loopback0
   ip address 10.0.0.3/32
ip routing
router ospf 1000
   router-id 10.0.0.3
   passive-interface default
   no passive-interface Vlan1
   network 0.0.0.0/0 area 0.0.0.0
   max-lsa 10000

The sed utility has many commands, flags, & use-cases for which I only intend to scratch the surface here and provide just enough interest for you to feel motivated to investigate this powerful stream editor more on your own. The following command will process my.config and output the result to stdout without overwriting my.config. The basic syntax can utilize regex (regular expressions) in order to match an input pattern and then substitute in a replacement string. In the following example “s” means “substitute”, “g” means “global” (ie. if a line had multiple occurrences of “10.0” they’d all be replaced, as opposed to just the first occurrence encountered per line). The pattern follows the “s/” flag and the replacement precedes “/g” flag.

NOTE: There are “[” & “]” (square brackets) surrounding the “.” this is because in the world of RegEx the “.” is a wildcard and the RegEx would inadvertently match on my OSPF router process number & and the max-lsa parameter too, thereby incorrectly changing them. The square brackets in the RegEx expression mean to treat the “.” literally.

[admin@eos temp]$ sed s/10[.]0/11.4/g my.config
interface Loopback0
   ip address 11.4.0.3/32
ip routing
router ospf 1000
   router-id 11.4.0.3
   passive-interface default
   no passive-interface Vlan1
   network 0.0.0.0/0 area 0.0.0.0
   max-lsa 10000

The following command with the “-i.bak” flag (“-i”) & suffix (“.bak”, the suffix can be anything) will process the file my.config and output (and overwrite) the result to the original file while creating a backup of the original in a file called my.config.bak (NOTE: Use sudo as you are creating a new file).

[admin@ temp]$ sudo sed -i.bak s/10[.]0/11.4/g my.config
[admin@ temp]$ ls -al
total 16
drwxrwx---  2 root eosadmin 4096 Dec 28 19:10 .
drwxrwx--- 10 root eosadmin 4096 Dec 28 18:09 ..
-rwxrwx---  1 root eosadmin 2203 Dec 28 19:10 my.config
-rwxrwx---  1 root eosadmin 2203 Dec 28 19:10 my.config.bak

[admin@ temp]$ cat my.config (overwritten file)
interface Loopback0
   ip address 11.4.0.3/32
ip routing
router ospf 1000
   router-id 11.4.0.3
   passive-interface default
   no passive-interface Vlan1
   network 0.0.0.0/0 area 0.0.0.0
   max-lsa 10000

[admin@ temp]$ cat my.config.bak (original file)
interface Loopback0
   ip address 10.0.0.3/32
ip routing
router ospf 1000
   router-id 10.0.0.3
   passive-interface default
   no passive-interface Vlan1
   network 0.0.0.0/0 area 0.0.0.0
   max-lsa 10000

To see an example that takes further advantage of the “\g” flag, look at the following example. NOTE: “!!” is a comment in the Arista CLI, and make sure you also intend to change the IP address in this comment!

[admin@ temp]$ cat my-other.config
interface Vxlan1
   !! NOTE TO SELF: 123.3.2.1, 10.0.0.101
   vxlan udp-port 4789
   vxlan vlan 1000 flood vtep 10.0.100.1 10.0.100.2 10.0.200.4 30.30.30.1

[admin@ temp]$ sudo sed -i.bak s/10[.]0/44.44/g my-other.config
[admin@ temp]$ cat my-other.config (overwritten file)
interface Vxlan1
   !! NOTE TO SELF: 123.3.2.1, 44.44.0.101
   vxlan udp-port 4789
   vxlan vlan 1000 flood vtep 44.44.100.1 44.44.100.2 44.44.200.4 30.30.30.1

And one more (albeit rather non-sensical) example that utilizes some whitespace. When there is whitespace, use the ‘ (single quotes) as shown below:

[admin@ temp]$ sudo sed -i.good 's/interface Ethernet/GOO GOO/g' my.config

[admin@ temp]$ cat my.config.good (original file)
interface Ethernet48
   switchport access vlan 20
interface Ethernet49/1
   switchport access vlan 20
interface Ethernet50/1
   switchport access vlan 20

[admin@ temp]$ cat my.config (overwritten file)
GOO GOO48
   switchport access vlan 20
GOO GOO49/1
   switchport access vlan 20
GOO GOO50/1
   switchport access vlan 20

As always make sure to confirm the changes made by sed are the changes that you expected to make. You may have an MD5 hash that generated the same pattern that you are trying to match and then suddenly the behavior of the system changes.

sed can be extremely powerful and I encourage you to learn more about sed with sed –help and also visit http://www.gnu.org/software/sed/

These examples were performed on an Arista 7280SRA-48C6 running EOS version 4.25.0F.

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: