Exploring Expect
1. Chapter 1 Intro—What Is Expect?
1.Ouch, Those Programs Are Painful!
2.A Very Brief Overview
3.A First Script—dialback
4.Total Automation
5.Differing Behavior When Running Non-Interactively
6.Partial Automation
7.Dangerous, Unfriendly, Or Otherwise Unlikable User Interfaces
8.Graphical Applications
9.A Little More About Tcl
10.Job Control
11.Background Processes
12.Using Expect With Other Programs
13.Using Expect On UNIX
14.Using Expect On Other Operating Systems
15.Using Expect In Real Applications
16.Using Expect In Commercial Applications—Legalese
17.Obtaining Expect and the Examples
18.Expect And Tcl Resources
19.Exercises
2. Chapter 2 Tcl—Introduction And Overview
1.Everything Is A String
Notes1: ctrl-z
# stick control-Z in a variable
set controlZ \032
# define control-C
Notes2: multiple line.
set word \
really-long-string-which-does-not-quite-
2.Quoting Conventions
2.2.1. Return Values
Note1: bracketed command is replaced by its return value. #set p "My pid is [pid]."
Note2: The set command returns its second argument. #set b "[set a 0]"
3.Expressions
Note1: The expr command takes any number of arguments and evaluates them as a single
expression and returns the result
set x "The answer is 1 + 3"
set y "The answer is [1 + 3]" #wrong. 1 is not command
set z "The answer is [expr 1 + 3]"
4.Braces—Deferring Evaluation
5.Control Structures
2.5.1. The while Command
set total 1
while {$count > 0} {
set total [expr $total * $count]
set count [expr $count-1]
}
2.5.2. The incr Command
incr count -1
2.5.6 The if command:
1#if {$count < 0} {
puts "count is less than zero"
}
2#if {$count < 0} \ #====> continue line
{ puts "count is less than zero"
}
3#if {$count < 0}
{
puts "count is less than zero"
}
Note: 1,2 are correct. if missed one argu in 3.
2.5.7. The break And continue Commands
set a 0
while {1} {
incr a
if {$a == 3} break
puts "hello"
}
2.5.8. The proc And return Commands: proc接3个变量,名字,变量和body
proc fib {pen ult n} {
for {set i 0} {$i<$n} {incr i} {
set new [expr $ult+$pen]
set pen $ult ;# new penultimate value
set ult $new ;# new ultimate value
}
return $pen
}
set m [fib 0 1 9] #how to use proc.
2.5.8.B about global: define outside the proc. then you can access it from proc's body.
proc area_of_circle {radius} {
global pi
expr 2*$pi*$radius
}
2.5.9 the command source:
Procedures and variables that are used in numerous scripts can be stored in other files,
allowing them to be conveniently shared
#code for 2_5_9_CallSource.tcl
source 2_5_9_source.tcl
printabc
#code for 2_5_9_source.tcl
proc printabc {} {
puts "abc\n"
}
6.More On Expressions
7.Lists
2.7.1. Selecting Elements Of Lists: lindex need 2 argu, lrange need 3 argu.
#code A
tclsh> lindex "a b c d e" 0
a
tclsh> lindex "a b c d e" 2
c
tclsh> lrange "a b c d e" 0 2
a b c
tclsh> llength [lrange "a b c d e" 0 2]
3
#code B: command foreach need 3 argument.
foreach element $list {
puts $element
}
8.More Ways To Manipulate Strings
9.Arrays
10.Indirect References
11.Handling Errors
12.Evaluating Lists As Commands
13.Passing By Reference
14.Working With Files
15.File I/O
16.Executing UNIX Commands
17.Environment Variables
18.Handling Unknown Commands
19.Libraries
20.Is There More To Tcl?
21.Exercises
3.Chapter 3 Getting Started With Expect
1.The send Command
2.The expect Command
3.Anchoring
4.What Happens When Input Does Not Match
5.Pattern-Action Pairs
6.Example—Timed Reads In The Shell
7.The spawn Command
8.The interact Command
9.Example—Anonymous ftp
10.Exercises
4.Chapter 4 Glob Patterns And Other Basics
1.The * Wildcard
2.More Glob Patterns
3.Backslashes
4.Handling Timeout
5.Handling End Of File (eof)
6.Hints On The spawn Command
7.Back To Eof
8.The close Command
9.Programs That Ignore Eof
10.The wait Command
11.Exercises
5.Chapter 5 Regular Expressions
1.Regular Expressions—A Quick Start
2.Identifying Regular Expressions And Glob Patterns
3.Using Parentheses To Override Precedence
4.Using Parentheses For Feedback
5.More On The timed–read Script
6.Pattern Matching Strategy
7.Nested Parentheses
8.Always Count Parentheses Even Inside Of Alternatives
9.Example—The Return Value From A Remote Shell
10.Matching Customized Prompts
11.Example—A Smart Remote Login Script
12.What Else Gets Stored In expect_out
13.More On Anchoring
14.Exercises
6.Chapter 6 Patterns, Actions, And Limits
1.Matching Anything But
2.Really Complex Patterns
3.Really Simple Patterns
4.Matching One Line And Only One Line
5.Tcl’s string match Command
6.Tcl’s regexp Command
7.Tcl’s regsub Command
8.Ignoring Case
9.All Those Other String Functions Are Handy, Too
10.Actions That Affect Control Flow
11.Example—rogue
12.Character Graphics
13.More Actions That Affect Control Flow
14.Matching Multiple Times
15.Recognizing Prompts (Yet Again)
16.Speed Is On Your Side
17.Controlling The Limits Of Pattern Matching Input
18.The full_buffer Keyword
19.Double Buffering
20.Perpetual Buffering
21.The Politics Of Patterns
22.Expecting A Null Character
23.Parity
24.Length Limits
25.Comments In expect Commands
26.Restrictions On expect Arguments
27.eval—Good, Bad, And Ugly
28.Exercises
7.Chapter 7 Debugging Patterns And Controlling Output
1.Pattern Debugging
2.Enabling Internal Diagnostics
3.Logging Internal Diagnostics
4.Disabling Normal Program Output
5.The log_user Command
6.Example—su2
7.Recording All Expect Output
8.Sending Messages To The Log
9.About File Names
10.Log And Diagnostic State
11.Exercises
8.Chapter 8 Handling A Process And A User
1.The send_user Command
2.The send_error Command
3.The expect_user Command
4.Dealing With Programs That Reprompt
5.Dealing With Programs That Miss Input
6.Sleeping
7.Line Versus Character-Oriented And Other Terminal Modes
8.Echoing
9.Prompting For A Password On Behalf Of A Program
10.Security And Insecurity
11.Resetting The Terminal Upon Exit
12.More On The stty Command
13.The system Command
14.Redirecting The Standard Input Or Output
15.The expect_tty Command
16.The send_tty Command
17.Exercises
9.Chapter 9 The Expect Program
1.Expect—Just Another Program
2.Invoking Scripts Without Saying “expect”
3.Rewriting The #! Line
4.The .exp Extension
5.The—And Other Flags
6.The —c Flag
7.The -f Flag
8.Writing The #! Line
9.The −i Flag
10.The -n And -N Flags
11.The -d Flag
12.The -D Flag
13.The -b Flag
14.The - Flag
15.The interpreter Command
16.Exercises
10.Chapter 10 Handling Multiple Processes
1.The spawn_id Variable
2.Example—chess Versus chess
3.Example—Automating The write Command
4.How exp_continue Affects spawn_id
5.The Value Of spawn_id Affects Many Commands
6.Symbolic Spawn Ids
7.Job Control
8.Procedures Introduce New Scopes
9.How Expect Writes Variables In Different Scopes
10.Predefined Spawn Ids
11.Exercises
11.Chapter 11 Handling Multiple Processes Simultaneously
1.Implicit Versus Explicit Spawn Ids
2.Waiting From Multiple Processes Simultaneously
3.Example—Answerback
4.Which Pattern Goes With Which Spawn Id
5.Which Spawn Id Matched
6.Spawn Id Lists
7.Example—Connecting Together Two Users To An Application
8.Example—Timing All Commands
9.Matching Any Spawn Id Already Listed
10.The expect_before And expect_after Commands
11.Indirect Spawn Ids
12.Exercises
12.Chapter 12 Send
1.Implicit Versus Explicit Spawn Ids
2.Sending To Multiple Processes
3.Sending Without Echoing
4.Sending To Programs In Cooked Mode
5.Sending Slowly
6.Sending Humanly
7.Sending Nulls
8.Sending Breaks
9.Sending Strings That Look Like Flags
10.Sending Character Graphics
11.Comparing send To puts
12.Exercises
13.Chapter 13 Spawn
1.The Search Path
2.Philosophy--Processes Are Smart
3.Treating Files As Spawned Processes
4.Opening Ttys
5.Bugs And Workarounds
6.Process Pipelines And Ptys
7.Automating xterm
8.Checking For Errors From spawn
9.spawn -noecho
10.Example—unbuffer
11.Obtaining Console Output
12.Setting Pty Modes From spawn
13.Hung Ptys
14.Restrictions On Spawning Multiple Processes
15.Getting The Process Id From A Spawn Id
16.Using File I/O Commands On Spawned Processes
17.Exercises
14.Chapter 14 Signals
1.Signals
2.Signals In Spawned Processes
3.Notes On Specific Signals
4.When And Where Signals Are Evaluated
5.Overriding The Original Return Value
6.Using A Different Interpreter To Process Signals
7.Exit Handling
8.Exercises
15.Chapter 15 Interact
1.
The interact Command
2.
Simple Patterns
3.
Exact Matching
4.
Matching Patterns From The Spawned Process
5.
Regular Expressions
6.
What Happens To Things That Do Not Match
7.
More Detail On Matching
8.
Echoing
9.
Avoiding Echoing
10.
Giving Feedback Without -echo
11.
Telling The User About New Features
12.
Sending Characters While Pattern Matching
13.
The continue And break Actions
14.
The return Action
15.
The Default Action
16.
Detecting End-Of-File
17.
Matching A Null Character
18.
Timing Out
19.
More On Terminal Modes (Or The -reset Flag)
20.
Example—Preventing Bad Commands
21.
Exercises
16.
Chapter 16 Interacting With Multiple Processes
1.
Connecting To A Process Other Than The Currently Spawned Process
2.
Connecting To A Process Instead Of The User
3.
Example—rz And sz Over rlogin
4.
Redirecting Input And Output
5.
Default Input And Output
6.
Controlling Multiple Processes—kibitz
7.
Combining Spawn Ids In A Single -input Or -output
8.
Which Spawn Id Matched
9.
Indirect Spawn Ids
10.
An Extended Example—xkibitz
11.
Exercises
17.
Chapter 17 Background Processing
1.
Putting Expect In The Background
2.
Running Expect Without A Controlling Terminal
3.
Disconnecting The Controlling Terminal
4.
The fork Command
5.
The disconnect Command
6.
Reconnecting
7.
Using kibitz From Other Expect Scripts
8.
Mailing From Expect
9.
A Manager For Disconnected Processes—dislocate
10.
Expect As A Daemon
11.
Example—Automating Gopher and Mosaic telnet Connections
12.
Exercises
18.
Chapter 18 Debugging Scripts
1.
Tracing
2.
Logging
3.
Command Tracing
4.
Variable Tracing
5.
Example—Logging By Tracing
6.
UNIX System Call Tracing
7.
Tk And tkinspect
8.
Traditional Debugging
9.
Debugger Command Overview And Philosophy
10.
Stepping Over Procedure Calls
11.
Stepping Into Procedure Calls
12.
Where Am I
13.
The Current Scope
14.
Moving Up And Down The Stack
15.
Returning From A Procedure
16.
Continuing Execution
17.
Defining Breakpoints
18.
Help
19.
Changing Program Behavior
20.
Changing Debugger Behavior
21.
Exercises
19.
Chapter 19 Expect + Tk = Expectk
1.
Tk—A Brief Technical Overview
2.
Expectk
3.
The send Command
4.
An Extended Example—tkpasswd
5.
The expect Command And The Tk Event Loop
6.
The expect_background Command
7.
Multiple Spawn Ids In expect_background
8.
Background Actions
9.
Example—A Dumb Terminal Emulator
10.
Example—A Smarter Terminal Emulator
11.
Using The Terminal Emulator For Testing And Automation
12.
Exercises
20.
Chapter 20 Extended Examples
1.
Encrypting A Directory
2.
File Transfer Over telnet
3.
You Have Unread News—tknewsbiff
4.
Exercises
21.
Chapter 21 Expect, C, And C++
1.
Overview
2.
Linking
3.
Include Files
4.
Ptys And Processes
5.
Allocating Your Own Pty
6.
Closing The Connection To The Spawned Process
7.
Expect Commands
8.
Regular Expression Patterns
9.
Exact Matching
10.
Matching A Null
11.
What Characters Matched
12.
When The Number Of Patterns Is Not Known In Advance
13.
Expecting From Streams
14.
Running In The Background
15.
Handling Multiple Inputs And More On Timeouts
16.
Output And Debugging Miscellany
17.
Pty Trapping
18.
Exercises
22.
Chapter 22 Expect As Just Another Tcl Extension
1.
Adding Expect To Another Tcl-based Program
2.
Differences Between Expect And The Expect Extension In Another Program
3.
Adding Extensions To Expect
4.
Adding Extensions To Expectk
5.
Creating Script-less Expect Programs
6.
Functions And Variables In The Expect Extension
7.
Exercises
23.
Chapter 23 Miscellaneous
1.
Random Numbers
2.
Example—Generating Random Passwords
3.
The Expect Library
4.
Expect Versions
5.
Timestamps
6.
The time Command
7.
Exercises
1.
Appendix Appendix—Commands and Variables
1.
Commands And Flags
2.
Variables
Wednesday, March 9, 2011
Friday, March 4, 2011
DNS解析过程及原理
第一步:客户机提出域名解析请求,并将该请求发送给本地的域名服务器。
第二步:当本地的域名服务器收到请求后,就先查询本地的缓存,如果有该纪录项,则本地的域名服务器就直接把查询的结果返回。
第三步:如果本地的缓存中没有该纪录,则本地域名服务器就直接把请求发给根域名服务器,然后根域名服务器再返回给本地域名服务器一个所查询域(根的子域)的主域名服务器的地址。
第四步:本地服务器再向上一步返回的域名服务器发送请求,然后接受请求的服务器查询自己的缓存,如果没有该纪录,则返回相关的下级的域名服务器的地址。
第五步:重复第四步,直到找到正确的纪录。
第六步:本地域名服务器把返回的结果保存到缓存,以备下一次使用,同时还将结果返回给客户机。
让我们举一个例子来详细说明解析域名的过程.假设我们的客户机如果想要访问站点:www.linejet.com , 此客户本地的域名服务器是dns.company.com , 一个根域名服务器是NS.INTER.NET , 所要访问的网站的域名服务器是 dns.linejet.com,域名解析的过程如下所示:
(1)客户机发出请求解析域名www.linejet.com的报文
(2)本地的域名服务器收到请求后, 查询本地缓存, 假设没有该纪录, 则本地域名服务器dns.company.com则向根域名服务器 NS.INTER.NET发出请求解析域名www.linejet.com
(3)根域名服务器NS.INTER.NET收到请求后查询本地记录得到如下结果:linejet.com NS dns.linejet.com (表示linejet.com域中的域名服务器为:dns.linejet.com ), 同时给出dns.linejet.com的地址,并将结果返回给域名服务器dns.company.com。
(4)域名服务器dns.company.com 收到回应后,再发出请求解析域名www.linejet.com的报文。
(5)域名服务器 dns.linejet.com收到请求后,开始查询本地的记录,找到如下一条记录:www.linejet.com A 211.120.3.12 (表示linejet.com域中域名服务器dns.linejet.com的IP 地址为:211.120.3.12),并将结果返回给客户本地域名服务器dns.company.com。
(6)客户本地域名服务器将返回的结果保存到本地缓存,同时将结果返回给客户机。
这样就完成了一次域名解析过程
===============================
Article Two.
最近在研究bind,科普一下dns查询流程 :)
1.用户向本地DNS发出解析请求
2.本地DNS向根服务器(根服务器地址在本地有个静态列表)请求谁是该域名的顶机DNS(并把结果缓存在本地)
3.根DNS告诉本地DNS谁是该域的顶级DNS
4.本地DNS向顶级DNS请求谁是权威DNS(并缓存在本地)
5.应答谁是该域权威DNS
6.本地DNS向权威DNS请求域名解析(并把结果缓存在本地)
7.权威服务器应答6的请求
8.本地DNS把结果告诉用户
over~
Wednesday, March 2, 2011
IPv6 Plug and Play
A. Address
link local: fe80::/64. similar to 169.254.x.x
global: routable address.
B.Algo
区分 RS, RA , NS and NA
a, Host boot up.
-case 1: host A received regular Router advertisement (::1) which include the prefix. prefix + its own mac ==> new global IPv6 address.
-case 2: host send out Router solicitation(::2). router reply RA.
b, after got global address, it is ready to talk to destination.
-case 1: dst in local. Host send out neighbor solicitation and the dst response neighbor advertisement. Works like ARP.
-case 2: dst at remote. Host send out NS for gateway.