Monday, 5 December 2016

TCL-Regular Expression Examples

Useful Link:
=========
http://docs.activestate.com/activetcl/8.5/expect4win/regex.html

1.Swap 2 Values in Tcl using regexp:

   Solution:
   =========
   set var "a b"
   regsub {(.) (.)} $var {\2 \1} var
   puts $var  : b a --->>> Values Swapped


2.Write a regsub command to replace all “.” in ip address to “_”

  Solution:
  =======
  set ip "192.168.55.55"
  set b [string map {. _} $ip]
  puts $b  ---->>>192_168_55_55

  ALITER
  set ip "192.168.55.55"
  regsub -all {\.} $ip {_} ip2
  puts $ip2


3.Write regular expression to from the key "1G_M-C_Gig-Copper" from the below output
  Port   1/1/1: Type  : 1G M-C (Gig-Copper)

  Solution:
  =======
  set out "Switch#sh media e 1/1/1
  Port   1/1/1: Type  : 1G M-C (Gig-Copper)
  Switch#"

  regexp -line {Type\s*:\s*(.*)} $out -- media  
  puts $media          --->>> 1G M-C (Gig-Copper)
  regsub -all {[\(\)]} $media "" media1
  puts $$media1        --->>>1G M-C Gig-Copper
  regsub -all {\s+} $media1 "_" media_key
  puts $media_key      --->>>1G_M-C_Gig-Copper


4.Matching any ip address

  Solution:
  =======
  set str 192.168.32.110
  regexp "(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})" $str all first second third fourth
  ALITER
  regexp {(^[0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$} $str all first second third fourth
  puts "$all \n $first \n $second \n $third \n $fourth \n"
  if {$first <= 255 && $second <= 255 && $third <= 255 && $fourth <= 255} {
   puts "valid ip address"
  } else {
   puts "invlaid ip address"
  }


5.To match any url:

  Solution:
  =======
  set url https://groups.google.com
  regexp -all {^[a-z]+\:\/\/[0-9.a-z_/]+} $url new
  puts $new

6.To match any Email Address:

  Solution:
  =======
  [a-zA-Z0-9_\.\+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+

  set email "maw_raj.abcd01@gmail.com"
  regexp {[a-zA-Z0-9_]+.[0-9a-zA-Z_]+@[a-z0-9A-Z_]+.[a-zA-Z_]+} $email match
  puts $match

  ALITER
  regexp -nocase {[A-Z_]+.[A-Z0-9_]+@[A-Z]+.[A-Z]+} $email match
  puts $match


7.To match date:

  Solution:
  ========
  Date – accept date input in the mm/dd/yyyy or mm-dd-yyyy formats.
  ((0[1-9])|(1[0-2]))[\/-]((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))[\/-](\d{4})

  set date "31/01/1000"
  regexp {(0[1-9]|1[0-9]|2[0-9]|3[0-1])/(0[1-9]|1[0-2])/(\d{4})} $date match
  puts $match


8.Complex Password – only accept a string that has 1 uppercase alphabet, 1 lowercase alphabet, 2 digits and 1 special character.  Also the minimum allowed length is 8 characters.


 
  Solution:
  =======
  (?=.*[A-Z])(?=.*[a-z])(?=.*[0-9].*[0-9])(?=.*[^a-zA-Z0-9]).{8,}


9.(hardware )?address matches either “hardware address” or “address”
 
  Solution:
  =======
  set add "hardware address"
  regexp -- {(hardware)?address} $add print
  puts $print   --->> This just print address


10.Regexp to find and print a duplicate word in a string
 
   Solution:
   =======
   set line "hello sir how r u. Good to see u sir"
   regexp -all {\S+} $line new
   puts $new

Within the RE, \S+ means a non-empty sequence of non-whitespace characters and \s+ means a non-empty sequence of whitespace.


11. Write a TCL program to get the inet addr of eth3 interface in the following output.

  Output:
  ======
[root@Redhat_5339 ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:21:5E:C6:14:B8

          inet addr:10.20.53.39  Bcast:10.20.63.255  Mask:255.255.240.0

          inet6 addr: fe80::221:5eff:fec6:14b8/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:396614696 errors:0 dropped:221362893 overruns:0 frame:0

          TX packets:128329705 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:120322245 (114.7 MiB)  TX bytes:173316774 (165.2 MiB)

          Interrupt:169 Memory:92000000-92012800



eth2      Link encap:Ethernet  HWaddr 00:05:1E:8E:82:BB

          inet addr:1.1.1.1  Bcast:1.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::205:1eff:fe8e:82bb/64 Scope:Link

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:45 errors:0 dropped:0 overruns:69283952 frame:0

          TX packets:96 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:6819 (6.6 KiB)  TX bytes:20773 (20.2 KiB)

          Memory:97a80000-97abffff



eth3      Link encap:Ethernet  HWaddr 00:05:1E:8E:82:BC

          inet addr:3.3.3.1  Bcast:3.3.3.255  Mask:255.255.255.0

          inet6 addr: fe80::205:1eff:fe8e:82bc/64 Scope:Link

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:988372099 errors:0 dropped:0 overruns:926779818 frame:0

          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:2640535661 (2.4 GiB)  TX bytes:4431 (4.3 KiB)

          Memory:97ac0000-97afffff


Solution:
=========
      #! /usr/bin/tclsh


set str "eth0      Link encap:Ethernet  HWaddr 00:21:5E:C6:14:B8

          inet addr:10.20.53.39  Bcast:10.20.63.255  Mask:255.255.240.0

          inet6 addr: fe80::221:5eff:fec6:14b8/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:396614696 errors:0 dropped:221362893 overruns:0 frame:0

          TX packets:128329705 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:120322245 (114.7 MiB)  TX bytes:173316774 (165.2 MiB)

          Interrupt:169 Memory:92000000-92012800



eth2      Link encap:Ethernet  HWaddr 00:05:1E:8E:82:BB

          inet addr:1.1.1.1  Bcast:1.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::205:1eff:fe8e:82bb/64 Scope:Link

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:45 errors:0 dropped:0 overruns:69283952 frame:0

          TX packets:96 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:6819 (6.6 KiB)  TX bytes:20773 (20.2 KiB)

          Memory:97a80000-97abffff



eth3      Link encap:Ethernet  HWaddr 00:05:1E:8E:82:BC

          inet addr:3.3.3.1  Bcast:3.3.3.255  Mask:255.255.255.0

          inet6 addr: fe80::205:1eff:fe8e:82bc/64 Scope:Link

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:988372099 errors:0 dropped:0 overruns:926779818 frame:0

          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:2640535661 (2.4 GiB)  TX bytes:4431 (4.3 KiB)

          Memory:97ac0000-97afffff"


regexp {eth3 [a-z A-Z].+} $str match1
regexp {inet addr:[.0-9]+} $match1 match2
puts $match2          ------->>inet addr:3.3.3.1


12. Write a TCL program  to check the given ip address falls in which class (A, B…) assuming the default subnet mask.

Solution:
======
 #! /usr/bin/tclsh

puts "enter ipv4 address in w.x.y.z format"
gets stdin ip

set result [regexp {[0-9]+.[0-9]+.[0-9]+.[0-9]+} $ip str]
if {$result == 0} {
puts "IP address is invalid... not in correct format"
exit
}

set result [regexp {([0-9]+).([0-9]+).([0-9]+).([0-9]+)} $ip match part1 part2 part3 part4]
if {$part1 > 0 && $part1 < 256 && $part2 > 0 && $part2 < 256 && $part3 > 0 && $part3 < 256 && $part4 > 0 && $part4 < 256} {
} else { puts "invalid ip... range should be 0-255"
exit
}

set n $part1
if {$n > 0 && $n < 128} {
puts "class A"
}  elseif {$n > 127 && $n < 192} {
puts "class B"
}  elseif {$n > 191 && $n < 224} {
puts "class C"
}  elseif {$n > 223 && $n < 240} {
puts "class D"
}  elseif {$n >239 && $n <256} {
puts "class E"
}  else {
puts "not a valid ip address"
}

13.Use of eval:

   Solution:
   =======
   set a 10
   set b a
   eval puts $$b  --->>10

14.To Extract "Hardware is ","Configured speed","input errors" "CRC" from the below output.

  set out "Switch#ICX7150-C12 Switch#sh int ethe 1/1/2
  GigabitEthernet1/1/2 is disabled, line protocol is down
  Port down for 2 day(s) 22 hour(s) 41 minute(s) 38 second(s)
  Hardware is GigabitEthernet, address is 00e0.5200.0101 (bia 00e0.5200.0101)
  Configured speed auto, actual unknown, configured duplex fdx, actual unknown
  Configured mdi mode AUTO, actual unknown
  Member of L2 VLAN ID 1, port is untagged, port state is DISABLED
  BPDU guard is Disabled, ROOT protect is Disabled, Designated protect is Disabled
  Link Error Dampening is Disabled
  STP configured to ON, priority is level0, mac-learning is enabled
  Flow Control is config enabled, oper enabled, negotiation disabled
  Mirror disabled, Monitor disabled
  Mac-notification is disabled
  Not member of any active trunks
  Not member of any configured trunks
  No port name
  IPG MII 0 bits-time, IPG GMII 0 bits-time
  MTU 1500 bytes
  300 second input rate: 0 bits/sec, 0 packets/sec, 0.00% utilization
  300 second output rate: 0 bits/sec, 0 packets/sec, 0.00% utilization
  0 packets input, 0 bytes, 0 no buffer
  Received 0 broadcasts, 0 multicasts, 0 unicasts
  0 input errors, 0 CRC, 0 frame, 0 ignored
  0 runts, 0 giants
  0 packets output, 0 bytes, 0 underruns
  Transmitted 0 broadcasts, 0 multicasts, 0 unicasts
  0 output errors, 0 collisions
  Relay Agent Information option: Disabled

Egress queues:
Queue counters    Queued packets        Dropped Packets
         0                   0                   0
         1                   0                   0
         2                   0                   0
         3                   0                   0
         4                   0                   0
         5                   0                   0
         6                   0                   0
         7                   0                   0

ICX7150-C12 Switch#"

     
Solution:
=======
regexp " Hardware is +(\[0-9A-Za-z\.\]+)," $out str hw_type
puts $hw_type

regexp "Configured speed +(\[0-9A-Za-z\.\]+)," $out str config_speed
puts $config_speed

regexp " (\[0-9/]+) input errors," $out str in_error
puts $in_error

regexp " (\[0-9/]+) CRC," $out str crc
puts $crc
       

15.To Get the Speed Output from the below output "None" in the below case.

Solution:
======
set out "Switch#show inte bri ethe 1/1/2

Port       Link    State   Dupl Speed Trunk Tag Pvid Pri MAC             Name
1/1/2      Disable None    None None  None  No  1    0   00e0.5200.0101
Switch#"

set speed ""
set line_out [split $out '\n']      
foreach out1 $line_out {
        if {$out1 != ""} {
         set str ""
         if { [ regexp "(\[0-9/]+)+/+(\[0-9/]+)+/+(\[0-9/])+\s*" $out1 str ] } {
             set sp [split $out1 "t+" ]
             regexp {[^ ]+\s+[^ ]+\s+[^ ]+\s+[^ ]+\s+([^ ]+)\s+} $sp -- speed
           }
        }
    }
puts $speed

1 comment:

Note: only a member of this blog may post a comment.