⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extmisc.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 4 页
字号:
	      options this powerful utility takes.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SELFCOPY"></A><P><B>Example 15-55. A script that copies itself</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# self-copy.sh   3&nbsp;   4&nbsp;# This script copies itself.   5&nbsp;   6&nbsp;file_subscript=copy   7&nbsp;   8&nbsp;dd if=$0 of=$0.$file_subscript 2&#62;/dev/null   9&nbsp;# Suppress messages from dd:   ^^^^^^^^^^^  10&nbsp;  11&nbsp;exit $?  12&nbsp;  13&nbsp;#  A program whose only output is its own source code  14&nbsp;#+ is called a "quine" per Willard Quine.  15&nbsp;#  Does this script qualify as a quine?</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EXERCISINGDD"></A><P><B>Example 15-56. Exercising <ICLASS="FIRSTTERM">dd</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# exercising-dd.sh   3&nbsp;   4&nbsp;# Script by Stephane Chazelas.   5&nbsp;# Somewhat modified by ABS Guide author.   6&nbsp;   7&nbsp;infile=$0       # This script.   8&nbsp;outfile=log.txt # This output file left behind.   9&nbsp;n=3  10&nbsp;p=5  11&nbsp;  12&nbsp;dd if=$infile of=$outfile bs=1 skip=$((n-1)) count=$((p-n+1)) 2&#62; /dev/null  13&nbsp;# Extracts characters n to p (3 to 5) from this script.  14&nbsp;  15&nbsp;# --------------------------------------------------------  16&nbsp;  17&nbsp;echo -n "hello world" | dd cbs=1 conv=unblock 2&#62; /dev/null  18&nbsp;# Echoes "hello world" vertically.  19&nbsp;# Why? Newline after each character dd emits.  20&nbsp;  21&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="DDKEYSTROKES"></A></P><P>To demonstrate just how versatile <BCLASS="COMMAND">dd</B> is,	     let's use it to capture keystrokes.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DDKEYPRESS"></A><P><B>Example 15-57. Capturing Keystrokes</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# dd-keypress.sh: Capture keystrokes without needing to press ENTER.   3&nbsp;   4&nbsp;   5&nbsp;keypresses=4                      # Number of keypresses to capture.   6&nbsp;   7&nbsp;   8&nbsp;old_tty_setting=$(stty -g)        # Save old terminal settings.   9&nbsp;  10&nbsp;echo "Press $keypresses keys."  11&nbsp;stty -icanon -echo                # Disable canonical mode.  12&nbsp;                                  # Disable local echo.  13&nbsp;keys=$(dd bs=1 count=$keypresses 2&#62; /dev/null)  14&nbsp;# 'dd' uses stdin, if "if" (input file) not specified.  15&nbsp;  16&nbsp;stty "$old_tty_setting"           # Restore old terminal settings.  17&nbsp;  18&nbsp;echo "You pressed the \"$keys\" keys."  19&nbsp;  20&nbsp;# Thanks, Stephane Chazelas, for showing the way.  21&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="DDRANDOM"></A></P><P>The <BCLASS="COMMAND">dd</B> command can do random access on a	      data stream.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo -n . | dd bs=1 seek=4 of=file conv=notrunc   2&nbsp;#  The "conv=notrunc" option means that the output file   3&nbsp;#+ will not be truncated.   4&nbsp;   5&nbsp;# Thanks, S.C.</PRE></TD></TR></TABLE>            </P><P><ANAME="DDCOPY"></A></P><P>The <BCLASS="COMMAND">dd</B> command can copy raw data	      and disk images to and from devices, such as floppies and	      tape drives (<AHREF="contributed-scripts.html#COPYCD">Example A-5</A>). A common use is	      creating boot floppies.</P><P>		<TTCLASS="USERINPUT"><B>dd if=kernel-image of=/dev/fd0H1440</B></TT>             </P><P>Similarly, <BCLASS="COMMAND">dd</B> can copy the entire	       contents of a floppy, even one formatted with a	       <SPANCLASS="QUOTE">"foreign"</SPAN> OS, to the hard drive as an	       image file.</P><P>		<TTCLASS="USERINPUT"><B>dd if=/dev/fd0 of=/home/bozo/projects/floppy.img</B></TT>             </P><P><ANAME="DDSWAP"></A></P><P>	      Other applications of <BCLASS="COMMAND">dd</B> include	      initializing temporary swap files (<AHREF="zeros.html#EX73">Example 28-2</A>)	      and ramdisks (<AHREF="zeros.html#RAMDISK">Example 28-3</A>). It can even do a	      low-level copy of an entire hard drive partition, although	      this is not necessarily recommended.</P><P>People (with presumably nothing better to do with	      their time) are constantly thinking of interesting	      applications of <BCLASS="COMMAND">dd</B>.</P><P><ANAME="DDFDEL"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="BLOTOUT"></A><P><B>Example 15-58. Securely deleting a file</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# blot-out.sh: Erase "all" traces of a file.   3&nbsp;   4&nbsp;#  This script overwrites a target file alternately   5&nbsp;#+ with random bytes, then zeros before finally deleting it.   6&nbsp;#  After that, even examining the raw disk sectors by conventional methods   7&nbsp;#+ will not reveal the original file data.   8&nbsp;   9&nbsp;PASSES=7         #  Number of file-shredding passes.  10&nbsp;                 #  Increasing this slows script execution,  11&nbsp;                 #+ especially on large target files.  12&nbsp;BLOCKSIZE=1      #  I/O with /dev/urandom requires unit block size,  13&nbsp;                 #+ otherwise you get weird results.  14&nbsp;E_BADARGS=70     #  Various error exit codes.  15&nbsp;E_NOT_FOUND=71  16&nbsp;E_CHANGED_MIND=72  17&nbsp;  18&nbsp;if [ -z "$1" ]   # No filename specified.  19&nbsp;then  20&nbsp;  echo "Usage: `basename $0` filename"  21&nbsp;  exit $E_BADARGS  22&nbsp;fi  23&nbsp;  24&nbsp;file=$1  25&nbsp;  26&nbsp;if [ ! -e "$file" ]  27&nbsp;then  28&nbsp;  echo "File \"$file\" not found."  29&nbsp;  exit $E_NOT_FOUND  30&nbsp;fi    31&nbsp;  32&nbsp;echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? "  33&nbsp;read answer  34&nbsp;case "$answer" in  35&nbsp;[nN]) echo "Changed your mind, huh?"  36&nbsp;      exit $E_CHANGED_MIND  37&nbsp;      ;;  38&nbsp;*)    echo "Blotting out file \"$file\".";;  39&nbsp;esac  40&nbsp;  41&nbsp;  42&nbsp;flength=$(ls -l "$file" | awk '{print $5}')  # Field 5 is file length.  43&nbsp;pass_count=1  44&nbsp;  45&nbsp;chmod u+w "$file"   # Allow overwriting/deleting the file.  46&nbsp;  47&nbsp;echo  48&nbsp;  49&nbsp;while [ "$pass_count" -le "$PASSES" ]  50&nbsp;do  51&nbsp;  echo "Pass #$pass_count"  52&nbsp;  sync         # Flush buffers.  53&nbsp;  dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength  54&nbsp;               # Fill with random bytes.  55&nbsp;  sync         # Flush buffers again.  56&nbsp;  dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength  57&nbsp;               # Fill with zeros.  58&nbsp;  sync         # Flush buffers yet again.  59&nbsp;  let "pass_count += 1"  60&nbsp;  echo  61&nbsp;done    62&nbsp;  63&nbsp;  64&nbsp;rm -f $file    # Finally, delete scrambled and shredded file.  65&nbsp;sync           # Flush buffers a final time.  66&nbsp;  67&nbsp;echo "File \"$file\" blotted out and deleted."; echo  68&nbsp;  69&nbsp;  70&nbsp;exit 0  71&nbsp;  72&nbsp;#  This is a fairly secure, if inefficient and slow method  73&nbsp;#+ of thoroughly "shredding" a file.  74&nbsp;#  The "shred" command, part of the GNU "fileutils" package,  75&nbsp;#+ does the same thing, although more efficiently.  76&nbsp;  77&nbsp;#  The file cannot not be "undeleted" or retrieved by normal methods.  78&nbsp;#  However . . .  79&nbsp;#+ this simple method would *not* likely withstand  80&nbsp;#+ sophisticated forensic analysis.  81&nbsp;  82&nbsp;#  This script may not play well with a journaled file system.  83&nbsp;#  Exercise (difficult): Fix it so it does.  84&nbsp;  85&nbsp;  86&nbsp;  87&nbsp;#  Tom Vier's "wipe" file-deletion package does a much more thorough job  88&nbsp;#+ of file shredding than this simple script.  89&nbsp;#     http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2  90&nbsp;  91&nbsp;#  For an in-depth analysis on the topic of file deletion and security,  92&nbsp;#+ see Peter Gutmann's paper,  93&nbsp;#+     "Secure Deletion of Data From Magnetic and Solid-State Memory".  94&nbsp;#       http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html</PRE></TD></TR></TABLE><HR></DIV><P>See also the <AHREF="biblio.html#DDLINK">dd	      thread</A> entry in the <AHREF="biblio.html#BIBLIOREF">bibliography</A>.</P></DD><DT><ANAME="ODREF"></A><BCLASS="COMMAND">od</B></DT><DD><P>The <BCLASS="COMMAND">od</B>, or <ICLASS="FIRSTTERM">octal	      dump</I> filter converts input (or files) to octal	      (base-8) or other bases. This is useful for viewing or	      processing binary data files or otherwise unreadable system	      device files, such as <TTCLASS="FILENAME">/dev/urandom</TT>,	      and as a filter for binary data.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'   2&nbsp;# Sample output: 1324725719, 3918166450, 2989231420, etc.   3&nbsp;   4&nbsp;# From rnd.sh example script, by St閜hane Chazelas</PRE></TD></TR></TABLE>	  </P><P>See also <AHREF="randomvar.html#SEEDINGRANDOM">Example 9-31</A> and <AHREF="contributed-scripts.html#INSERTIONSORT">Example A-38</A>.</P></DD><DT><ANAME="HEXDUMPREF"></A><BCLASS="COMMAND">hexdump</B></DT><DD><P>Performs a hexadecimal, octal, decimal, or ASCII	      dump of a binary file. This command is the rough equivalent	      of <BCLASS="COMMAND">od</B>, above, but not nearly as	      useful. May be used to view the contents of a binary file,	      in combination with <AHREF="extmisc.html#DDREF">dd</A> and <AHREF="filearchiv.html#LESSREF">less</A>.</P><P>	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;dd if=/bin/ls | hexdump -C | less   2&nbsp;# The -C option nicely formats the output in tabular form.</PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="OBJDUMPREF"></A><BCLASS="COMMAND">objdump</B></DT><DD><P>Displays information about an object file or binary	      executable in either hexadecimal form or as a disassembled	      listing (with the <TTCLASS="OPTION">-d</TT> option).</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>objdump -d /bin/ls</B></TT> <TTCLASS="COMPUTEROUTPUT">/bin/ls:     file format elf32-i386 Disassembly of section .init: 080490bc &#60;.init&#62;:  80490bc:       55                      push   %ebp  80490bd:       89 e5                   mov    %esp,%ebp  . . .</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="MCOOKIEREF"></A><BCLASS="COMMAND">mcookie</B></DT><DD><P>This command generates a <SPANCLASS="QUOTE">"magic cookie,"</SPAN> a	      128-bit (32-character) pseudorandom hexadecimal number,	      normally used as an authorization <SPANCLASS="QUOTE">"signature"</SPAN>	      by the X server. This also available for use in a script	      as a <SPANCLASS="QUOTE">"quick 'n dirty"</SPAN> random number.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;random000=$(mcookie)</PRE></TD></TR></TABLE></P><P>Of course, a script could use <AHREF="filearchiv.html#MD5SUMREF">md5</A> for the same purpose.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Generate md5 checksum on the script itself.   2&nbsp;random001=`md5sum $0 | awk '{print $1}'`   3&nbsp;# Uses 'awk' to strip off the filename.</PRE></TD></TR></TABLE></P><P>The <BCLASS="COMMAND">mcookie</B> command gives yet another way	      to generate a <SPANCLASS="QUOTE">"unique"</SPAN> filename.</P><DIVCLASS="EXAMPLE"><HR><ANAME="TEMPFILENAME"></A><P><B>Example 15-59. Filename generator</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# tempfile-name.sh:  temp filename generator   3&nbsp;   4&nbsp;BASE_STR=`mcookie`   # 32-character magic cookie.   5&nbsp;POS=11               # Arbitrary position in magic cookie string.   6&nbsp;LEN=5                # Get $LEN consecutive characters.   7&nbsp;   8&nbsp;prefix=temp          #  This is, after all, a "temp" file.   9&nbsp;                     #  For more "uniqueness," generate the  10&nbsp;                     #+ filename prefix using the same method  11&nbsp;                     #+ as the suffix, below.  12&nbsp;  13&nbsp;suffix=${BASE_STR:POS:LEN}  14&nbsp;                     #  Extract a 5-character string,  15&nbsp;                     #+ starting at position 11.  16&nbsp;  17&nbsp;temp_filename=$prefix.$suffix  18&nbsp;                     # Construct the filename.  19&nbsp;  20&nbsp;echo "Temp filename = "$temp_filename""  21&nbsp;  22&nbsp;# sh tempfile-name.sh  23&nbsp;# Temp filename = temp.e19ea  24&nbsp;  25&nbsp;#  Compare this method of generating "unique" filenames  26&nbsp;#+ with the 'date' method in ex51.sh.  27&nbsp;  28&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="UNITSREF"></A><BCLASS="COMMAND">units</B></DT><DD><P>This utility converts between different <ICLASS="FIRSTTERM">units	      of measure</I>. While normally invoked in interactive	      mode, <BCLASS="COMMAND">units</B> may find use in a	      script.</P

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -