networktestinggenius.tk
Your networking tutor
Exclusive networking testing notes
Learn Networking & Testing topics

1. Write a regexp to match an ip address?
set str 192.168.3.110
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 "invalid ip address"
}
2. How do you pick each part of the ip address and place them in separate variables?
set str 192.168.3.110
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"
3. What do these signify (. * ? +) in regexp?
. Any character.
x* Should match 0 or more occurrences of the preceding x.
x+ Should match 1 or more occurrences of the preceding x.
[a-z]? Should match 0 or 1 occurrence of the preceding x.
4. What is command substitution, variable substitution and backslash substitution?
Command substitution : Square brackets are used for command substitution.
Variable substitution : $ dollar sign is used for Variable substitution
Backslash substitution: With the backslash substitution, we escape the original meaning of the character.
Command substitution :
puts [expr 2+2]
Output:
4
The expr command is used to perform arithmetic calculations. First, the command between the square brackets is evaluated and the result is returned to the puts command. The puts command then evaluates the result and prints it to the console.
Variable substitution :
set name Thiru
puts name
puts $name
Output:
name
Thiru
The dollar-sign and the following characters are replaced in the word by the value of a variable. We create a variable called name and set a value to it.
puts name In this case, we print a string "name" to the console.
puts $name In the second case, the argument is preceded by the $ character. The value of the name variable is printed to the console.
Backslash substitution:
puts "This is \t thiru"
This is thiru
Here the \t sequence is replaced with a tab.
puts "My name is \"Thiru\""
puts "The \\ character is the backslash character"
Output:
My name is "Thiru"
The \ character is the backslash character
5. Difference between using curly braces and quotes in TCL?
In Tcl, both Curley braces and double quotes can be used to hold a block of program or data together as a single unit / parameter ... but there are differences ...
a) Curley braces can stretch over a number of lines, with new lines within the block being simply a part of the block. So they're ideal for defining blocks of code
b) Curley braces can be nested - since there are different open and close characters, blocks within blocks are written easily and naturally, which is quite impractical with double quotes!
c) the biggest difference is that double quoted blocks are evaluated at the time they are encountered by the language parser, but curley braces are deferred until they are (perhaps) evaluated later under the control of the command of which they form a part.
6. Convert a string into a list.
set list1 [split {This is Thiru} ""]
puts $list1
7. Convert a list into a string.
set list {a b c d e f}
for {set i 0} {$i<[llength $list]} {incr i} {
append string [lindex $list $i]
}
puts $string
8. Write a program to reverse a string.
proc string'reverse str {
set res {}
set i [string length $str]
while {$i > 0} {append res [string index $str [incr i -1]]}
set res
}
9. Find the length of a string without using “string length” command.
set l "Thiru"
set m 0
foreach i [split $l ""] {
incr m
}
puts "$m"
10. How to increment each element in a list ? eg: incrlist {1 2 3} =>2 3 4
set list1 {1 2 3}
set list2 {}
foreach i $list1 {
lappend list2 [expr {$i+1}] }
puts $list2
11. What is difference between lappend and concat
set list1 {1 2 3}
puts $list1
set list2 {a b c}
puts $list2
set new [lappend list1 $list2]
puts $new
set lengthlist [llength $new]
puts $lengthlist
#output
1 2 3
a b c
1 2 3 {a b c}
4
set list1 {1 2 3}
puts $list1
set list2 {a b c}
puts $list2
set b [concat $list1 $list2]
puts $b
set concatlength [llength $b]
puts $concatlength
#output
1 2 3
a b c
1 2 3 a b c
6
Note: length of output of lappend is 4 whereas for output of concat is 6
12. Write a script to increment the given ip by 1. if ip is 10.10.10.10 then output should be 10.10.10.11. if 10.10.10.255 than output should be 10.10.11.0
set ip "10.255.10.255"
set list [split $ip "."]
set ww [lindex $list 0]
set xx [lindex $list 1]
set yy [lindex $list 2]
set zz [lindex $list 3]
if {$zz>254} {
set zz 0
incr yy
if {$yy>255} {
set yy 0
incr xx
if {$xx>255} {
set xx 0
incr ww }
if {$ww>255} {
set ww 0
}}} else {
incr zz
}
puts "$ww.$xx.$yy.$zz"
12. Write a script for Sorting
set l { 1 2 4 3 5 7 9 8}
set len [llength $l]
set swapped true
while {$swapped} {
set swapped false
for {set i 0} {$i < $len - 1} {incr i} {
set j [expr {$i + 1}]
if {[lindex $l $i] > [lindex $l $j]} {
set t [lindex $l $i]
lset l $i [lindex $l $j]
lset l $j $t
}
}
incr len -1
}
puts $l
13. How to extract "information" from "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb" in tcl using a single command?
set a "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbcc"
set output [string trimright [string trimleft $a "abc"] "abc"]
puts $output
14. write a program to the given ip is valid private address or not?
set ip 169.254.255.255
if {[regexp {^(10|169|172|192).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$} $ip match oct1 oct2 oct3 oct4] } {
if {$oct1==10} {
puts "$ip IP is CLASS A private ip range"
}
if {$oct1==169} {
if {$oct2==254} {
puts "$ip IP is internal/system private ip range "
} else {
puts "$ip is public ip"
}
}
if {$oct1==192} {
if {$oct2==168} {
puts "$ip IP is CLASS C private ip range "
} else {
puts "$ip is public ip"
}
}
if {$oct1==172} {
if {$oct2>=16} {
#puts "$ip is private ip "
if {$oct2<=31} {
puts "$ip IP is CLASS B private ip range "
} else {
puts "$ip is public ip"
}
} }
} else {
puts "$ip is Invalid ip"
}
15. Is their any possibility to find the words which has letter "a" in the given list?
puts "searching variable a"
set i 0
set list [list ananth raja]
foreach item $list {
set elem [split $item ""]
foreach var $elem {
if {$var == "a"} {
incr i
}
}
}
puts $i
16. if i give in numbers output should be in characters
example : set a 23
o/p twentythree
set oned { one two three four five six seven eight nine ten }
set towd { ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen }
set multten {ten twenty thirty forty fifty sixty seventy eighty ninety}
set num 67
set len [string len $num]
if {$len == 1} {
set first [regexp -inline {[0-9]} $num]
set first [lindex $oned $first-1]
puts $first
}
if {$len == 2 && $num < 19} {
set first [regexp -inline {[0-9]$} $num]
set first [lindex $towd $first]
puts $first
}
if {$len == 2 && $num > 19} {
set first [regexp -inline {[0-9]} $num]
set first [lindex $multten $first-1]
set second [regexp -inline {[0-9]$} $num]
set second [lindex $oned $second-1]
puts "$first $second"
}
17. Write a program to increment IP address to +10 and verify it is correct ip or not ??
set ip "10.255.255.255"
set list [split $ip "."]
set oct1 [lindex $list 0]
set oct2 [lindex $list 1]
set oct3 [lindex $list 2]
set oct4 [lindex $list 3]
if {$oct4>254} {
set oct4 0
set oct3 [expr $oct3+10]
if {$oct3>255} {
set oct3 0
set oct3 [expr $oct3+10]
if {$oct2>255} {
set oct2 0
set oct3 [expr $oct1+10] }
if {$oct1>255} {
set oct1 0
}}} else {
set oct4 [expr $oct4+10]
}
puts "$oct1.$oct2.$oct3.$oct4"
18. What are the different ways to initialize a variable.
1. Initializing using set
set name Thiru
2. Initializing using output of an expression
set a 10
srt b 10
set c [expr $a + $b ]
3. Initializing using a list
set m [list Thiru Bhuvana Selvi Mashavan Raja Ananth]
4. Intializing using a array
set names(1) Thiru
set names(2) Bhuvana
set names(3) Selvi
set names(4) Madhavan
set names(5) Raja
set names(6) Ananth
19. Check wither given is file or directory.
if {file exists $filename} {
set dir [ file isdirectory $filename]
if {$dir == 1} { puts "$filename is directory"
} else {
puts "$filename is file"
}
20. How will you compare a list?
proc listcomp {a b} {
set diff {}
foreach i $a {
if {[lsearch -exact $b $i]==-1} {
lappend diff $i
}
}
puts $diff
}
set a {1 2 3 4 5 6}
set b {2 4 6 8}
listcomp $a $b