Archive

Archive for the ‘Linux’ Category

Sed Tips


Note: This sed tips are more focused on how sed can be used along with Cisco NXOS to format the output. Some syntax may be slightly different from the linux one, but concepts remain the same

    1. Simple Find and replace : How to use sed to replace multiple spaces in an output with a single space
      show ip arp | sed ‘s: \+: :g’
      Note: 
      The sed command will look for zero or more(+) spaces and replace it with a single space

      N6K-SPINE3# show ip arp | head lines 11
      
      Flags: * - Adjacencies learnt on non-active FHRP router
             + - Adjacencies synced via CFSoE
             # - Adjacencies Throttled for Glean
             D - Static Adjacencies attached to down interface
      
      IP ARP Table for context default
      Total number of entries: 7631
      Address         Age       MAC Address     Interface
      37.40.51.101    00:18:33  5801.0500.0028  Vlan3740
      37.40.51.102    00:18:33  5801.0500.0050  Vlan3740
      % cli: command canceled
      N6K-SPINE3# show ip arp | head lines 11 | sed 's: \+: :g'
      
      Flags: * - Adjacencies learnt on non-active FHRP router
       + - Adjacencies synced via CFSoE
       # - Adjacencies Throttled for Glean
       D - Static Adjacencies attached to down interface
      
      IP ARP Table for context default
      Total number of entries: 7631
      Address Age MAC Address Interface
      37.40.51.101 00:18:41 5801.0500.0028 Vlan3740
      37.40.51.102 00:18:40 5801.0500.0050 Vlan3740
      % cli: command canceled
      N6K-SPINE3#
      
    2. Advanced Find and Replace: How to remove duplicate spaces in an output as well as replace space between two words(alpha numeric) with a hyphen(-).
      Note: This is really useful when we are using cut command to select columns with a delimiter as space. Some outputs will have column members having values with space and we need to replace those spaces with hyphen else the cut logic will get screwed up

      N6K-SPINE3# show platform afm info copp-tbls 
      COPP table                                   
      ----------                                   
      * mgmt/ipv6-mgmt - http,snmp,ntp,telnet,ssh,ftp
      ** excp/ipv6-excp - mtu failure, martian address, punt, l3 header errors, icmp errors
      
      Policer-Num  Name                  CIR       Burst     Passed-Bytes  Dropped-Bytes
      ----------------------------------------------------------------------------------
      0            default               64000     6250      41239419        0             
      1            stp                   2500000   4687      12456664        0             
      2            lacp                  128000    4687      171548112       0             
      3            fc control            3750000   65535     0               0             
      4            fc errors             125000    65535     0               0             
      5            fc data               125000    65535     0               0             
      6            lldp                  256000    4687      428894355       0             
      7            sat control           62500000  65535     0               0             
      8            sat data              250000    16000     0               0             
      9            udld                  256000    4687      170310698       0             
      10           igmp/ipv6-mld         128000    63        1998396656      200172        
      11           fip                   128000    4687      0               0             
      13           dhcp/ipv6-dhcp        128000    4687      9555449676      1213208586    
      14           L3 data hi            2500000   4096      0               0             
      15           L3 data low           1250000   512       0               0             
      16           L3 front panel rdt    250000    2048      0               0
      17           isis                  128000    4687      6499005632      0
      18           cdp                   128000    4687      186341581       0
      19           dot1x                 125000    65535     0               0
      20           pim/ipv6-pim          128000    4687      4997316070      0
      21           mgmt/ipv6-mgmt*       1500000   4687      0               0
      23           arp/ipv6-nd           128000    3515      1373801198      0
      24           rip                   1200000   4687      0               0
      25           ospf/ipv6-ospf        1200000   4687      115913712       0
      26           eigrp/ipv6-eigrp      1200000   4687      0               0
      27           hsrp vrrp/ipv6-hsrp   128000    250       119511901630    0
      29           bgp/ipv6-bpg          1200000   4687      0               0
      30           mcast first hop       1200000   4687      0               0
      31           ping                  8000      3515      0               0
      33           mcast rpf fail        64000     3125      0               0
      35           ucast fib miss        8000      15        122942          2313730
      36           glean                 128000    4687      27752854        51827500
      37           msdp                  1200000   4687      0               0
      38           excption ttl          8000      3125      0               0
      39           exception same intf   8000      3125      0               0
      40           exception ip frag     8000      3125      0               0
      41           excp/ipv6-excp**      8000      4687      1734386         0
      42           bfd                   1200000   4687      5277193652      0
      43           mcast last hop        64000     3125      0               0
      44           wccp                  132500    4687      0               0
      45           onep dpss             78125     3125      0               0
      46           ecp                   800000    3125      0               0
      47           vxlan                 128000    4687      0               0
      48           nvgre                 125000    65535     0               0
      49           L3 isis               128000    4687      0               0
      50           CFSoE Rel             62500000  65535     0               0
      ----------------------------------------------------------------------------------
      
      
      N6K-SPINE3#
      

      Note: In the above output below fields in the 2nd column has spaces in between the entry like “sat control”, “sat data”, “L3 data hi”, “L3 data low”, “L3 front panel rdt”, “hsrp vrrp/ipv6-hsrp”, “mcast rpf fail”, “ucast fib miss”, “excption ttl”, “exception same intf”, “exception ip frag”, “mcast last hop”, “L3 isis”, “CFSoE Rel”

      Problem: A use case where we need to print only first three columns. For the aforementioned fields with SPACE we are unable to see the CIR 
      N6K-SPINE3# show platform afm info copp-tbls | sed 's: \+: :g' | cut -d " " -f 1-3
      COPP table                                                                        
      ----------                                                                        
      * mgmt/ipv6-mgmt -                                                                
      ** excp/ipv6-excp -                                                               
      
      Policer-Num Name CIR
      ----------------------------------------------------------------------------------
      0 default 64000                                                                   
      1 stp 2500000                                                                     
      2 lacp 128000                                                                     
      3 fc control                                                                      
      4 fc errors                                                                       
      5 fc data                                                                         
      6 lldp 256000                                                                     
      7 sat control                                                                     
      8 sat data                                                                        
      9 udld 256000                                                                     
      10 igmp/ipv6-mld 128000                                                           
      11 fip 128000                                                                     
      13 dhcp/ipv6-dhcp 128000                                                          
      14 L3 data                                                                        
      15 L3 data                                                                        
      16 L3 front
      17 isis 128000
      18 cdp 128000
      19 dot1x 125000
      20 pim/ipv6-pim 128000
      21 mgmt/ipv6-mgmt* 1500000
      23 arp/ipv6-nd 128000
      24 rip 1200000
      25 ospf/ipv6-ospf 1200000
      26 eigrp/ipv6-eigrp 1200000
      27 hsrp vrrp/ipv6-hsrp
      29 bgp/ipv6-bpg 1200000
      30 mcast first
      31 ping 8000
      33 mcast rpf
      35 ucast fib
      36 glean 128000
      37 msdp 1200000
      38 excption ttl
      39 exception same
      40 exception ip
      41 excp/ipv6-excp** 8000
      42 bfd 1200000
      43 mcast last
      44 wccp 132500
      45 onep dpss
      46 ecp 800000
      47 vxlan 128000
      48 nvgre 125000
      49 L3 isis
      50 CFSoE Rel
      ----------------------------------------------------------------------------------
      
      
      N6K-SPINE3#
      

      Sed command to the Rescue

      N6K-SPINE3# show platform afm info copp-tbls | sed 's: \+: :g' | sed 's:\([A-Z,a-z][0-9]*\) \([A-Z,a-z][0-9]*\):\1\-\2:g' | cut -d " " -f 1-3
      COPP-table                                                                                                                                   
      ----------                                                                                                                                   
      * mgmt/ipv6-mgmt -                                                                                                                           
      ** excp/ipv6-excp -                                                                                                                          
      
      Policer-Num-Name-CIR-Burst-Passed-Bytes-Dropped-Bytes
      ----------------------------------------------------------------------------------
      0 default 64000                                                                   
      1 stp 2500000                                                                     
      2 lacp 128000                                                                     
      3 fc-control 3750000                                                              
      4 fc-errors 125000                                                                
      5 fc-data 125000                                                                  
      6 lldp 256000                                                                     
      7 sat-control 62500000                                                            
      8 sat-data 250000                                                                 
      9 udld 256000                                                                     
      10 igmp/ipv6-mld 128000                                                           
      11 fip 128000                                                                     
      13 dhcp/ipv6-dhcp 128000                                                          
      14 L3-data-hi 2500000                                                             
      15 L3-data-low 1250000                                                            
      16 L3-front-panel-rdt 250000
      17 isis 128000
      18 cdp 128000
      19 dot1x 125000
      20 pim/ipv6-pim 128000
      21 mgmt/ipv6-mgmt* 1500000
      23 arp/ipv6-nd 128000
      24 rip 1200000
      25 ospf/ipv6-ospf 1200000
      26 eigrp/ipv6-eigrp 1200000
      27 hsrp-vrrp/ipv6-hsrp 128000
      29 bgp/ipv6-bpg 1200000
      30 mcast-first-hop 1200000
      31 ping 8000
      33 mcast-rpf-fail 64000
      35 ucast-fib-miss 8000
      36 glean 128000
      37 msdp 1200000
      38 excption-ttl 8000
      39 exception-same-intf 8000
      40 exception-ip frag
      41 excp/ipv6-excp** 8000
      42 bfd 1200000
      43 mcast-last-hop 64000
      44 wccp 132500
      45 onep-dpss 78125
      46 ecp 800000
      47 vxlan 128000
      48 nvgre 125000
      49 L3-isis 128000
      50 CFSoE-Rel 62500000
      ----------------------------------------------------------------------------------
      
      
      N6K-SPINE3#
      
      
Categories: Linux

PC Based Traffic generators


Unicast/Multicast

  1. iperf: CLI mode only. Works on all platforms.
  2. jperf : GUI equivalent of iperf.
  3. Ostinato –> Ixia like interface and hence can create any type of packet easily.

Multicast

  1. mcast.exe -> Windows only
  2. uftp -> Windows/MAC/Linux
  3. vlc –> Works on all platforms

 

VI/VIM tips

August 8, 2016 Leave a comment

Following are some random tips which I found to be useful. All of them has been collected from different sites and I am just putting them together for my reference.

Most of the examples mentioned here uses the below methodology

if there is a column prefix “:”  then that indicate its the vi command mode. To do this, open the file, press “ESC” key and type “:”

If you want these changes to be permanent, then you can put these commands in a special file called “.vimrc” which is present in your home directory(/users/<username> in Linux & Mac). Create this file, if it doesnt exist.

Indentation Tips

  1.  Enabling/Disabling auto indent
    :set ai ==> To enable
    :set noai ==> to disable
  2. Disabling auto indentation while pasting a code
    :set paste        ==>To disable autoindenting while pasting
    :set nopaste   ==>To enable it back
  3. How to indent a line which you are newly pasting to the existing code block’s indentation
    use ]p instead of just p. This aligns the pasted block with the surrounding text.
  4. Code block indentation tips
    These commands will fix the indents:

    • =i{ reindents “inner block” (inside the braces).
    • =a{ reindents “a block” (including the braces).
    • =2a{ reindents 2 blocks (this block and containing block).

    Instead of “{“, you can use “}” or “B”, for example, =aB indents a block.

    These commands will decrease or increase indents:

    • >i{ increase indent for inner block.
    • . repeat last change (increase indent of block again).
    • <i{ decrease indent for inner block.

    With the cursor on { or }:

    • =% indents the block (including matching brace).
    • >% or <% indents or unindents the block.

Commenting/uncommenting multiple lines in a file
Open the file in VI and in command mode, press “ctrl+v”, now it will go in to visual mode, now move the cursor using the movement keys (arrow keys up/down), move the up/down arrow till you reach the last line you want to comment. Now press “Shift+i” and now enter the character used for commenting “#”

In Nutshell
Commenting: CTRL+v ==> SHIFT+i ==> # ==> Will insert # on the selected column
Uncommenting: CTRL+v ==> x ==> Will delete the #’s in the selected column

Categories: Linux, MAC, Technical

Linux Tips and FAQ

March 29, 2013 Leave a comment

    1. How to find the Linux distribution using command line?
      $ cat /etc/*-release

      [root@linux-65 ~]# cat /etc/*-release
      Red Hat Enterprise Linux Server release 5.4 (Tikanga)
      [root@linux-65 ~]#
    2. How to find the linux Kernel version using command line?

      $ uname -a
      [root@linux-65 ~]# uname -a
      Linux linux-65.cisco.com 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
      [root@linux-65 ~]#
      Where 2.6.18-164.e15 is the kernel version
    3. How to find the Linux OS processor architecture using command line?
      $ uname -m[root@linux-65 ~]# uname -m
      i686
      [root@linux-65 ~]#Where i686 or in general ix86 (where x=3,4,5,6) which means this is a 32-bit proessor
      if the output is x86_64 then it indicates its a 64 bit processor.
    4. How to find out whether the processor is 32 bit or 64 bit?
      $ cat /proc/cpuinfo | grep flag

      lm flag means Long mode cpu - 64 bit CPU
      Real mode 16 bit CPU
      Protected Mode is 32-bit CPU

      [root@linux-65 ~]# cat /proc/cpuinfo | grep flag
      flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl cid cx16 xtpr
      flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl cid cx16 xtpr
      [root@linux-65 ~]#
    5. How to find out whether the processor supports VT
      For Intel processors
      grep vmx /proc/cpuinfo
      For AMD processors
      grep svm /proc/cpuinfo

Linux Output formatting tool tips

There are lot of powerful output formatting commands/tools available in linux as well as some routers/devices which are based on linux.

Few of them are

  1. grep
  2. egrep
  3. sed
  4. cut
  5. tr
  6. awk

Using tr and cut together to filter out a column in an output

Requirement:  Need to extract only the last column

Tools which can be used: cut

Problem: cut command has a limitation that the delimiter has to be a single character and in the example text the delimiter for columns are “multiple spaces/tabs”.

Solution: Use tr commands “-s”  option to remove duplicate delimiters and then pipe that output to cut command

Command in action

cat testfile.txt | tr -s ” ” | cut -d “” -f 4

where
Sample file content used in this example
linux > cat testfile.txt
0 1 0 0
1 1 1 0
2 1 2 0
3 1 3 0
4 1 4 0
5 1 5 0
6 1 6 0
7 1 7 0
8 1 8 0
9 1 9 0
10 1 10 0
linux >

cat testfile.txt | tr -s ” ”  will remove all the duplicate spaces in the output
linux > cat testfile.txt | tr -s " "
0 1 0 0
1 1 1 0
2 1 2 0
3 1 3 0
4 1 4 0
5 1 5 0
6 1 6 0
7 1 7 0
8 1 8 0
9 1 9 0
10 1 10 0
linux >

Now the above output we pipe it to cut command with space as the delimiter (-d ” “) and to get the 4th column alone use the -f 4
linux > cat testfile.txt | tr -s " " | cut -d " " -f 4
0
0
0
0
0
0
0
0
0
0
0
linux >

test

Categories: Linux, Technical

Linux Notes

March 29, 2013 Leave a comment

This post is to note some of the common issues which I have seen and their workarounds.

  1. Error while trying to compress a file using tar
    Symptom: 
    tar: regression-batch1.2013Feb09_20\:05\:59.tar.gz: Cannot open: Input/output error
    tar: Error is not recoverable: exiting now

    Command executed: 
    tar -zcvf regression-batch1.2013Feb09_20:05:59.tar.gz regression-batch1.2013Feb09_20:05:59

    Reason for the Error: The file name contains special character colon ":" which tar thinks that its a remote file.

    Solution: Add the "--force-local" switch along with the command which solves the problem.
    Another way to resolve is by giving complete path name in the command.
    eg: tar --force-local -zcvf regression-batch1.2013Feb09_20:05:59.tar.gz regression-batch1.2013Feb09_20:05:59

Categories: Linux, Technical