📄 scripts.ps
字号:
0 F1.67 (This script begins by disabling echo so that the password can be typed unseen. Unlike) 72 280.5 P5 11 Q1.7 (exec) 516.8 280.5 P0 12 Q-0.18 (which manipulates its standard I/O so that it is accessible to) 72 266.5 P2 F-0.18 (expect) 359.53 266.5 P0 F-0.18 (, the) 392.17 266.5 P5 11 Q-0.18 (system) 415.46 266.5 P0 12 Q-0.18 ( command does no) 450.26 266.5 P(manipulation, thereby allowing) 72 252.5 T2 F(stty) 225.24 252.5 T0 F( to af) 243.89 252.5 T(fect the terminal.) 268.32 252.5 T5 11 Q0.68 (fork) 72 228.5 P0 12 Q0.67 ( literally causes) 90.31 228.5 P2 F0.67 (expect) 169.93 228.5 P0 F0.67 ( to fork. Like the UNIX system call of the same name, it returns the) 202.56 228.5 P0.19 (child process ID to the parent. Since the parent has nothing else to do, it immediately exits. The) 72 214.5 P0.37 (shell will detect this as normal program termination. Meanwhile,) 72 200.5 P5 11 Q0.37 (disconnect) 392.8 200.5 P0 12 Q0.37 ( breaks the associa-) 445.31 200.5 P0.16 (tion between the child process and the terminal so that the rest of the script can continue immune) 72 186.5 P(to the user logging out.) 72 172.5 T1.2 (This paradigm provides a secure way of starting long-running background processes which re-) 72 148.5 P0.31 (quire passwords. This works well with security schemes such as MIT\325) 72 134.5 P0.31 (s Kerberos system. In or-) 414.54 134.5 P1.3 (der to run a process authenticated by Kerberos, all that is necessary is to spawn) 72 120.5 P2 F1.3 (kinit) 473.81 120.5 P0 F1.3 ( to get a) 497.81 120.5 P(ticket, and similarly) 72 106.5 T2 F(kdestr) 170.61 106.5 T(oy) 203.04 106.5 T0 F( when the ticket is no longer needed.) 215.03 106.5 T-0.19 (Before) 72 82.5 P2 F-0.19 (expect) 107.44 82.5 P0 F-0.19 (, there was no way to achieve such results. The choice was either in\337exibility or in-) 140.07 82.5 P(security) 72 68.5 T(.) 109.19 68.5 T2 F(expect) 118.19 68.5 T0 F( has made this choice unnecessary) 150.82 68.5 T(, and given us the best of both worlds.) 314.24 68.5 TFMENDPAGE%%EndPage: "17" 16%%Page: "16" 16612 792 0 FMBEGINPAGE72 36 504 47.95 R7 X0 KV0 12 Q0 X(16) 72 39.95 T0 10 Q(Don Libes) 108 39.95 T72 740.81 504 756 R7 XV72 63 540 720 RV4 12 Q0 X(break) 207 712 T(}) 162 698.5 T(}) 115.2 685 T0 F-0.18 (This script uses) 72 661.5 P2 F-0.18 (ed) 149.09 661.5 P0 F-0.18 ( although any editor could be used. First) 161.09 661.5 P2 F-0.18 (ed) 358.71 661.5 P0 F-0.18 ( is directed to search for the printer) 370.7 661.5 P-0.18 (.) 537 661.5 P-0.07 (Once the printer is found, returns are sent to get the successive lines until the value is either locat-) 72 647.5 P(ed or no more lines remain.) 72 633.5 T0.19 (Using a specialized tool such as) 72 609.5 P2 F0.19 (awk) 229.03 609.5 P0 F0.19 ( might seem like a better alternative, except if you aren\325) 250.35 609.5 P0.19 (t fa-) 520.16 609.5 P0.94 (miliar with) 72 595.5 P2 F0.94 (awk) 129.84 595.5 P0 F0.94 (\325) 151.17 595.5 P0.94 (s style of processing. While the same claim could be made about) 154.5 595.5 P2 F0.94 (expect) 483.1 595.5 P0 F0.94 (, this) 515.74 595.5 P1.56 (script illustrates the idea that \050ignoring syntax dif) 72 581.5 P1.56 (ferences\051 you can automate a procedure you) 318.86 581.5 P(know how to do interactively by simply translating it into) 72 567.5 T5 11 Q(send) 351.18 567.5 T0 12 Q(/) 375 567.5 T5 11 Q(expect) 378.33 567.5 T0 12 Q( sequences.) 410.7 567.5 T3 16 Q(12. Is expect too fast?) 108 526.83 T0 12 Q0.18 (The previous example demonstrated how) 72 501.5 P2 F0.18 (expect) 273.74 501.5 P0 F0.18 ( can use an editor to read a \336le.) 306.38 501.5 P2 F0.18 (expect) 463.7 501.5 P0 F0.18 ( has sim-) 496.33 501.5 P-0.24 (pler ways of reading \336les. For instance the command) 72 487.5 P4 F-0.57 (send [exec cat /etc/motd]) 329.75 487.5 P0 F-0.24 ( writes) 507.93 487.5 P0.37 (the contents of) 72 473.5 P2 F0.37 (/etc/motd) 146.73 473.5 P0 F0.37 ( to the current process. Calling a UNIX program to read a \336le may not) 194.69 473.5 P0.61 (seem like a fast method but it is a lot faster than having a user type it in. In a window environ-) 72 459.5 P0.07 (ment, cutting and pasting is an alternative, but this takes a lar) 72 445.5 P0.07 (ge amount of time also. Realistical-) 366.03 445.5 P(ly) 72 431.5 T(, blazing speed is hardly needed in a program that simulates users.) 80.55 431.5 T-0.24 (The speed of) 72 407.5 P2 F-0.24 (expect) 136.22 407.5 P0 F-0.24 ( operations is described by Libes [2]. One side-ef) 168.86 407.5 P-0.24 (fect not discussed is that) 405.63 407.5 P2 F-0.24 (ex-) 524.68 407.5 P-0.22 (pect) 72 393.5 P0 F-0.22 ( can overrun input buf) 93.31 393.5 P-0.22 (fers designed for human typists.) 198.8 393.5 P5 11 Q-0.22 (send) 357.03 393.5 P0 12 Q-0.22 ( supports a) 380.85 393.5 P1 F-0.22 (slow) 435.15 393.5 P0 F-0.22 ( option \050) 457.15 393.5 P4 F-0.53 (send -) 497.35 393.5 P-0.18 (s) 72 379.5 P0 F-0.08 (\051 speci\336cally to avoid this problem. It is controlled by parameters which describe the number of) 79.2 379.5 P(bytes to send atomically and a length of time to wait between each packet.) 72 365.5 T5 11 Q0.5 (send) 72 341.5 P0 12 Q0.49 ( also supports a simulation of actual human typing speed \050) 95.82 341.5 P4 F1.18 (send -h) 379.89 341.5 P0 F0.49 (\051 according to a modi-) 431.45 341.5 P0.8 (\336ed W) 72 327.5 P0.8 (eibull distribution [8], a common statistical tool to simulate interarrival times. The algo-) 104.15 327.5 P3.53 (rithm is driven by a random number generator and several user) 72 313.5 P3.53 (-chosen parameters. The) 408.85 313.5 P1.31 (parameters describe two average character interarrival times \050default and word endings\051, mini-) 72 299.5 P0.22 (mum and maximum interarrival times, and a variability \322shape\323. Errors are not simulated as this) 72 285.5 P0.67 (can be done by the user directly) 72 271.5 P0.67 (. Simplistic errors may be generated by embedding typing mis-) 227.8 271.5 P0.58 (takes and corrections \050if desired\051 in a) 72 257.5 P5 11 Q0.59 (send) 256.93 257.5 P0 12 Q0.58 ( ar) 280.74 257.5 P0.58 (gument. A more sophisticated approach could use) 293.43 257.5 P(an expert system as a coroutine.) 72 243.5 T3 16 Q(13. Security) 108 202.83 T0 12 Q0.21 (The) 72 177.5 P2 F0.21 (passwd) 93.86 177.5 P0 F0.21 ( script shown earlier was designed solely to be used for conformance testing. Many) 131.18 177.5 P1.82 (system administrators want such a script to embed in a comprehensive) 72 163.5 P1 F1.82 (adduser) 432.77 163.5 P0 F1.82 ( script, which) 471.41 163.5 P-0.29 (would set up everything that a generic new user needs including an account and password. Unfor-) 72 149.5 P0.34 (tunately) 72 135.5 P0.34 (, calling the) 109.86 135.5 P2 F0.34 (passwd) 170.18 135.5 P0 F0.34 ( script from another script reopens the very problem that the) 207.5 135.5 P2 F0.34 (passwd) 502.68 135.5 P0 F-0.01 (program was designed to solve. Passwords should not be used as ar) 72 121.5 P-0.01 (guments to programs because) 397.8 121.5 P(they can be seen by) 72 107.5 T2 F(ps) 168.92 107.5 T0 F( and similar programs.) 180.25 107.5 TFMENDPAGE%%EndPage: "16" 15%%Page: "15" 15612 792 0 FMBEGINPAGE72 740.81 540 756 R7 X0 KV72 36 540 47.95 RV0 10 Q0 X(expect: Scripts for Controlling Interactive Processes) 295.54 39.95 T0 12 Q(15) 528.01 39.95 T72 63 540 720 R7 XV0 X0.05 (This script has two) 72 712 P5 11 Q0.05 (interact) 166.14 712 P0 12 Q0.05 (s. The user switches between them by typing) 202.16 712 P4 F0.12 (~d) 423.77 712 P0 F0.05 ( \050for Dvorak\051 and) 438.17 712 P4 F0.12 (~q) 525.61 712 P0 F0.63 (\050for qwerty\051. The Dvorak translation occurs in the procedure) 72 698 P5 11 Q0.64 (dvorak) 375.05 698 P0 12 Q0.63 ( de\336ned with) 408.02 698 P5 11 Q0.64 (proc) 475.54 698 P0 12 Q0.63 (. W) 496.91 698 P0.63 (ithin) 518 698 P5 11 Q0.07 (dvorak) 72 684 P0 12 Q0.07 (, an) 104.97 684 P5 11 Q0.07 (interact) 125.43 684 P0 12 Q0.07 ( gives each character an action that corresponds to sending its Dvorak counter-) 161.45 684 P0.42 (part instead. Nothing has to be sent to the user) 72 670 P0.42 (, since the character will be echoed \050if necessary\051) 300.55 670 P(by the current process.) 72 656 T0.43 (The) 72 632 P4 F1.04 (return continue) 94.08 632 P0 F0.43 ( action for) 203.06 632 P4 F1.04 (~q) 256.65 632 P0 F0.43 ( causes the Dvorak) 271.04 632 P5 11 Q0.44 (interact) 366.69 632 P0 12 Q0.43 ( to return the value) 402.71 632 P5 11 Q0.44 (continue) 498.47 632 P0 12 Q0.55 (to its caller) 72 618 P0.55 (.) 125.74 618 P5 11 Q0.56 (interact) 135.83 618 P0 12 Q0.55 (\325) 171.86 618 P0.55 (s caller happens to be an earlier) 175.19 618 P5 11 Q0.56 (interact) 333.24 618 P0 12 Q0.55 ( \050at the bottom of the script\051 which) 369.26 618 P1.43 (evaluates the) 72 604 P5 11 Q1.46 (continue) 140.14 604 P0 12 Q1.43 ( and literally continues. This isn\325) 181.67 604 P1.43 (t anything magical. They are just T) 350.94 604 P1.43 (cl) 531.34 604 P(commands that are appropriately handled.) 72 590 T-0.01 (The script chooses the desired shell by examining the) 72 566 P5 11 Q-0.01 (SHELL) 331.38 566 P0 12 Q-0.01 ( environment variable. Since) 366.18 566 P2 F-0.01 (print-) 510.02 566 P-0.13 (env) 72 552 P0 F-0.13 ( appends a newline to the end of its output, this has to be stripped of) 89.99 552 P-0.13 (f and is done here by) 414.31 552 P5 11 Q-0.13 (scan) 516.8 552 P0 12 Q(\320 an equivalent to scanf in the C programming language.) 72 538 T0.37 (This script is excessive and is not at all what this feature of) 72 514 P5 11 Q0.37 (interact) 362.92 514 P0 12 Q0.37 ( was intended for) 398.94 514 P0.37 (. Neverthe-) 482.32 514 P(less it works and demonstrates a number of interesting aspects.) 72 500 T3 16 Q(1) 108 459.33 T(1. Non-interactive programs are controlled dif) 115.12 459.33 T(ferently) 422.66 459.33 T0 12 Q1.01 (Some interactive programs have non-interactive alternatives. However) 72 434 P1.01 (, it is often the case that) 420.33 434 P(these alternatives are controlled in a way quite unlike the original interactive program. Thus, you) 72 420 T(need to learn two ways of doing things: interactively and non-interactively) 72 406 T(.) 428.97 406 T-0.13 (For example, suppose you want to locate a printer server) 72 382 P-0.13 (. This is described by the) 341.99 382 P4 F-0.3 (rm) 467.03 382 P0 F-0.13 ( value in the) 481.42 382 P2 F0.12 (printcap) 72 368 P0 F0.12 ( \336le. Interactively) 115.98 368 P0.12 (, you might use an editor) 203.49 368 P0.12 (, or even,) 322.88 368 P2 F0.12 (mor) 370.86 368 P0.12 (e) 391.96 368 P0 F0.12 (, to search the \336le for the cor-) 397.28 368 P-0.12 (rect printer and then begin scanning for the) 72 354 P4 F-0.28 (rm) 280.89 354 P0 F-0.12 ( \336eld. T) 295.28 354 P-0.12 (o automate this, you must switch to a com-) 334.73 354 P(pletely dif) 72 340 T(ferent program, such as) 120.75 340 T2 F(awk) 236.32 340 T0 F(.) 257.65 340 T-0.29 (Alternatively) 72 316 P-0.29 (, you could just translate the interaction you were doing by hand into) 134.51 316 P5 11 Q-0.3 (send) 463.79 316 P0 12 Q-0.29 (/) 487.61 316 P5 11 Q-0.3 (expect) 490.95 316 P0 12 Q-0.29 ( se-) 523.31 316 P0.01 (quences. The following fragment does exactly this. It was used by a lar) 72 302 P0.01 (ger script that manipulat-) 419.04 302 P(ed printers by running) 72 288 T2 F(lpc) 181.26 288 T0 F(, the interactive interface to the BSD line printer system.) 196.59 288 T4 F(spawn ed /etc/printcap) 115.2 268 T(expect {*\134n}) 115.2 254.5 T( ;# discard character count) 207 254.5 T(send "/$printer/\134r") 115.2 241 T(for {} {1} {} {) 115.2 227.5 T(expect "*\134r\134n*:rm=*\134n*" {) 162 214 T(# found rm, now get value) 207 200.5 T(set i [string first :rm= $expect_match]) 207 187 T(scan [range $expect_match [expr $i+4] end c] \134) 207 173.5 T("%\134[^:\134]" server) 252 160 T(break) 207 146.5 T(} "*\134r\134n*\134\134\134r\134n" {) 162 133 T(# look at next line of entry) 207 119.5 T(send "\134r") 207 106 T(} "*\134r\134n*\134n" {) 162 92.5 T(# no more lines in entry - give up) 207 79 TFMENDPAGE%%EndPage: "15" 14%%Page: "14" 14612 792 0 FMBEGINPAGE72 36 504 47.95 R7 X0 KV0 12 Q0 X(14) 72 39.95 T0 10 Q(Don Libes) 108 39.95 T72 740.81 504 756 R7 XV72 63 540 720 RV0 12 Q0 X0.69 (to the) 72 712 P2 F0.69 (expect) 103.37 712 P0 F0.69 ( interpreter and invoke a procedure to answer the remaining questions without fur-) 136 712 P(ther interaction from you. This can be made as complex as you like.) 72 698 T0.22 (The ar) 72 674 P0.22 (guments to) 102.97 674 P5 11 Q0.22 (interact) 159.38 674 P0 12 Q0.22 ( are actually string-action pairs. \050The default action is to invoke the in-) 195.4 674 P0.73 (terpreter interactively) 72 660 P0.73 (.\051 This generalized mechanism can support all the usual styles of escapes.) 175.52 660 P1.11 (such as) 72 646 P2 F1.11 (tip) 112.19 646 P0 F1.11 (\325) 126.19 646 P1.11 (s) 129.52 646 P5 11 Q1.13 (~) 138.3 646 P0 12 Q1.11 (-pre\336xed commands or) 144.72 646 P2 F1.11 (csh) 262.3 646 P0 F1.11 (\325) 278.96 646 P1.11 (s single-character job control keys. Actions may be) 282.29 646 P-0.12 (any) 72 632 P2 F-0.12 (expect) 92.2 632 P0 F-0.12 ( command. As an example, the following line maps the strings) 124.83 632 P4 F-0.28 (~y) 429.22 632 P0 F-0.12 (,) 442.83 632 P4 F-0.28 (~a) 448.71 632 P0 F-0.12 (, and the) 463.1 632 P5 11 Q-0.12 (^C) 506.72 632 P0 12 Q-0.12 ( and) 519.8 632 P5 11 Q(^Z) 72 618 T0 12 Q( characters.) 83.86 618 T4 F(interact \134) 115.2 598 T(~y {yes} \134) 162 584.5 T(~a {send "[exec date]"; send_user "hello world"} \134) 162 571 T(\134Cc {exit} \134) 162 557.5 T(\134Cz {exec kill -STOP 0}) 162 544 T0 F0.11 (When) 72 520.5 P4 F0.26 (~y) 103.75 520.5 P0 F
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -