Creating Command Aliases

Problem

You want to create aliases for commonly used or complex commands.

Solution

You can create command aliases on your router with the alias command:

Router1#configure terminal Enter configuration commands, one per line. End with CNTL/Z. Router1(config)#alias exec rt show ip route Router1(config)#alias exec on show ip ospf neighbor Router1(config)#end Router1#

 

Discussion

Unix system administrators have been using command aliases for many years to help reduce typing and save time. These shortcut commands allow you to reduce long or complex command sequences to a few simple characters. This is most useful for extremely common commands, and for commands that are complex or difficult to remember. You can create an alias for any command, including some or all of its associated keywords or variables.

Here we have created the alias, rt, for one of the most common commands we use everyday, show ip route:

Router1(config)#alias exec rt show ip route

We can now use this simple two-letter command to display the routing table, saving time and typing:

Router1#rt Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area * - candidate default, U - per-user static route, o - ODR P - periodic downloaded static route Gateway of last resort is 172.25.1.1 to network 0.0.0.0 S 192.168.10.0/24 [1/0] via 172.22.1.4 172.16.0.0/24 is subnetted, 1 subnets C 172.16.2.0 is directly connected, FastEthernet0/0.2 172.20.0.0/16 is variably subnetted, 3 subnets, 3 masks O 172.20.10.0/24 [110/74] via 172.20.1.2, 00:52:55, Serial0/0.2 C 172.20.1.0/30 is directly connected, Serial0/0.2 O 172.20.100.1/32 [110/65] via 172.20.1.2, 00:52:55, Serial0/0.2 172.22.0.0/16 is variably subnetted, 2 subnets, 2 masks D 172.22.0.0/16 is a summary, 20:31:03, Null0 C 172.22.1.0/24 is directly connected, FastEthernet0/1 Router1#

The key to choosing a good alias command name is to pick something that is short and easy to remember. Of course, it is critical to select an alias that does not conflict with an existing command. In our example, we choose rt as a short, easy-to-remember mnemonic for "Route Table" that didn't conflict with any existing IOS command.

You can also use a command alias as part of a longer command. For example, we could use our rt alias to shorten the command show ip route 172.16.2.0:

Router1#rt 172.16.2.0 Routing entry for 172.16.2.0/24 Known via "connected", distance 0, metric 0 (connected, via interface) Routing Descriptor Blocks: * directly connected, via FastEthernet0/0.2 Route metric is 0, traffic share count is 1 Router1#

Command aliases are most effective if you use them consistently among all the routers that you manage. Otherwise, you'll have to remember a different set of alias commands for each group of devices. If you want to use this feature, we recommend that the entire network management team work together to develop a standard set of aliases before implementing them. We also recommend keeping the aliases simple. Above all, resist the urge to alias every possible command. Instead, create aliases for only the most common commands.

Command aliases are also useful for scripting. You can build a script to perform a task on a router that might be slightly different on each router. For example, suppose you want to clear the counters of a particular access list on a weekly basis, but some of your routers use a different access list number. You can simply build an alias with the same name on each router, but make the actual commands represented by the alias appropriate to each individual router. Finally, you can build a script to issue the command alias and automate what would otherwise be an extremely onerous task.

The show alias command displays all the command aliases configured on the router:

Router1#show aliases Exec mode aliases: h help lo logout p ping r resume s show u undebug un undebug w where rt show ip route on show ip ospf neighbor Router1#

If you type this command on any router, you will see that Cisco implements several command aliases by default.

Категории