📄 extmisc.html
字号:
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 #!/bin/bash 2 # self-copy.sh 3 4 # This script copies itself. 5 6 file_subscript=copy 7 8 dd if=$0 of=$0.$file_subscript 2>/dev/null 9 # Suppress messages from dd: ^^^^^^^^^^^ 10 11 exit $? 12 13 # A program whose only output is its own source code 14 #+ is called a "quine" per Willard Quine. 15 # 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 #!/bin/bash 2 # exercising-dd.sh 3 4 # Script by Stephane Chazelas. 5 # Somewhat modified by ABS Guide author. 6 7 infile=$0 # This script. 8 outfile=log.txt # This output file left behind. 9 n=3 10 p=5 11 12 dd if=$infile of=$outfile bs=1 skip=$((n-1)) count=$((p-n+1)) 2> /dev/null 13 # Extracts characters n to p (3 to 5) from this script. 14 15 # -------------------------------------------------------- 16 17 echo -n "hello world" | dd cbs=1 conv=unblock 2> /dev/null 18 # Echoes "hello world" vertically. 19 # Why? Newline after each character dd emits. 20 21 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 #!/bin/bash 2 # dd-keypress.sh: Capture keystrokes without needing to press ENTER. 3 4 5 keypresses=4 # Number of keypresses to capture. 6 7 8 old_tty_setting=$(stty -g) # Save old terminal settings. 9 10 echo "Press $keypresses keys." 11 stty -icanon -echo # Disable canonical mode. 12 # Disable local echo. 13 keys=$(dd bs=1 count=$keypresses 2> /dev/null) 14 # 'dd' uses stdin, if "if" (input file) not specified. 15 16 stty "$old_tty_setting" # Restore old terminal settings. 17 18 echo "You pressed the \"$keys\" keys." 19 20 # Thanks, Stephane Chazelas, for showing the way. 21 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 echo -n . | dd bs=1 seek=4 of=file conv=notrunc 2 # The "conv=notrunc" option means that the output file 3 #+ will not be truncated. 4 5 # 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 #!/bin/bash 2 # blot-out.sh: Erase "all" traces of a file. 3 4 # This script overwrites a target file alternately 5 #+ with random bytes, then zeros before finally deleting it. 6 # After that, even examining the raw disk sectors by conventional methods 7 #+ will not reveal the original file data. 8 9 PASSES=7 # Number of file-shredding passes. 10 # Increasing this slows script execution, 11 #+ especially on large target files. 12 BLOCKSIZE=1 # I/O with /dev/urandom requires unit block size, 13 #+ otherwise you get weird results. 14 E_BADARGS=70 # Various error exit codes. 15 E_NOT_FOUND=71 16 E_CHANGED_MIND=72 17 18 if [ -z "$1" ] # No filename specified. 19 then 20 echo "Usage: `basename $0` filename" 21 exit $E_BADARGS 22 fi 23 24 file=$1 25 26 if [ ! -e "$file" ] 27 then 28 echo "File \"$file\" not found." 29 exit $E_NOT_FOUND 30 fi 31 32 echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? " 33 read answer 34 case "$answer" in 35 [nN]) echo "Changed your mind, huh?" 36 exit $E_CHANGED_MIND 37 ;; 38 *) echo "Blotting out file \"$file\".";; 39 esac 40 41 42 flength=$(ls -l "$file" | awk '{print $5}') # Field 5 is file length. 43 pass_count=1 44 45 chmod u+w "$file" # Allow overwriting/deleting the file. 46 47 echo 48 49 while [ "$pass_count" -le "$PASSES" ] 50 do 51 echo "Pass #$pass_count" 52 sync # Flush buffers. 53 dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength 54 # Fill with random bytes. 55 sync # Flush buffers again. 56 dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength 57 # Fill with zeros. 58 sync # Flush buffers yet again. 59 let "pass_count += 1" 60 echo 61 done 62 63 64 rm -f $file # Finally, delete scrambled and shredded file. 65 sync # Flush buffers a final time. 66 67 echo "File \"$file\" blotted out and deleted."; echo 68 69 70 exit 0 71 72 # This is a fairly secure, if inefficient and slow method 73 #+ of thoroughly "shredding" a file. 74 # The "shred" command, part of the GNU "fileutils" package, 75 #+ does the same thing, although more efficiently. 76 77 # The file cannot not be "undeleted" or retrieved by normal methods. 78 # However . . . 79 #+ this simple method would *not* likely withstand 80 #+ sophisticated forensic analysis. 81 82 # This script may not play well with a journaled file system. 83 # Exercise (difficult): Fix it so it does. 84 85 86 87 # Tom Vier's "wipe" file-deletion package does a much more thorough job 88 #+ of file shredding than this simple script. 89 # http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2 90 91 # For an in-depth analysis on the topic of file deletion and security, 92 #+ see Peter Gutmann's paper, 93 #+ "Secure Deletion of Data From Magnetic and Solid-State Memory". 94 # 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 head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' 2 # Sample output: 1324725719, 3918166450, 2989231420, etc. 3 4 # 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 dd if=/bin/ls | hexdump -C | less 2 # 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 <.init>: 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 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 # Generate md5 checksum on the script itself. 2 random001=`md5sum $0 | awk '{print $1}'` 3 # 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 #!/bin/bash 2 # tempfile-name.sh: temp filename generator 3 4 BASE_STR=`mcookie` # 32-character magic cookie. 5 POS=11 # Arbitrary position in magic cookie string. 6 LEN=5 # Get $LEN consecutive characters. 7 8 prefix=temp # This is, after all, a "temp" file. 9 # For more "uniqueness," generate the 10 #+ filename prefix using the same method 11 #+ as the suffix, below. 12 13 suffix=${BASE_STR:POS:LEN} 14 # Extract a 5-character string, 15 #+ starting at position 11. 16 17 temp_filename=$prefix.$suffix 18 # Construct the filename. 19 20 echo "Temp filename = "$temp_filename"" 21 22 # sh tempfile-name.sh 23 # Temp filename = temp.e19ea 24 25 # Compare this method of generating "unique" filenames 26 #+ with the 'date' method in ex51.sh. 27 28 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 + -