📄 writingscripts.html
字号:
CLASS="COMMAND">Testing Passwords</B></DT><DD><P>Write a script to check and validate passwords. The object is to flag <SPANCLASS="QUOTE">"weak"</SPAN> or easily guessed password candidates.</P><P>A trial password will be input to the script as a command line parameter. To be considered acceptable, a password must meet the following minimum qualifications: <UL><LI><P>Minimum length of 8 characters</P></LI><LI><P>Must contain at least one numeric character</P></LI><LI><P>Must contain at least one of the following non-alphabetic characters: <SPANCLASS="TOKEN">@</SPAN>, <SPANCLASS="TOKEN">#</SPAN>, <SPANCLASS="TOKEN">$</SPAN>, <SPANCLASS="TOKEN">%</SPAN>, <SPANCLASS="TOKEN">&</SPAN>, <SPANCLASS="TOKEN">*</SPAN>, <SPANCLASS="TOKEN">+</SPAN>, <SPANCLASS="TOKEN">-</SPAN>, <SPANCLASS="TOKEN">=</SPAN></P></LI></UL></P><P>Optional: <UL><LI><P>Do a dictionary check on every sequence of at least four consecutive alphabetic characters in the password under test. This will eliminate passwords containing embedded <SPANCLASS="QUOTE">"words"</SPAN> found in a standard dictionary.</P></LI><LI><P>Enable the script to check all the passwords on your system. These may or may not reside in <TTCLASS="FILENAME">/etc/passwd</TT>.</P></LI></UL></P><P>This exercise tests mastery of <AHREF="regexp.html#REGEXREF">Regular Expressions</A>.</P></DD><DT><BCLASS="COMMAND">Logging File Accesses</B></DT><DD><P>Log all accesses to the files in <TTCLASS="FILENAME">/etc</TT> during the course of a single day. This information should include the filename, user name, and access time. If any alterations to the files take place, that should be flagged. Write this data as neatly formatted records in a logfile.</P></DD><DT><BCLASS="COMMAND">Monitoring Processes</B></DT><DD><P>Write a script to continually monitor all running processes and to keep track of how many child processes each parent spawns. If a process spawns more than five children, then the script sends an e-mail to the system administrator (or root) with all relevant information, including the time, PID of the parent, PIDs of the children, etc. The script writes a report to a log file every ten minutes. </P></DD><DT><BCLASS="COMMAND">Strip Comments</B></DT><DD><P>Strip all comments from a shell script whose name is specified on the command line. Note that the <SPANCLASS="QUOTE">"#! line"</SPAN> must not be stripped out.</P></DD><DT><BCLASS="COMMAND">HTML Conversion</B></DT><DD><P>Convert a given text file to HTML. This non-interactive script automatically inserts all appropriate HTML tags into a file specified as an argument.</P></DD><DT><BCLASS="COMMAND">Strip HTML Tags</B></DT><DD><P>Strip all HTML tags from a specified HTML file, then reformat it into lines between 60 and 75 characters in length. Reset paragraph and block spacing, as appropriate, and convert HTML tables to their approximate text equivalent.</P></DD><DT><BCLASS="COMMAND">XML Conversion</B></DT><DD><P>Convert an XML file to both HTML and text format.</P></DD><DT><ANAME="CSPAMMERS"></A><BCLASS="COMMAND">Chasing Spammers</B></DT><DD><P> Write a script that analyzes a spam e-mail by doing DNS lookups on the IP addresses in the headers to identify the relay hosts as well as the originating ISP. The script will forward the unaltered spam message to the responsible ISPs. Of course, it will be necessary to filter out <ICLASS="EMPHASIS">your own ISP's IP address</I>, so you don't end up complaining about yourself.</P><P>As necessary, use the appropriate <AHREF="communications.html#COMMUNINFO1">network analysis commands</A>.</P><P>For some ideas, see <AHREF="communications.html#ISSPAMMER">Example 12-37</A> and <AHREF="contributed-scripts.html#ISSPAMMER2">Example A-27</A>.</P><P>Optional: Write a script that searches through a batch of e-mail messages and deletes the spam according to specified filters.</P></DD><DT><BCLASS="COMMAND">Creating man pages</B></DT><DD><P>Write a script that automates the process of creating <ICLASS="EMPHASIS">man pages</I>.</P><P>Given a text file which contains information to be formatted into a <ICLASS="EMPHASIS">man page</I>, the script will read the file, then invoke the appropriate <AHREF="textproc.html#GROFFREF">groff</A> commands to output the corresponding <ICLASS="EMPHASIS">man page</I> to <TTCLASS="FILENAME">stdout</TT>. The text file contains blocks of information under the standard <ICLASS="EMPHASIS">man page</I> headings, i.e., <SPANCLASS="QUOTE">"NAME,"</SPAN> <SPANCLASS="QUOTE">"SYNOPSIS,"</SPAN> <SPANCLASS="QUOTE">"DESCRIPTION,"</SPAN> etc.</P><P>See <AHREF="textproc.html#MANVIEW">Example 12-26</A>.</P></DD><DT><BCLASS="COMMAND">Morse Code</B></DT><DD><P>Convert a text file to Morse code. Each character of the text file will be represented as a corresponding Morse code group of dots and dashes (underscores), separated by whitespace from the next. For example, <SPANCLASS="QUOTE">"script"</SPAN> ===> <SPANCLASS="QUOTE">"... _._. ._. .. .__. _"</SPAN>.</P></DD><DT><BCLASS="COMMAND">Hex Dump</B></DT><DD><P>Do a hex(adecimal) dump on a binary file specified as an argument. The output should be in neat tabular fields, with the first field showing the address, each of the next 8 fields a 4-byte hex number, and the final field the ASCII equivalent of the previous 8 fields.</P></DD><DT><BCLASS="COMMAND">Emulating a Shift Register</B></DT><DD><P>Using <AHREF="arrays.html#STACKEX">Example 26-14</A> as an inspiration, write a script that emulates a 64-bit shift register as an <AHREF="arrays.html#ARRAYREF">array</A>. Implement functions to <ICLASS="EMPHASIS">load</I> the register, <ICLASS="EMPHASIS">shift left</I>, <ICLASS="EMPHASIS">shift right</I>, and <ICLASS="EMPHASIS">rotate</I> it. Finally, write a function that interprets the register contents as eight 8-bit ASCII characters.</P></DD><DT><BCLASS="COMMAND">Determinant</B></DT><DD><P>Solve a 4 x 4 determinant.</P></DD><DT><BCLASS="COMMAND">Hidden Words</B></DT><DD><P>Write a <SPANCLASS="QUOTE">"word-find"</SPAN> puzzle generator, a script that hides 10 input words in a 10 x 10 matrix of random letters. The words may be hidden across, down, or diagonally.</P><P>Optional: Write a script that <ICLASS="EMPHASIS">solves</I> word-find puzzles. To keep this from becoming too difficult, the solution script will find only horizontal and vertical words. (Hint: Treat each row and column as a string, and search for substrings.)</P></DD><DT><BCLASS="COMMAND">Anagramming</B></DT><DD><P> Anagram 4-letter input. For example, the anagrams of <ICLASS="EMPHASIS">word</I> are: <ICLASS="EMPHASIS">do or rod row word</I>. You may use <TTCLASS="FILENAME">/usr/share/dict/linux.words</TT> as the reference list.</P></DD><DT><SPANCLASS="QUOTE">"<BCLASS="COMMAND">Word Ladders</B>"</SPAN></DT><DD><P>A <SPANCLASS="QUOTE">"word ladder"</SPAN> is a sequence of words, with each successive word in the sequence differing from the previous one by a single letter.</P><P>For example, to <SPANCLASS="QUOTE">"ladder"</SPAN> from <ICLASS="EMPHASIS">mark</I> to <ICLASS="EMPHASIS">vase</I>:</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 mark --> park --> part --> past --> vast --> vase</PRE></TD></TR></TABLE> </P><P>Write a script that solves <SPANCLASS="QUOTE">"word ladder"</SPAN> puzzles. Given a starting and an ending word, the script will list all intermediate steps in the <SPANCLASS="QUOTE">"ladder"</SPAN>. Note that <ICLASS="EMPHASIS">all</I> words in the sequence must be <SPANCLASS="QUOTE">"legal."</SPAN></P></DD><DT><BCLASS="COMMAND">Fog Index</B></DT><DD><P>The <SPANCLASS="QUOTE">"fog index"</SPAN> of a passage of text estimates its reading difficulty, as a number corresponding roughly to a school grade level. For example, a passage with a fog index of 12 should be comprehensible to anyone with 12 years of schooling.</P><P>The Gunning version of the fog index uses the following algorithm.</P><OLTYPE="1"><LI><P>Choose a section of the text at least 100 words in length.</P></LI><LI><P>Count the number of sentences (a portion of a sentence truncated by the boundary of the text section counts as one).</P></LI><LI><P>Find the average number of words per sentence.</P><P>AVE_WDS_SEN = TOTAL_WORDS / SENTENCES</P></LI><LI><P>Count the number of <SPANCLASS="QUOTE">"difficult"</SPAN> words in the segment -- those containing at least 3 syllables. Divide this quantity by total words to get the proportion of difficult words.</P><P>PRO_DIFF_WORDS = LONG_WORDS / TOTAL_WORDS</P></LI><LI><P>The Gunning fog index is the sum of the above two quantities, multiplied by 0.4, then rounded to the nearest integer.</P><P>G_FOG_INDEX = int ( 0.4 * ( AVE_WDS_SEN + PRO_DIFF_WORDS ) )</P></LI></OL><P>Step 4 is by far the most difficult portion of the exercise. There exist various algorithms for estimating the syllable count of a word. A rule-of-thumb formula might consider the number of letters in a word and the vowel-consonant mix.</P><P>A strict interpretation of the Gunning fog index does not count compound words and proper nouns as <SPANCLASS="QUOTE">"difficult"</SPAN> words, but this would enormously complicate the script.</P></DD><DT><BCLASS="COMMAND">Calculating PI using Buffon's Needle</B></DT><DD><P>The Eighteenth Century French mathematician de Buffon came up with a novel experiment. Repeatedly drop a needle of length <SPANCLASS="QUOTE">"n"</SPAN> onto a wooden floor composed of long and narrow parallel boards. The cracks separating the equal-width floorboards are a fixed distance <SPANCLASS="QUOTE">"d"</SPAN> apart. Keep track of the total drops and the number of times the needle intersects a crack on the floor. The ratio of these two quantities turns out to be a fractional multiple of PI.</P><P>In the spirit of <AHREF="mathc.html#CANNON">Example 12-45</A>, write a script that runs a Monte Carlo simulation of Buffon's Needle. To simplify matters, set the needle length equal to the distance between the cracks, <ICLASS="EMPHASIS">n = d</I>.</P><P>Hint: there are actually two critical variables: the distance from the center of the needle to the crack nearest to it, and the angle of the needle to that crack. You may use <AHREF="mathc.html#BCREF">bc</A> to handle the calculations.</P></DD><DT><BCLASS="COMMAND">Playfair Cipher</B></DT><DD><P>Implement the Playfair (Wheatstone) Cipher in a script.</P><P>The Playfair Cipher encrypts text by substitution of <SPANCLASS="QUOTE">"digrams"</SPAN> (2-letter groupings). It is traditional to use a 5 x 5 letter scrambled-alphabet <ICLASS="EMPHASIS">key square</I> for the encryption and decryption.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 C O D E S 2 A B F G H 3 I K L M N 4 P Q R T U 5 V W X Y Z 6 7 Each letter of the alphabet appears once, except "I" also represents 8 "J". The arbitrarily chosen key word, "CODES" comes first, then all 9 the rest of the alphabet, in order from left to right, skipping letters 10 already used. 11 12 To encrypt, separate the plaintext message into digrams (2-letter 13 groups). If a group has two identical letters, delete the second, and 14 form a new group. If there is a single letter left over at the end, 15 insert a "null" character, typically an "X." 16 17 THIS IS A TOP SECRET MESSAGE 18 19 TH IS IS AT OP SE CR ET ME SA GE 20 21 For each digram, there are three possibilities. 22 ---------------------------------------------- 23 1) Both letters will be on the same row of the key square 24 For each letter, substitute the one immediately to the right, in that 25 row. If necessary, wrap around left to the beginning of the row. 26 27 or 28 29 2) Both letters will be in the same column of the key square 30 For each letter, substitute the one immediately below it, in that 31 row. If necessary, wrap around to the top of the column. 32 33 or 34 35 3) Both letters will form the corners of a rectangle within the key 36 square. For each letter, substitute the one on the other corner the 37 rectangle which lies on the same row. 38 39 40 The "TH" digram falls under case #3. 41 G H 42 M N 43 T U (Rectangle with "T" and "H" at corners) 44 45 T --> U 46 H --> G 47 48 49 The "SE" digram falls under case #1. 50 C O D E S (Row containing "S" and "E") 51 52 S --> C (wraps around left to beginning of row) 53 E --> S 54 55 ========================================================================= 56 57 To decrypt encrypted text, reverse the above procedure under cases #1 58 and #2 (move in opposite direction for substitution). Under case #3, 59 just take the remaining two corners of the rectangle. 60 61 62 Helen Fouche Gaines' classic work, ELEMENTARY CRYPTANALYSIS (1939), gives a 63 fairly detailed rundown on the Playfair Cipher and its solution methods.</PRE></TD></TR></TABLE> </P><P>This script will have three main sections</P><OLTYPE="I"><LI><P>Generating the <SPANCLASS="QUOTE">"key square"</SPAN>, based on a user-input keyword.</P></LI><LI><P>Encrypting a <SPANCLASS="QUOTE">"plaintext"</SPAN> message.</P></LI><LI><P>Decrypting encrypted text.</P></LI></OL><P>The script will make extensive use of <AHREF="arrays.html#ARRAYREF">arrays</A> and <AHREF="functions.html#FUNCTIONREF">functions</A>.</P></DD></DL></DIV><P>--</P><P>Please do not send the author your solutions to these exercises. There are better ways to impress him with your cleverness, such as submitting bugfixes and suggestions for improving this book.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="exercises.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="revisionhistory.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Exercises</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="exercises.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Revision History</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -