Showing posts with label IOS. Show all posts
Showing posts with label IOS. Show all posts
IOS shell processing, variables and router-ids

IOS shell processing, variables and router-ids

IOS 15.1(4)M brought some nice cool new stuff with it. With it was shell processing, which opens up a plethora of linux commands.

Enabling shell processing on Cisco IOS

To enable shell processing we use the command:
R17(config)#shell processing full
R17(config)#
This then allows us to use lot's of new commands. To see the new commands type in "man":
R17#man
((              evaluate a numeric test expression
IOS.sh          The IOS shell
[[              evaluate a logical test expression
cat             output data from a pipe or file to the terminal
compatibility   compatibility information on IOS.sh
control         control constructs in IOS.sh
cut             edit piped output
echo            echo arguments to the terminal
expressions     usage of expressions in IOS.sh
false           return false in while or if expressions, and set the result
fetch           return values from the configuration database
for             IOS.sh for loops
functions       IOS.sh function facility
grep            search for regular expressions in piped output or files
head            print the first lines in the input
if-else         IOS.sh if command
interface       print interfaces that match the argument
let             evaluate a numeric expression, and set the result
man             print information for builtins
more            page piped output to the terminal
nl              number the lines in the input
null            ignore the input
printf          output formatted data to the terminal
quoting         IOS.sh quoting facility
read            read input into variables
scripting       how to script the IOS CLI
set_oper        set operational values
sleep           pause execution of the terminal
sort            sort the input
tail            print the tail of the input
true            return true in while or if expressions, and set the result
uname           print system information
variables       usage of variables in IOS.sh
wc              count lines, words, and chars
while           iterate while an expression is TRUE

R17#

Using grep to limit output

Now that we have enabled shell processing, we can use "grep":
R17#sh run int lo0 | grep 'address'
 ip address 123.17.17.17 255.255.255.255

R17#
This gives us the ip address.

Using cut to get an IP address

We can then use "cut" to return just the numbers:
R17#sh run int lo0 | grep 'address' | cut -c 13-24
123.17.17.17

R17#
This can be useful, as we can turn this command into a variable.

Why would we do this? Well, for certain things, such as setting router-ids for routing protocols, we would usually hard-code this to be the loopback interface's IP address. When switching from one router to the next it's easy to forget what the IP address is. You can always use "do sh run int lo0", but then it's easy to lose the flow, having it set as a variable makes life much easier.

Creating a variable in IOS shell

To create a variable we use the command "variable=variableoutput":
R17#varMyLo0=`sh run int lo0 | grep 'address' | cut -c 13-24`
Here, we create a variable called "varMyLo0". The backticks set the output of the command as the actual variable (the IP address).

Using a variable to set a router-id

We can then use this variable when configuring the device:
R17(config)#router bgp 100
R17(config-router)#bgp router-id $varMyLo0
R17(config-router)#do sh run | sec bgp
router bgp 100
 bgp router-id 123.17.17.17
 bgp log-neighbor-changes
R17(config-router)#exit
If we do this again, just to prove it works, for OSPF, we can see that OSPF is not running, we can create the OSPF process, and, again, use the variable to set the router ID:
R17#sh run | sec router ospf
R17#conf t
R17(config)#router ospf 100
R17(config-router)#router-id $varMyLo0
R17(config-router)#end
R17#sh run | sec router ospf
router ospf 100
 router-id 123.17.17.17
R17#
So, now, you don't need to keep looking at the "sh run int lo0" to remind yourself of the IP address, and type it in manually, or copy and paste it.

If the IP address is shorter (say 123.1.1.1) then the cut would be shorter (cut -c 13-21).

This could save some time during the exam!

If you have any good shell processing tips, then feel free to comment with them!

Cisco VIRL is out! It's not free!

Cisco VIRL

Cisco VIRL has been released today.

After waiting for what seems like ages, since it was first mentioned, it is finally here.

You can get it from http://virl.cisco.com.

The rumours were that is would be free, and would run up to fifteen (15) nodes, that could be either IOSV, IOS XRv, NX-OSv, or the CSR1000v.

While the capabilities remain the same, it is not free. It costs $199.99 PER YEAR.

This does seem to be a bit of a kick to the groin, really. Whilst this is the true, Cisco virtual environment, with all the support and abilities needed (obviously barring a few layer 2 technologies), $200 a year is a little costly.

For a limited time you can get $50 off using the code "VIRL50" at checkout. Which still makes it $150. It's not a bad price I suppose, but many people did expect this to be released without an associated cost.

I think I will stick with IOU and GNS3 for the moment!
Managing multiple configurations on Cisco IOS routers and switches

Managing multiple configurations on Cisco IOS routers and switches

In the journey to CCIE there are a great number of labs that we can be doing, such as INE, Narbik, CCIECert, Cisco 360 etc etc. So chances are our switches and routers are going to be reloaded a number of times with varying configurations.

Thankfully we can make the process a little easier and store a number of configurations on the equipment and change between these at our leisure.

The IOS supports a number of commands that can help us with this, so lets have a look at how we can manage the respective configs for a number of different labs.

The first command is mkdir, we can make a directory on the flash storage with the command
mkdir flash:/myConfigs
and confirm that its been created using the command
dir flash:
The output should look like this:
Router#dir flash
Directory of flash:/

1 -rw- 20570916 Jun 6 2000 19:19:48 +00:00 c1841-spservices9-mz.124.16b.bin
2 drw- 0 Jul 23 2013 11:11:42 +00:00 myConfigs

So now we have a nice place holder for our respective configs and we can begin filling this with our configuration files.

So lets make some very basic configurations. I am using an 1841 router, its got a serial interface that will connect to the BB1 router (the frame-relay backbone).

I assign a description to the Serial 0/0/0 interface:

Interface Serial0/0/0 
 description Link to BB1 - INE 

Like I said its a basic configuration! And now lets copy this to the myConfigs folder, and check that its there:
Router#copy run flash:/myConfigs/INE.txt
Destination filename [/myconfigs/INE.txt]?

694 bytes copied in 0.744 secs (933 bytes/sec)
Router#dir flash:/myconfigs
Directory of flash:/myConfigs/

12 -rw- 694 Jul 23 2013 11:20:28 +00:00 INE.txt

On the second line just hit enter to accept the offered destination filename.

So we now have one configuration. Lets make another one:

Router#conf t
Router(config)#int Serial 0/0/0
Router(config-if)#desc Link to BB1 - 360
Router(config-if)#exit
Router(config)#exit
Router#copy run flash:/myConfigs/360.txt
Destination filename [/myconfigs/360.txt]?

694 bytes copied in 1.268 secs (547 bytes/sec)
Router#dir flash:/myconfigs
Directory of flash:/myConfigs/

12 -rw- 694 Jul 23 2013 11:20:28 +00:00 INE.txt
13 -rw- 694 Jul 23 2013 11:24:16 +00:00 360.txt

So now we have two configurations - but how can we easily switch between them?

One option is to copy the file name back to the startup config and reload the router

Router#copy flash:/myConfigs/INE.txt startup-config 

But this means your router is down for a few minutes. Another way is to use the configure replace command.

At the moment our router is running the 360 config, we can see this by doing sh run | beg Serial0/0/0 and we can see the following:

Router#sh run | beg Serial0/0/0 
interface Serial0/0/0           
description Link to BB1 - 360   
no ip address                   
!                               

To make our router run the INE configuration we can use the command configure replace and specify the file we are going to substitute as the current running config:

Router#configure replace flash:/myConfigs/INE.txt    
This will apply all necessary additions and deletions    
to replace the current running configuration with the      
contents of the specified configuration file, which is      
assumed to be a complete configuration, not a partial       
configuration. Enter Y if you are sure you want to proceed. ? [no]: Y   
Total number of passes: 1        
Rollback Done                  

Router#                                                                  
*Jul 23 11:33:32.947: Rollback:Acquired Configuration lock.                
Router#                                                                 
*Jul 23 11:33:34.035: %PARSER-3-CONFIGNOTLOCKED: Unlock requested by process '3'. Configuration not locked.                                                        
Router#sh run | beg Serial0/0/0                                            
interface Serial0/0/0                                                        
description Link to BB1 - INE                                          
no ip address                                                              
!  

So now we can easily and quickly switch between multiple configurations!

We can also push multiple configurations onto the flash file system through tftp, or another method such as SCP. Here I am using SCP, firstly I copy a configuration to the SCP client hosted on a laptop, then I edited the file and saved it under the name CCIECert.txt, and then copied it back to the router. Lastly I did a configure replace to set it as the current running config:
Router#copy flash:/myConfigs/INE.txt scp:
Address or name of remote host []? 10.250.1.10
Destination username [Router]?
Destination filename [INE.txt]?
Writing INE.txt
Password:
!
694 bytes copied in 12.968 secs (54 bytes/sec)

Router#copy scp: flash:/myConfigs/CCIECert.txt
Address or name of remote host []? 10.250.1.10
Source username [Router]?
Source filename []? CCIECert.txt
Destination filename [/myConfigs/CCIECert.txt]?

Password:
!
699 bytes copied in 11.392 secs (61 bytes/sec)

Router#dir flash:/myConfigs
Directory of flash:/myConfigs/

   12  -rw-         694  Jul 23 2013 11:20:28 +00:00  INE.txt
   13  -rw-         694  Jul 23 2013 11:24:16 +00:00  360.txt
   14  -rw-         699  Jul 23 2013 12:25:12 +00:00  CCIECert.txt

Router#configure replace flash:/myConfigs/CCIECert.txt
This will apply all necessary additions and deletions
to replace the current running configuration with the
contents of the specified configuration file, which is
assumed to be a complete configuration, not a partial
configuration. Enter Y if you are sure you want to proceed. ? [no]: y
Total number of passes: 1
Rollback Done

Router#
*Jul 23 12:26:41.679: Rollback:Acquired Configuration lock.
*Jul 23 12:26:43.331: %PARSER-3-CONFIGNOTLOCKED: Unlock requested by process '3'. Configuration not locked.
Router#
Router#
*Jul 23 12:26:44.791: %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to administratively down
*Jul 23 12:26:45.791: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to down
Router#sh run | beg Serial0/0/0
interface Serial0/0/0
 description Link to BB1 - CCIECert
 no ip address
!

So there we have it, its easy to host multiple configurations and switch between them quickly. just remember to save your work as you go!