Вы находитесь на странице: 1из 25

TCL(56) 1. Tcl version?

2.In swap tcl without using 3rd variable? ANS: 1) Works with: Tcl version 8.5 proc swap {aName bName} { upvar 1 $aName a $bName b lassign [list $a $b] b a } 2) Works with: Tcl version 8.4 proc swap {aName bName} { upvar 1 $aName a $bName b foreach {b a} [list $a $b] break } 3) set a 1 set b 2 puts "before\ta=$a\tb=$b" swap a b puts "after\ta=$a\tb=$b 4) proc swap {aName bName} { upvar 1 $aName a upvar 1 $bName b lassign [list $a $b] b a } set a 10 set b 20 puts "$a--->$b" swap a b puts "$a--->$b" 3.Write a program to add 0 t0 10 numbers using tcl? ANS: WHILE LOOP #!/usr/bin/tclsh set a 0

while {$a <=10} { puts $a incr a } FOR LOOP for {set a 0} puts $a } {$a<=10} {incr a} {

4.Explain any program using while loop? ANS: set a 0 while {$a <10} { incr a puts $a } 5.Lists Commands? ANS: 1.list - Create a list 2.llength - Count the number of elements in a list 3. lindex - Retrieve an element from a list 4.lrange - Return one or more adjacent elements from a list 5. linsert - Insert elements into a list 6.lreplace - Replace elements in a list with new elements 7.lsort - Sort the elements of a list 8.lsearch - See if a list contains a particular element 9.lrepeat - Build a list by repeating elements 10.lreverse - Reverse the order of a list 11. lappend - Append list elements onto a variable 12. lset - Change an element in a list 13. lassign - Assign list elements to variables 14.mylset List to String: 1.join - Create a string by joining together list elements The join command is the complementary of split. It joins every element of a list into a string, using another string as separator between elements. The command structure is: join list ?joinString? the joinString argument can be omitted and defaults to null. 2.concat - Join lists together

6. Lists reverse? ANS: 1) #!/usr/bin/tclsh proc listreverse l { set reslist { } set len [llength $l] for {set i $len} {$i >= 0 } {incr i -1} lappend reslist [lindex $l $len] incr len -1 } puts $reslist } puts "[listreverse {r a o}]" 2) proc lreverse l { set result {} set i [llength $l] incr i -1 while {$i >= 0} { lappend result [lindex $l $i] incr i -1 } return $result } The usage is simple too: 3) lreverse {a b c} c b a 7. Lists in lists write a problem? ANS: #!/usr/bin/tclsh #how to get list in list elements set list1 {{{a b} {c d}} {{e f} {g h}}} set x [lindex $list1 1 1 0] {

puts $x set x [lindex {{{a b} {c d}} {{e f} {g h}}} {1 1 0}] puts $x 8.Accessing the last Element in list without using Length? ANS: #!/usr/bin/tclsh set mylist "foo bar biz! Tcl" set x [lindex $mylist end] puts $x set y [lindex $mylist end-1] puts $y 9. Write a program to replace a element from a list? 10.list1{2 3{5 6}{8 9 10 11}} I want to element 10?how to get that element? ANS: #!/usr/bin/tclsh set list1 {2 3 {5 6} {8 9 10 11}} set n [lindex $list1 3 2] puts $n 11. The list list1 contains repetitive elements set list1 aa bb cc aa cc dd aa ee ff cc Get the count of each repeated elements? 12. Write a program to sort elements of a list (with out using lsort) 13.String Reverse? ANS: 1.#!/usr/bin/tclsh proc stringReverse s { set res {} for {set i 0} {$i < [string length $s]} {incr i} { append res [string index $s end-$i] } return $res } 2.#!/usr/bin/tclsh set b "" gets stdin x set l [string length $x]

puts $x while {$l >= 0 } { set c [string index append b $c incr l -1 } puts $b 3.#/usr/bin/tclsh puts "enter a string" gets stdin tmp set len [string length $tmp] incr len -1 while {$len >= 0 } { set a [string index $tmp $len] append var $a incr len -1 } puts "reverse of $tmp is $var" $x $l]

4.#!/usr/bin/tclsh proc stringreverse s { set res "" set len [string length $s] set len1 [incr len -1] for {set i $len} {$i >= 0 } {incr i -1} {

append res [string index $s $len1] incr len1 -1 } puts $res }

puts "[stringreverse ewewrtewer]"

5.#!/usr/bin/tclsh % set string "hello world" hello world % join [lreverse [split $string {}]] {} dlrow olleh 14.String Commands? ANS: 1.The append command 2.string length 3.string 4.string 5.string 6.string 7.string 8.string 9.string range index equal compare match map is

String to List: 1.split - Split a string into a proper Tcl list

The split command converts a string into a list, where every element of the list is obtained splitting the string in parts. The position where to split the string, is specified in terms of a set of characters: the string is splitted in every position where one of the specified characters appears. The structure of the split command is: split string ? splitChars? 2. concat - Join lists together

concat ?arg arg ...? This command joins each of its arguments together with spaces after trimming leading and trailing white-space from each of them. If all of the arguments are lists, this has the same effect as concatenating them into a single list. It permits any number of arguments; if no args are supplied, the result is an empty string. 15. String equal, String Compare, String match and String Map?

ANS: String equal: String equal does it searching for an exact match, that's, the strings must match character by character to be considered the same for the command. The return value is 1 if the two strings passed as value are the same, otherwise 0 is returned: % string equal foo bar

0 % string equal tcl tcl 1 % string equal tcl TCL 0 String Compare: string compare will compare between the two strings. This subcommand is very similar to equal, but instead to return true or false if the strings are the same or not, the command will return: -1 if the first string is < than the second 0 if the first string is the same as the second 1 if the first string is > than the second This gives more information compared to string equal that may be useful for sorting or other tasks. string match: string match will match the element in the string. string match can be used in place of string equal, because instead to compare two strings, the command compares a string against a pattern. * Matches any sequence of characters. Even an empty string. ? Matches any single character. [chars] Matches the set of characters specified. It's possible to specify a sequence in the x-y form, like [a-z], that will match every character from a to z. \x Matches exactly x without to interpret it in a special way. This is used in order to match *, ?, [, ], \, as single characters. This is some example of pattern, and what it may match, in order to make it simpler to understand how it works: *xyz* can match xyz, fooxyz, fooxyzbar, and so on. x?z can match xaz, xxz, x8z, but can't match xz. [ab]c can match ac, bc. [a-z]*[0-9] can match alf4, biz5, but can't match 123, 2foo1 The command structure for string match is: string match ?-nocase? pattern string The return value is 1 or 0 respectively if string matches pattern or not. The -nocase option can be used to don't care about the case when matching. Example: % string match {[0-9]} 5 1 % string match foo* foo 1 % string match foo* foobar 1 % string match foo* barfoo 0 % string match ?*@*.* antirez@invece.org 1 % string match ?*@?*.?* antirez@invece.org 1 % string match ?*@?*.?* antirez@org 0 %

String Map String map is a powerful tool able to substitute occurrences of strings with other strings. The substitution is driven by a key-value pairs list. For example the list {foo bar x {} y yy} will replace every occurence of "foo" with "bar", will remove every occurrence of "x", and will duplicate every occurrence of "y". The command structure is the following: string match ?-nocase? pattern string Substitutions are done in an ordered way: starting from the first character of the original string, every key in the key-value pairs list is searched. If there is no match, the character is appended to the result that will be returned, and the process continues from the next character. If instead there is a match, the value relative to the matching key is appended to the result, and the process continues from the character just after the matching key. The above description may appear pedanting and complex, actually it's not hard at all to understand how string map works. It turns every occurence of a key in the key-value pair to the occurrence of the coresponding value. Once the programmer will get comfortable with string map, he will probably want know the details of the substitution process, so the above text will be more useful later when you will be a more experieced Tcl programmer. Examples: % string map {x {}} exchange echange % string map {1 Tcl 2 great} "1 is 2" Tcl is great Note how string map iterates just one time on the original string, so a pattern can't match as effect of an early substitution: % string map {{ } xx x yyy} "Hello World" HelloxxWorld When the key value paris list is not constant it's better to use the list command to create it: % set a foo foo % set b bar bar % string map [list $a $b $b $a] foobar barfoo % Similarly to many other string subcommands, map can take a -nocase option in order to turn the matching process case insensitive. 16. Write a proram to compare two strings? 17. What is the difference between string compare and string match? 18. WHAT IS THE DIFFERENCE BETWEEN {SET X 1} AND (SET X 1)? 19.WHAT IS THE DIFFERENCE BETWEEN THESE SET X = = 1 AND SET X 1, (SET X = 1)? 20. Procedures arguments and pass by value? Ans:

proc myproc x { set x "" } set list "1 2 3 4 5" myproc $list puts $list 21. Procedures with a variable number of arguments? ANS: proc + {x args} { foreach e $args { set x [expr $x+$e] } return $x } % + 10 20 30 % + 1 2 3 4 10 % + 50 50 % 22. Procedures with default arguments? ANS: proc lincr l { set result {} foreach e $l { lappend result [expr $e+1] } return $result } This is some output: % lincr {10 20 30} 11 21 31 % lincr {5 6} 6 7 proc lincr {l increment} { set result {} foreach e $l { lappend result [expr $e+$increment] } return $result } Again this works well, cut&paste the code into the tclsh and try yourself: % lincr {10 20 30} 1 11 21 31 % lincr {10 20 30} 5 15 25 35

proc lincr {l {increment 1}} { set result {} foreach e $l { lappend result [expr $e+$increment] } return $result } % lincr {1 2 3} 2 3 4 % lincr {1 2 3} 10 11 12 13 % % 5 % 5 % 5 % proc foo {a {b 10} {c 20} args} {puts "$a - $b - $c - $args"} foo 5 - 10 - 20 foo 5 1 2 - 1 - 2 foo 5 1 2 a b c d - 1 - 2 - a b c d

23. Procedure structure writes a program? ANS: NOT SURE THESE ANSWER proc isanagram {word1 word2} { set sorted1 [join [lsort [split $word1 {}]] {}] set sorted2 [join [lsort [split $word2 {}]] {}] string equal $sorted1 $sorted2 }

24. Procedures problem? In a program Number of procedure functions is there how to delete particular procedure function? ANS: Rename the procedure to have no name, for example: rename procedureName "" 25. How to pass current line variable to a function? 26. How to pass variable number of arguments to a procedure? 27.Write a library procedure in one file and call a function from another file? 28.Factorial function writes a program(iterative)? ANS: #! /usr/bin/tclsh puts " enter a number " set n [gets stdin var] proc fact {n} { if {$n == 1} {

10

return 1 } else { set x [expr $n -1] return [expr $n * [fact $x]] } } puts "factorial of $var is [fact $var]" 29.Recursive functions for factorial procedure program? 30.Passing arrays to a procedure write a program? ANS: Q.B4- How can I pass an array into a proc? Tcl Language Usage Questions And Answers (http://psg.com/~joem/tcl/faq.html#PassingArrays) Use upvar rather than try to use global variables when possible. If the function is event driven, you are forced to use global variables. # print elements of an array proc show_array arrayName { upvar $arrayName myArray foreach element [array names myArray] { puts stdout "${arrayName}($element) = } } set arval(0) zero set arval(1) one show_array arval To return an array from a procedures, just take the array name in as an argument, as above. Any changes you make in the array will be made in the parent's array as well. Extended Tcl introduces a concept called keyed lists which are arrays made out of lists of key-value pairs and can be passed by value to routines, over networks, etc. Q.B3- How can I call one proc with the multi parameter value returned by another proc? Assuming y requires multiple args and x returns multiple words, use Tcl's eval command: eval y [x] Q.B6- How can I delete a procedure from within a script? Rename the procedure to have no name, for example: rename procedureName "" Q.B11- How can I find the command line arguments to my application? The program name is assigned to the global variable argv0 and any arguments to it are placed in the global variable argv as a list. The $myArray($element)"

11

variable argc is set to the number of elements in the list argv. As an example: #! /usr/local/bin/tclsh if { $argc != 2 } { puts stderr "$argv0: Usage: $argv0 <infile> <outfile>" exit 1 } set infile [lindex $argv 0] set outfile [lindex $argv 1] 31. Write a programe to create a database of a student?[arrays] 32. write a programe the details of student by giving the name of a student? 33.Write a procedure to sum the floating-point array elements? 34. Write a program to search a particular element in an array 35. Fibonacci function? ANS: FIB(1) = FIB(2) = 1 FIB(N) = FIB(N-2)+FIB(N-1) (for N > 2) The Tcl implementation of the Fibonacci function is the following: proc fib n { if {$n < 3} { return 1 } else { expr {[fib [expr {$n-2}]]+[fib [expr {$n-1}]]} } } 36. How to pass variable to a file (command line arguments)? 37. Default value to the command line variable? 38. What are command line arguments? 39. What is the difference between global & upvar? ANS: A global command in a proc allows you to refer to a variable in the main code from within a proc. (No need to declare it global at the top level too) An upvar command allows you to reference a variable in your calling code from within your proc, and is commonly used for "pass by name" requirements where you're writing a proc that must alter an incoming variable. global allows us to access a variable globally anywhere in the programme. They are similar but subtly different. upvar allows you access variables up x levels in the call stack. They don't necessarily need to be global variables. You can use upvar to emulate global by passing upvar #0 varName localVarName You will get the global variable with a local name in that case. To emulate pass by reference, you are pass the name of the variable, then call upvar on that name.

12

If you know the name of the variable, you can use it as is. 40. Eval command? ANS: eval - Evaluate a Tcl script eval arg ?arg ...? Eval takes one or more arguments, which together comprise a Tcl script containing one or more commands. Eval concatenates all its arguments in the same fashion as the concat command, passes the concatenated string to the Tcl interpreter recursively, and returns the result of that evaluation (or any error generated by it). Note that the list command quotes sequences of words in such a way that they are not further expanded by the eval command. 41. Write a program to print the prime numbers from a list? 42. Write a program to print the number of characters, number of spaces and number of new lines in a string? 43. Write a procedure to convert a decimal value to binary? 44. Write a program to check a string is palindrome or not? 45. Write a program to print the number of occurrences of each character of a string? 46. Write a program to push and pop elements to a stack? 47. Write a program to search a given pattern in a file? 48. Write a program to sort dates (given in the format dd/mm/yr and ddmm-yr? 49. Write a program to print date in words if given in numeric format? 50. Write a program to sort numbers in ascending and descending with out using lsort ? 51. write a program to print specific values from a list when the input is specified? 52. Is the following a valid expression?? set a {$f} set $a 10 If yes how will u print it ? 53. What will be the output of the following script? set a [list 1 2 3 4 5 6 7 8] set ctr 0 for {set i 0} {[lindex $a $i] != ""} {incr i} {incr ctr} puts $ctr *********************** 54. proc sort {x} { upvar $x b set b [lsort $b] } set a [list 1 3 5 6 9 0 2] sort a puts $a 55 ****************** set a "1,2.62,3.2,86,2" puts [expr [join [split $a ","] "+"]]

13

********************** 56. Write a program using 3 levels of upvar , can global work when it is used in a procedure nested in another procedure?

REGULAR EXPRESSION QUESTIONS(22) 1. Regular Expression (., +, *, /, [ ], ^, \) using write programs? ANS: #!/usr/bin/tclsh set tmp "Hello World IP is 192.168.1.1" set ip #....{ "eth0 address is 192.168.1.5" }....{ .}...{. .}....{ \.}....{[ ]}...{[ .]}...{[ ].}.....

regexp {Hello} $tmp matchingstring puts "matching string for the pattern {Hello} is $matchingstring" regexp {Hello.} $tmp matchingstring puts "matching string for the pattern {Hello.} is $matchingstring" regexp {192.} $ip matchingstring

puts "matching string for the pattern {192.} is $matchingstring" #regexp {.is.} $ip matchingstring

#puts "matching string for the pattern {.is.} is $matchingstring" #puts $matchingstring regexp {192\.} $ip matchingstring

puts "matching string for the pattern {192\.} is $matchingstring" regexp {[Hello]} $tmp matchingstring puts "matching string for the pattern {\[Hello]} is $matchingstring" regexp {[192].} $ip matchingstring

puts "matching string for the pattern {\[192].} is $matchingstring" regexp {[192.]} $ip matchingstring

puts "matching string for the pattern {\[192.]} is $matchingstring"

14

#..{[A-Z]}..{[A-Z.]}..{[A-Z].}..{\[A-Z]}..{[a-z]}..{[a-z.]}..{[a-z].}... {\[a-z]}...(WORKING)... #set tmp "HELLO WORLD IP ADDRESS IS 192.168.1.1" #set tmp1 "hello world ip address is 192.168.1.2" #set tmp3 "Hello World ip address is 192.168.1.1" #regexp {[A-Z]} $tmp matchingstring #puts "matching string for the pattern {\[A-Z]} is $matchingstring" #regexp {[A-Z.]} $tmp matchingstring #puts "matching string for the pattern {\[A-Z.]} is $matchingstring" #regexp {[A-Z].} $tmp matchingstring #puts "matching string for the pattern {\[A-Z].} is $matchingstring" #regexp {\[A-Z]} $tmp matchingstring

#puts "matching string for the pattern {\[A-Z]} is $matchingstring" #regexp {[a-z]} $tmp1 matchingstring #puts "matching string for the pattern {\[a-z]} is $matchingstring" #regexp {[a-z.]} $tmp1 matchingstring

#puts "matching string for the pattern {\[a-z.]} is $matchingstring" #regexp {[a-z].} $tmp1 matchingstring #puts "matching string for the pattern {\[a-z].} is $matchingstring" #regexp {\[a-z]} $tmp matchingstring #puts "matching string for the pattern {\[a-z]} is $matchingstring" #.....{[0-9]}..{[0-9.]}..{[0-9].}...{\[0-9]}.......working.... #regexp {[0-9]} $tmp matchingstring

#puts "matching string for the pattern is $matchingstring" #regexp {[0-9.]} $tmp matchingstring

#puts "matching string for the pattern is $matchingstring" #regexp {[0-9].} $tmp matchingstring

#puts "matching string for the pattern is $matchingstring" #regexp {\[0-9]} $tmp matchingstring is $matchingstring"

#puts "matching string for the pattern

15

#..+..{[A-Z]+}..{[A-Z.]+}..{([A-Z].)+}..{([A-Z]/.)+}..{\[A-Z]+}..{([AZ./]+)}..{([A-Z]+)}..{([A-Z.]+)}..([A-Z]+)..{[a-z]+}..{[a-z.]+}..{([az].)+}..{\[a-z]+}..[a-z./]+...[a-z]+/..([a-z.]+)..([a-z]+)..(WORKING)... #regexp {[A-Z]+} $tmp4 matchingstring #puts "matching string for the pattern 1 is $matchingstring" #regexp {[A-Z.]+} $tmp4 matchingstring #puts "matching string for the pattern 2 is $matchingstring" #regexp {([A-Z].)+} $tmp4 matchingstring #puts "matching string for the pattern 3 is $matchingstring" #regexp {([A-Z]/.)+} $tmp4 matchingstring #puts "matching string for the pattern 4 is $matchingstring" #regexp {\[A-Z]+} $tmp4 matchingstring #puts "matching string for the pattern 5 is $matchingstring" #regexp regexp {([A-Z./]+)} $tmp4 matchingstring

#puts "matching string for the pattern 6 is $matchingstring" #regexp regexp {([A-Z]+)} $tmp4 matchingstring

#puts "matching string for the pattern 7 is $matchingstring" #regexp {([A-Z.]+)} $tmp4 matchingstring

#puts "matching string for the pattern 8 is $matchingstring" #regexp {([A-Z]+)} $tmp4 matchingstring

#puts "matching string for the pattern 9 is $matchingstring" #regexp {[a-z]+} $tmp1 matchingstring is $matchingstring"

#puts "matching string for the pattern 10 #regexp {[a-z.]+} $tmp1 matchingstring

#puts "matching string for the pattern 11 is $matchingstring" #regexp {([a-z].)+} $tmp1 matchingstring

#puts "matching string for the pattern 12 is $matchingstring" #regexp {\[a-z]+} $tmp1 matchingstring

#puts "matching string for the pattern 13 is $matchingstring"

16

#regexp

{[a-z./]+}

$tmp1 matchingstring

#puts "matching string for the pattern 14 is $matchingstring" #regexp {[a-z]+/.} $tmp1 matchingstring #puts "matching string for the pattern 15 is $matchingstring" #regexp {([a-z.]+)} $tmp1 matchingstring

#puts "matching string for the pattern 16 is $matchingstring" #regexp {([a-z]+)} $tmp1 matchingstring

#puts "matching string for the pattern 17 is $matchingstring" #regexp {[A-Z]+} $tmp matchingstring #puts "matching string for the pattern {\[A-Z]+} is $matchingstring" #regexp {[A-Z]+} $tmp matchingstring #puts "matching string for the pattern {\[A-Z]+} is $matchingstring" #regexp {[a-z]+} $tmp matchingstring #puts "matching string for the pattern {\[a-z]+} is $matchingstring" #..+..{[0-9]+}..{[0-9.]+}..{([0-9].)+}....{([0-9]/.)+}..{\[0-9]+}..{([09./]+)}............{([0-9]+)}..{([0-9.]+)}..([0-9.]+).......... #set tmp "Hello World ip address is 192.168.1.1" #regexp {[0-9]+} $tmp matchingstring

#puts "matching string for the pattern 0 is $matchingstring" #regexp {[0-9.]+} $tmp matchingstring #puts "matching string for the pattern 1 is $matchingstring" #regexp {([0-9].)+} $tmp matchingstring #puts "matching string for the pattern 2 is $matchingstring" #regexp {\[0-9]+} $tmp matchingstring

#puts "matching string for the pattern 3 is $matchingstring" #regexp {([0-9]/.)+} $tmp matchingstring

#puts "matching string for the pattern 4 is $matchingstring" #regexp {\[0-9]+} $tmp matchingstring

#puts "matching string for the pattern 5 is $matchingstring" #regexp {([0-9./]+)} $tmp matchingstring

#puts "matching string for the pattern 6 is $matchingstring"

17

#regexp {([0-9]+)} $tmp

matchingstring

#puts "matching string for the pattern 7 is $matchingstring" #regexp {([0-9.]+)} $tmp matchingstring #puts "matching string for the pattern 8 is $matchingstring" #regexp {([0-9.]+)} $tmp matchingstring

#puts "matching string for the pattern 9 is $matchingstring" #regexp {[a-zA-Z0-9 .,]+} $tmp matchingstring #puts "matching string for the pattern {\[a-zA-Z0-9 .,]+} is $matchingstring" #regexp {[0-9 .]+} $tmp matchingstring #puts "matching string for the pattern #regexp {[0-9.]+} $tmp matchingstring 11 is $matchingstring" 10 is $matchingstring"

#puts "matching string for the pattern

#...(.+)..{a+}..{ad+}...{adr+}...{.a+}...{.+a}..{Hello.+}... #set tmp "Hello World ip aaaaaaaaddddddrrrrreeeesssss is 192.168.1.1" #regexp {a+} $tmp matchingstring #puts "matching string for the pattern 00 is $matchingstring" #regexp {ad+} $tmp matchingstring #puts "matching string for the pattern 01 is $matchingstring" #regexp {adr+} $tmp matchingstring

#puts "matching string for the pattern 02 is $matchingstring" #regexp {.a+} $tmp matchingstring #puts "matching string for the pattern 03 is $matchingstring" #regexp {.+a} $tmp matchingstring

#puts "matching string for the pattern 04 is $matchingstring" #regexp {Hello.+} $tmp matchingstring

#puts "matching string for the pattern 05 is $matchingstring" #...(.*)..{a*}..{ad*}...{adr*}...{.a*}...{.*a}..{Hello.*}... set tmp "Hello World ip address is 192.168.1.1" set tmp1 "hello world ip address is 192.168.1.1" set tmp2 "aaaaaassssssdindicationfdfffffffghhhhh"

18

#regexp {a*} $tmp matchingstring #puts "matching string for the pattern 000 is $matchingstring" #regexp {a.*} $tmp matchingstring

#puts "matching string for the pattern 000 is $matchingstring" #regexp {*a} $tmp matchingstring

#puts "matching string for the pattern 000 is $matchingstring" #regexp {.*a} $tmp matchingstring

#puts "matching string for the pattern 000 is $matchingstring" #regexp {.a*} $tmp matchingstring

#puts "matching string for the pattern 000 is $matchingstring" #regexp {ad*} $tmp matchingstring #puts "matching string for the pattern 011 is $matchingstring" #regexp {adr*} $tmp matchingstring #puts "matching string for the pattern 022 is $matchingstring" #regexp {.a*} $tmp matchingstring

#puts "matching string for the pattern 033 is $matchingstring" #regexp {.*a} $tmp matchingstring

#puts "matching string for the pattern 044 is $matchingstring" #regexp {Hello.*} $tmp matchingstring

#puts "matching string for the pattern 055 is $matchingstring" #regexp {Hello.*} $tmp matchingstring

#puts "matching string for the pattern 055 is $matchingstring" #.......{^ }.....{[^]}..{^[]}..{[^.]+}... set tmp "Hello World ip address is 192.168.1.1" set tmp1 "hello world ip address is 192.168.1.1" set tmp2 "aaaaaassssssdindicationfdfffffffghhhhh"

#regexp {^H} $tmp matchingstring #puts "matching string for the pattern {^H} is $matchingstring" regexp {^h} $tmp1 matchingstring

19

puts "matching string for the pattern {^h} is $matchingstring" regexp {^[A-Z]} $tmp matchingstring puts "matching string for the pattern 001 is $matchingstring" regexp {^[a-z]} $tmp1 matchingstring puts "matching string for the pattern 002 is $matchingstring" regexp {^[A-Za-z]} $tmp matchingstring puts "matching string for the pattern 003 is $matchingstring" regexp {[^A-Z]} $tmp matchingstring puts "matching string for the pattern 004 is $matchingstring" regexp {[^A-Z]+} $tmp matchingstring puts "matching string for the pattern 005 is $matchingstring" regexp {[^0-9]+} $tmp matchingstring puts "matching string for the pattern 0010 is $matchingstring" regexp {[^A-Za-z]+} $tmp matchingstring puts "matching string for the pattern 006 is $matchingstring" regexp {[^A-Za-z.]+} $tmp matchingstring puts "matching string for the pattern 007 is $matchingstring" regexp {[^A-Za-z .]+} $tmp matchingstring puts "matching string for the pattern 008 is $matchingstring" regexp {[^A-Za-z0-9 .]+} $tmp matchingstring puts "matching string for the pattern 009 is $matchingstring" regexp {^[address]} $tmp matchingstring puts "matching string for the pattern 0011 is $matchingstring" #..........$.......{a$}..... set tmp "Hello World ip address is 192.168.1.1" set tmp2 "aaaaaassssssdindicationfdfffffffghhhhh"

regexp {a$} $tmp matchingstring puts "matching string for the pattern 1111 is $matchingstring" regexp {1.1$} $tmp matchingstring puts "matching string for the pattern 2222 is $matchingstring"

20

regexp {$} $tmp matchingstring puts "matching string for the pattern 3333 is $matchingstring" 2.Write a expect program to telnet to a router and get the ip address of the interfaces which are in up state get the version of the cisco ios check which are all the protocols are configured to get the ip address and status of a particular interface configure 1000 loopback interfaces configure OSPF and check adjacency is formed configure 1000 subinterfaces (fa0/0.1, fa0/0.2, fa0/0.3....) and configure different Vlan Ids for each interface Write a program to configure 100 Vlans in a switch? 3.Write a program to check the number of lines in file 4. Write a program to ssh to a remote device ANS: #script to ssh to a remote linux system and check the interface status #!/usr/bin/expect #the ip address of the remote system is provided as a command line argument set ip [lindex $argv 0] spawn ssh $ip expect "Password:" send "zframez123\r" expect "$" #sending the command to check the process running send "ps -ax\r" expect "$" set tmp $expect_out(buffer) puts "Different process running are $tmp" puts "\n\nexiting ......" send "exit\r" 5. Write a program to check IP address pattern is there in a given string ANS: set tmp " IP Address. . . . . . . . . . . . : 172.20.20.42" if {[regexp {[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+} $tmp match]} { puts "ip is $match" }

21

A Perfect regular expression to validate ip address with a single expression: if {[regexp {(^[2][5][0-5].|^[2][0-4][0-9].|^[1][0-9][0-9].|^[0-9][0-9].| ^[0-9].) ([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].) ([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].) ([2][0-5][0-5]|[2][0-4][0-9]|[1][0-9][0-9]|[0-9][0-9]|[0-9])$} $string match v1 v2 v3 v4]} {puts "$v1$v2$v3$v4"} else {puts "none"} 6. Write a program to check Mac address pattern is there in a given string ANS: set tmp " Physical Address. . . . . . . . . : 00-18-DE-06-C4-56" set var "Mac address of Eth0 is 00:11:22:22:AF:BB"

if {[regexp {[0-9A-F]+-[0-9A-F]+-[0-9A-F]+-[0-9A-F]+-[0-9A-F]+-[0-9A-F]+} $tmp match]} { puts "Mac address is $match" } if {[regexp {[0-9A-F]+:[0-9A-F]+:[0-9A-F]+:[0-9A-F]+:[0-9A-F]+:[0-9A-F]+} $var match2]} { puts "Mac address is $match2" } set mac 0000:4005:72c8:cccf regexp {[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+} $mac match puts "match is $match" 7. Write a program to check the class of a given ip address ANS: #script to check the class of a given IP address #!/usr/bin/tclsh #set ip1 10.0.0.1 #set ip2 172.16.1.1 #set ip3 192.168.1.1

22

puts "Enter the IP" gets stdin ip regexp {([0-9]+)\.[0-9]+\.[0-9]+\.[0-9]+} $ip match first if {$first < 128 } { puts "$ip is in class A" } elseif {$first >=128 && $first <= 191} { puts "$ip is in class B" } elseif {$first >=192 && $first <= 223} { puts "$ip is in class C" } 8. Write a program to increment a given ip address? ANS: set ip 192.168.1.1 regexp {([0-9]+.[0-9]+.[0-9]+.)([0-9]+)} $ip match first last puts "$match" puts "first = $first" puts " last = $last" set i 0 while {$i < 10 } { puts "$first$last" incr i incr last } 9. Write a procedure to increment MAC address? 10. What is the difference between regsub and regexp? ANS: regexp - Match a regular expression against a string regsub - Perform substitutions based on regular expression pattern matching 11. What is expect_out(buffer)? 12. What is spawn id? 13. write a program by using ipconfig, ifconfig in one file? 14. write a program to get the ip address , subnet mask,default gateway to store one file? 15. Write a expect program to ssh to a remote device and copy the list of different processes running in the remote system?

23

16. Show Version in regular expression? 17. Regular Expression using Date program (26/02/2010)? Verify the date? ANS: #How to get valid date month and year set dt 22/09/2009 set error [regexp {(^3[0-1]|^2[0-9]|^1[0-9]|^0[1-9])\/(1[0-2]|0[1-9])\/ (2[0-9][0-9][0-9])} $dt date a b c ] if { $error == 0} { puts "error-->$error" } else { puts " $date --> $a $b $c" } 18. I have number of lines in program for example (ifconfig or ipconfig) how to find an IP Address? ANS: #!/usr/bin/tclsh set tmp "Hello World ip address is 192.168.1.1" regexp {[0-9.]+} $tmp matchingstring

puts "matching string for the pattern 0 is $matchingstring" #!/usr/bin/tclsh set tmp "Hello World ip address is 192.168.1.1" regexp {[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+} $tmp matchingstring

puts "matching string for the pattern 0 is $matchingstring" 19. expect command what its do? 20. write a procedure to configure OSPF and check the adjacency? 21. Write a program using expect to telnet to a router and display the interface status and print the ip address of each interface configured? 22. Decode the IP packet. PFA (Ip_packet.txt) the sample IP packet. Decode each and every field of the header (ethernet and IP) and print the fields. solve this problem?

24

TESTING (14) 1. What is ur frame work? 2. Which traffic generator ur using? 3. WHAT IS UR TESTBED? 4. WHAT IS UR FRAME WORK? 5. HOW MANY TESTCASES U WRITTEN IN OSPF? 6. WHAT IS UR TESTING TOPOLOGY? 7. CAN U WRITE SIMPLE OSPF SCRIPT? 8. WHAT STREAMS U CAN GENERATE? 9. WHAT IS UR TRAFFIC GENERATOR? 10. Describe about the last project you have worked? 11. If you have done testing, a. What is the test setup you used? b. What are the tools you used for testing? c. What is the configuration you did in those tools? d. What was learnt from the captured packet on the end-machine to verify the test result? 12. If you want to capture packets with a sniffer, what modifications you make? 13. U FACED ANY CRITICAL BUGS? EXPLAIN FEW BUGS, HOW U CAN SOLVE?

25