Sunday, 12 March 2017

TCL IMP THINGS TO REMEMBER

1.Difference between  “” & {} .

myvar = "some string", then {a $myvar b} will result in the string literal "a $myvar b"... while "a $myvar b" will result in the string literal "a some string b" 
See the below code for explanation.If we have a variable myvar with value as abcd 
if we puts {} ,value of $abcd will not be substituted if we use " " value will be substituted . 


% set myvar "abcd"
abcd
% puts {1 $myvar 2}             
1 $myvar 2                            --------->No Substitution
% puts "1 $myvar 2"             
1 abcd 2                                ---------->Substitution
%


2. Upvar Command
Usage: Used when we have to change the value of a global variable from inside a procedure’s scope.

Upvar: Create link to variable in a different stack frame. Upvar simplifies the implementation of call-by-reference calling and also makes it easier to implement Tcl procedures that are new control constructs.

What is the use of upvar?
A) Map a variable from the calling scope into the local procedure scope.
B) Map a variable from the local scope into the calling scope.
C) Copy the value of a variable from the calling scope to the local scope

The upvar command links a local variable with another variable (usually global).Any change made to local variable will also change the global variable. The upvar command allows you to easily pass arrays and arguments into procedures.
Syntax : upvar level $target_variable link_variable

Example:1
proc example {one two} {
upvar $one local1
upvar $two local2

set local1 Kavitha
set local2 Anbarasu

}

set glob1 David
set glob2 Beckam

puts $glob1
puts $glob2\n

example glob1 glob2
puts $glob1
puts $glob2

Output
David
Beckam

Kavitha
Anbarasu

In the above example we are able to change the value of two global variables glob1 and glob2 from within a procedure.


3.Uplevel
uplevel  is similar to the upvar command but used to evaluate commands in the scope of the calling procedure. Use with care!
uplevel incr x
Increments the variable x in the calling procedure.

proc a {} {
  set x a
  uplevel 3 {set x Hi}
  puts "x in a = $x"
}
proc b {} {
  set x b
  a
  puts "x in b = $x"
}
proc c {} {
  set x c
  b
  puts "x in c = $x"
}
set x main
c
puts "x in main == $x"

% c
x in a = a
x in b = b
x in c = c
% puts "x in main == $x"
x in main == Hi
%

See the above code: Most inside proc in in level 0 always.
a is in level 0,b in level 1 and c in level 2 and the main program is in level 3.
So in proc a if i change the value of level then i can change the value of variable x of any proc be it a,b,c or main proc from method "a" itself. Try changing level to 3,2,1,0 and see the output.

If in proc a we have uplevel 2 {set x Hi} below will be the output: i.e level 2 i.e c value will be changed and so on.
x in a = a
x in b = b
x in c = Hi
x in main == main
  
4.EVAL Command
The eval command will evaluate a list of strings as though they were commands typed at the % prompt or sourced from a file. The eval command normally returns the final value of the commands  being evaluated. If the commands being evaluated throw an error (for example, if there is a syntax error in one of the strings), then eval will will throw an error.

eval arg1 ??arg2?? ... ??argn??

Evaluates arg1 - argn as one or more Tcl commands. The args are concatenated into a string, and passed to tcl_Eval to evaluate and execute.
Eval returns the result (or error code) of that evaluation.

eval is the Tcl interpreter itself. It takes a Tcl script as a string, passes it to the interpreter for evaluation, and returns the result. In fact, eval takes any number of string arguments and concatenates them together into a single string with whitespace separating the arguments before evaluation.
eval is useful for evaluating Tcl scripts passed by name.

•       eval - Evaluate a Tcl script 
•       eval arg ?arg ...

        set a 10
        set b a
        eval puts $$b


Example:1
        set cmd {puts "Evaluating a puts"}
        puts $cmd                         ---->>puts "Evaluating a puts"
        puts "Evaluating a puts"   ---->>Evaluating a puts
        eval $cmd                         ----->>Evaluating a puts
       
Example 2:
          set str {set lst [list 100 200 300 400]}
          set a [eval $str]
          puts $a   :  100 200 300 400

Example 3:
      set a {puts "this is log file"}
      puts $a             ------>>puts "this is log file"
      eval $a             ------->>this is log file
      puts "this is log file"
     

Global variables:
The scope in which a variable will be evaluated can be changed with the global and upvar commands.
The global command will cause a variable in a local scope (inside a procedure) to refer to the global variable of that name.
•       global varname ?varname ...?
•       This command has no effect unless executed in the context of a proc body. If the global command is executed in the context of a proc body, it creates local variables linked to the corresponding global variables 
proc accum {string} { 
global accumulator 
append accumulator $string \n 

Different ways of global variable identification.

set var 3
proc glob {} {
  global var
  puts "the value of variable is $var"
  }
glob

or

Declaring global variable using double colon (::)
proc proc01 {} {
puts
“The value of var1 is: $::var1”
}

5.Difference of local and global variable.

set x 100
proc fun {} {
global x
puts "inside fun $x"
}

proc fun1 {} {
set x 500
puts "inside fun1 $x"
}

fun            --------->>inside fun 100
fun1          --------->>inside fun1 500


NOTEglobal only declares a variable, not creates it. More precisely, in a special table of variables available in the current scope (procedure in your case), global creates a special "link" entry which points to the similar table in the global scope. Then all accesses to that local entry are transparently redirected to the linked global entry. Hence, after global finished, all the stuff like existence/value is defined by the linked-to variable.


6.Spawning telnet using expect



Saturday, 11 March 2017

VRRP virtual MAC address

The VRRP virtual MAC address (or virtual router MAC address) is a shared MAC address adopted by the VRRP master. If the master fails the same virtual MAC master fails over to the new master. As a result, all packets for VRRP routers can continue to use the same virtual MAC address. 
Each VRRP router is associated with its own virtual MAC address. The last part of the virtual MAC depends on the VRRP virtual router ID using the following format:
00-00-5E-00-01-<VRID_hex>
Where <VRID_hex> is the VRRP virtual router ID in hexadecimal format in internet standard bit-order. For more information about the format of the virtual MAC see RFC 3768.
Some examples:
·        If the VRRP virtual router ID is 10 the virtual MAC w/ould be 00-00-5E-00-01-0a.
·        If the VRRP virtual router ID is 200 the virtual MAC would be 00-00-5E-00-01-c8.

Virtual Router MAC Address
The virtual router MAC address associated with a virtual router is an IEEE 802 MAC Address in the following format:
00-00-5E-00-01-{VRID} (in hex in internet standard bit-order) The first three octets are derived from the IANA’s OUI. The next two
octets (00-01) indicate the address block assigned to the VRRP protocol. {VRID} is the VRRP Virtual Router Identifier. This
mapping provides for up to 255 VRRP routers on a network.


Wednesday, 8 March 2017

Ethernet interview questions

1. What is Ethernet –
Ethernet is a medium which is used by devices like computers, switches, routers, hubs etc. If two devices wishes to send data among each other using ethernet technology, both the devices should support ethernet. For example, there are network interface cards which support ethernet, switches and routers which support ethernet network cards. Ethernet comes at the layer 2 of the OSI Model and Layer 1 of the TCP/IP Model.

2. What is an ethernet frame -
An ethernet frame is used for encapsulating data from the higher layers of the TCP/IP Model. Assume that a PC1 pings PC2. The ping , which is an ICMP packet along with the IP Header is encapsulated in the ethernet frame. The frame is used for carrying the ping packet and ultimately deliver it to PC2.

3. What are the components of an ethernet frame?
Ethernet frames are of different types. Typically they consist of the source mac-address, destination mac-address, data from the upper layer, type of data like IP, IPX etc, preamble (for clocking) and a FCS value for identifying the validity of the frame (Checks if the frame is invalid, corrupt, etc)

4. How is an ethernet frame constructed?
An ethernet frame is constructed by the network card. When data on the operating system , comes down to the network card, the frame would be constructed to send the data out on the network.

5. How is a frame received at the destination?
The preamble field is looked into first inside a frame. This is purely for synchronization. This is followed by the destination looking into whether the frame is intended for itself or not, by checking the destination mac-address. Once the mac-address is verified, the FCS value is calculated. If a mismatch is found, the frame is discarded. Else, the data type of the frame is looked into and the data is send to the appropriate layers of the operating system
(IP Layer).