Alias – Simple yet powerful

Alias – Simple yet powerful

About: Alias mySimpleAlias <a maybe complicated command you would never remember>

Alias commands can be composed of multiple lines and embed variables. Below is an example of alias used as configuration template for automating configuration with just few arguments. Sunch template can satisfy complex configurations and be highly reusable. This high-level scripting or command bundling is simple to implement yet powerful.

The below example is a multi-line alias with variables (%<x>)

alias set-baremetal

!! Syntax : set-baremetal <INTF> <Po ID> <DESCR> <VLAN>

!! Example: set-baremetal e1,2 po1 “To Server 42” 200

10 config

20 interface %1

30 channel-group %2 mode active

40 description Physical link part of %2

50 interface %2

60 description Port-Channel to %3

70 switchport mode access

80 switchport access vlan %4

90 spanning-tree portfast edge

Note: You configure a multi-line alias either by:

  1. Entering “alias <name>”

  2. Either:

    1. <Line number> <EOS command line>

      1. This will use the line ID you specify

      2. This is the method I would recommend, because it is mirrors the EOS config.

    2. “command” <EOS command line>

      1. This will auto-generate a line ID for you (by increment of 10), the line numbers are optional, automatically added, like with ACLs,

      2. This is useful if you are lost about where you are in the numbering (err, where was I: line 60 or 70?).

      3. Like for ACLs, the config is not active until you exit the alias config hierarchy so “show active” or “sh run” is not usful.

      4. However “sh history” shows what you typed so you should know.

  3. Both typing methods generate the same config, with line IDs:

alias <name>

10 <cmd1>

20 <cmd2> …

The following section details the previous alias set-baremetal, commenting almost excessively on purpose to explain in details each line.

!

config

alias set-baremetal

^ name of the alias

!! Syntax : set-baremetal <INTF> <Po ID> <DESCR> <VLAN>

^ “!!” applies a custom comment that stays in-line of the config, they are not executed (ignored by CLI), so very useful to document your config in-line. Recommendation: keep-it < 80 chars wide, unlike this comment about the comment.

!! Example: set-baremetal e5,10 po1 43 “To Server 42”

^ comments can be multi-line

10 config

^ you can explicitly number aliases line for later easily inserting after-thought extra lines. By default the increment is by 10, which is usually enough

20 interface %1

^ %<x> is a variable, where <x> is the argument passed to the alias. Exactly like you pass argument to a command in any OS CLI. If I pass Eth1,10-11 (no spaces) or “Eth 1, 10- 11” (spaces at will, with quotes) then this line 20 will become “interface Eth1,10-11”

30 channel-group %2 mode active

^%2 is the 2nd argument passed to the alias.

40 description Physical link part of %2

^ I reuse the same argument twice

50 interface %2

^ As many times as you want. Useful for Po, customer name, service type, etc

60 description Port-Channel to %3

^ you can have as many arguments as you want, but cannot nest or call an alias inside an alias (no recursive look-up / resolution)

70 switchport mode access

80 switchport access vlan %4

^ This alias look like an DevOps tool template, a bit simpler, but no skill and no time is needed to learn, research or maintain it.

90 spanning-tree portfast edge

!

The following shows the step-by-step execution of a multi-line alias

arista(config)#

arista(config)#set-baremetal ?

Ethernet hardware Ethernet interface

Loopback Loopback interface

Management Management interface

Port-Channel Lag interface

Tunnel Tunnel interface

Vlan VLAN interface

Vxlan Vxlan Tunnel Interface

create Create interfaces

^ one of the nicest aspects of aliases is the context-awareness. Note that the question mark (‘?’) raises a valid contextual help: the 1st argument after the alias name is indeed an interface type. The CLI knows that the 1 line in your alias, line 20, has got “interface %1” and the next argument should be an interface type.

Auto-complete also works.

arista(config)#set-baremetal eth1,5 ?

% Unrecognized command

^ Now we hit a limitation, because we are currently inside the Po1 config hierarchy, the CLI does not recognize that interface command could be executed in global config mode, it is invalid.

arista(config-if-Po1)#int e1

^ If working on interfaces, jumping to alike config mode would satisfy the native contextual help + autocomplete. It will do nothing to where you go, it is just used as a contextual helper. It will only apply the config where you specify in your alias. This does not work if you alias jumps between config modes, for example interface, then ACL, then else.

arista(config)#int e1

arista(config-if-Et1)#set-baremetal ethernet5,10 ?

<1-2000> Channel Group ID

^Now it works, although it will configure Eth5,10. The current Eth1 is only used as context helper.

arista(config-if-Et1)#set-baremetal ethernet5,10 5 ?

<1-4094> VLAN id

^ next argument

arista(config-if-Et1)#set-baremetal ethernet5,10 5 45 ?

LINE Up to 240 characters describing this interface

^ next

arista(config-if-Et1)#set-baremetal ethernet5,10 5 45 “To My Baremetal Server”

LINE Up to 240 characters describing this interface

<cr>

^ finished, can now <cr>

arista(config-if-Et1)#set-baremetal ethernet5,10 5 45 “To My Baremetal Server”

! Access VLAN does not exist. Creating vlan 45

^ everything is executed, going to config, to the interfaces, applying everything and automatically creating the VLANs

arista(config-if-Po5)#

^ Note that we finish in the config mode where the alias was last, the port-channel interface. You could add an ‘exit’ if you wanted to return to global config mode.

If you want to create several similar aliases, such like the following “set-esx” one, you would require to include some repetitive parts also present in our precedent alias. It’s not possible to embed a generic alias into another one or to share a common block across aliases. There is no hierarchy or sharing possible. Every multi-line alias must be a complete standalone:

!

alias set-esx

!! Syntax : set-esx <INTF> <Po ID> <DESCR>

!! Example: set-esx ethernet1,2 1 “Port-channel to ESXi”

10 config

20 interface %1 <==== repetitive

30 channel-group %2 mode active <==== repetitive

40 description Physical link part of Po %2 <==== repetitive

50 interface po %2

60 description Port-Channel to %3

70 switchport mode trunk

80 no switchport access vlan

90 spanning-tree portfast edge

!

Nesting aliases is not possible, as there is no recursive lookups..

But there is a way:

It involves calling the aliases you want to nest with bash calls, treating the aliases like sub-modules.

To understand this method, first remember that EOS CLI <==> Bash CLI are fully interactive:

  1. From EOS:

bash <Bash command>

  1. From Bash:

FastCli -c <EOS command>

An alias cannot call an alias, but can call a bash script.

In summary:

From EOS CLI: alias_main calls a bash script that calls EOS CLI for sub-aliases

To break down the method, recall that the user command will not change, we have the same parameters as before:

set_baremetal eth1,2 12 Server12 42

However, the alias set_baremetal now looks different, it is much simpler and logically structured, it calls some simple bash scripts:

!

alias set_baremetal

10 bash /mnt/flash/common.sh %1 %2 %3

100 bash /mnt/flash/access.sh %2 %4

!

^ The common section does not need the VLAN (-> we omit passing %4)

The module to configure a Po trunk as access ports does need the VLAN info but not the physical ports details (-> we pass %2 and %4 only)

The two below scripts are very tiny (1 line each), just calling the relevant aliases. The main set_baremetal alias will execute everything in order.

cat common.sh

#!/bin/bash

FastCli -p 15 -c $”submodule_common $1 $2 $3″

cat access.sh

#/bin/bash

FastCli -p 15 -c $”submodule_access $1 $2″

cat trunk.sh

#/bin/bash

FastCli -p 15 -c $”submodule_trunk $1″

The aliases become just templates/modules that you call for your relevant configuration tasks:

alias set_baremetal

alias set_esx

common part –> single “common” alias template

10 config

20 interface %1

30 channel-group %2 mode active

40 description Physical link part of %2

Unique –> unique “access” template

Unique –> unique “trunk” template

50 interface po %2

60 description Port-Channel to %3

70 switchport mode access

80 switchport access vlan %4

90 spanning-tree portfast edge

50 interface po %2

60 description Port-Channel to %3

70 switchport mode trunk

80 no switchport access vlan

90 spanning-tree portfast edge

Note that the goal was to make the aliases modular, so different similar aliases can reuse common blocks. This is what the below achieves.

!

alias set_baremetal

10 bash /mnt/flash/common.sh %1 %2 %3

100 bash /mnt/flash/access.sh %2 %4

!

alias set_esx

10 bash /mnt/flash/common.sh %1 %2 %3

100 bash /mnt/flash/trunk.sh %2

!

alias submodule_access

100 config

105 vlan %2

110 interface po %1

120 switchport mode access

130 switchport access vlan %2

!

alias submodule_common

10 config

20 interface %1

30 channel-group %2 mode active

40 description Physical link part of Po %2 for %3

50 interface po %2

60 spanning-tree portfast edge

70 description Port-Channel to Server %3

!

alias submodule_trunk

100 config

110 interface po %1

120 switchport mode trunk

130 no switchport access vlan

!

This article explored alias as a useful tool with potential usefulness in some cases, and as point of interest, but it is not scalable or flexible enough to provide advanced automation.

Other steps going away from the “quick and dirty” alias-based templating are:

Bash, Python, Golang, Ruby script – still simple but more flexible and more powerful, can run locally. It does not scale because it lacks native distribution, but suits non-config scripting or purposely distributed intelligence.

DevOps tools (Chef, Puppet, Ansible, Salt stack): ideal for scalable config management and verification, validation.

eAPI from your customer controller or orchestration tool: Build your own DevOps tool or orchestrator with just few lines of scripts, or 1000s. Simple or advanced, build what you want.

SDK: demanding skilled-wise, but giving low-to-medium level control to the OS, executing fast compiled code, interacting with EOS (trigger-based exchanges), it is the upmost reactive programmatic method, not suitable for config-management but when microsecond to millisecond reactivity matters (DDoS, convergence, build your own protocol, populate the hardware tables the way you want).

Another use case for alias is the following VRF-change disconnect-protection.

As you might know, changing the VRF on a Layer3 interface cause the forceful necessary removal of its IP address.

For example:

Arista(config-if-Et1)#show active

interface Eth1

ip address 10.0.0.1/24

Arista(config-if-Et1)# ip vrf Mgt

<Arg lost IP>

^ not ideal when you access the device through that interface, because you would have just cut yourself out.

Luckily there is config session, allowing you to prepare a bunch of config without effect until you decide to apply it.

Otherwise, there is also the following multi-line alias option.

The main point is that ALL the lines will be executed, whether you are still there or not after it starts. So you can safely trust it to run till the end. In the below example it would guarantee you that the IP address will be re-applied and the switch reachable again, making this kind of stressful change low-risk.

[Credits to Sean Flack]

alias changevrf

!! multi-line alias

!! syntax: changevrf <int> <vrf> <IP>

!! example: changevrf ma1 MGMT 172.22.30.166/23

2 conf

4 int %1

6 vrf forwarding %2

8 ip address %3

10 end

Another use case for aliases is pure laziness, I like to put in my alias some config lines examples a bit too complex to remember, so I would normally store them (and lose them!) in emails, text files, cloud drives, manuals, blogs, bookmarks, etc. Instead I just include everything useful in different aliases, so I can simply retrieve and execute instantly these precious commands in my aliases, it becomes a bank of useful commands.

It might seem like the config becoming too verbose. Many examples are useful for production, few others are rather destined for lab or demo purpose, but all are shown here for you to pick what works best for you

!! The alias ‘backup’ is copying both startup and running configs,

!! timestamping them, diff them, (you could send an email if there is

!! a diff)

!! Sources at https://git.io/vHcdJ

alias backup bash /mnt/flash/configpush_scp.sh <user> <server> [<url>]

!! I can never remember my test email details, so I use the below.

!! An alias might not be the best place for this, but it’s convenient

alias setemail

10 config

20 email

30 from-user leaf1@dummy.lab

40 server vrf default smtp.gmail.com:587

50 auth username ad.arista.01

60 auth password 7 P1Nv3UFpXFgsJ9sQsQE62A==

70 tls

!! send an email with the disk usage.

alias email-bash-df bash df -h | email -s “[DiskUsage on $HOSTNAME]” test@customer.com

!! another email method, but this time the output is in the email body

!! rather than being sent as an attachment. Handy to see faster

alias email-bash-df-body bash df -h | email -i -s “[DiskUsage on $HOSTNAME]” test@customer.com

!! Yet another email method, changing the “from” sender to be the hostname

alias email-show-version show version | email test@customer.com -s “[show version] From $HOSTNAME”

!! Now in the email body. That is probably enough email examples

!! This was just for you to choose your preferred option among all

alias email-show-version-body show version | email test@customer.com -i -s “[show version] From $HOSTNAME”

!! Bypass the CLI check for what the hardware supports. So you can

!! configure forbidden things (NAT on all switches, although it won’t

!! be applied in HW). You might find it easier to test some commands,

!! especially on vEOS-lab for testing purpose. On production devices you

!! might instead find ways to break things, maybe.

!! Be careful, this is for lab/testing only.

alias enable-unsupported-cli-commands bash Cli -G

!! Nice awk + regexp, to get all the interfaces IP addresses

alias getTheIPs show int | awk ‘/^[A-Z]/ { intf = $1 } /Internet address is/ { print intf, $4 }’

!! variant for VRF awareness

alias getTheIPsVRFaware bash sudo -i sh -c ‘for i in `ip netns list`; do echo $i; ip netns exec $i ip addr list | grep inet; done’

!! Get all the MAC addresses across all the interfaces

alias getTheMACs show int | awk ‘/^[A-Z]/ { intf = $1 } /Hardware .* address is/ { print intf, $6 }’

!! Get LLDP neighbour from every switches in the network.

!! It assumes you have XMPP setup

alias lldp-everywhere xmpp send all@conference.lab command show lldp nei | exclude Number of

!! Find the next available VLAN. Convenient to allocate a new VLAN

!! dynamically.

alias nextvlan show vlan | awk ‘BEGIN { m=1 }; m && $1 ~ /[0-9]/ && $1!=++a { print a; m=0 }; END { if (m) print ++a }’

!! captures a LLDP table, save historical records and then send an

!! email if there was a diff with the previous capture

!! The script can be found at:

!! https://git.io/vH2Er

alias lldp_diff bash /mnt/flash/lldp_diff.sh

!! For production this script would be more suited as a schedule:

schedule lldp_diff interval 60 max-log-files 10 command bash /mnt/flash/lldp_diff.sh -e user@domain.com

!! Make a config change from bash, logging who did the change.

!! Useful for AEM event-manager

alias demo_custom_Bash_Action_with_log bash LOGNAME=MyAutomation REALTTY=MySession SSH_CONNECTION=123.123.123.123 Cli -p 15 -c $’enable\nconfig\ninterface Eth1\ndescriptionThis port changed status’

!! saves the startup-config to the flash with a timestamp.

!! Recommended with AEM event-manager for every config change.

alias config_versioning bash FN=/mnt/flash/startup-config; LFN=”`ls -1 $FN.*-* | tail -n 1`”; if [ -z “$LFN” -o -n “`diff -I ‘last modified’ $FN $LFN`” ]; then cp $FN $FN.`date +%Y%m%d-%H%M%S`; ls -1r $FN.*-* | tail -n +11 | xargs -I % rm %; fi

!! For lab testing – When you want to go in maintenance bypassing all

!! change control. Includes the useful commands outputs to monitor the process

alias go-maintenance

10 config

20 maintenance

30 unit System

40 quiesce

50 show maintenance

60 show ip bgp summary

!! Ethxmit is a software traffic generator available on all EOS,

!! extremely customizable. Check all the options with bash ethxmit –help

alias sendmcast bash sudo ethxmit -S 00:1C:73:00:44:D6 -D 01:00:5e:6f:ef:01 –ip-src=181.181.181.1 –ip-dst=239.239.239.1 -n 1000 et1

!! A single alias to send 7 flows with 7 QoS profiles. Note the CoS and

!! ToS values are already set. (CoS != IP Prec != ToS != DSCP). Ethxmit

!! uses CoS and ToS, so get your DSCP-to-ToS right.

!! Note that by default ethxmit use a dummy eth-type. Use 0x0800 for

!! valid Eth. The packet counts make each flow unique and easily

!! identifiable in the “sh int EthX count bin”/queues outputs

!! It’s faster than getting and configuring an Ixia/Spirent.

!! It works with vEOS too.

alias traffic-gen-7qos

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=0 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=0 et99 -n 100

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=1 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=40 et99 -n 200

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=2 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=72 et99 -n 400

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=3 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=104 et99 -n 800

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=4 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=136 et99 -n 1600

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=5 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=184 et99 -n 3200

command bash sudo ethxmit -e 0x0800 -s 512 –vlan=999 –vpri=6 -D 00:1c:73:a4:8a:4d –ip-src 99.0.0.2 –ip-dst=10.123.128.1 –tos=192 et99 -n 6400

!! This is the LLDP automatic port description script that I demo often

alias getportauto https://raw.githubusercontent.com/arista-eosext/PortAutoDescription/master/portAuto

!! Best prompt ever

alias best_prompt

10 config

20 prompt %H%v__%D{%H:%M:%S}%P

!! Here are some aliases commonly used for troubleshooting in EOS

alias log bash sudo cat /var/log/messages

alias ls bash ls -lrt /var/log/agents

alias senz show interface counter error | nz

alias shmc show int | awk ‘/^[A-Z]/ { intf = $1 } /, address is/ { print intf, $6 }’

alias snz show interface counter | nz

alias spd show port-channel %1 detail all

alias sqnz show interface counter queue | nz

alias srnz show interface counter rate | nz

alias nzdrop show inter counters queue det | egrep -v ‘\ 0$’

alias dontdothisitisbroken bash cobra -m Acons Systerdb

What are your favourite aliases?