📄 communications.html
字号:
27 # -t: reserve time 28 # -v: verbose 29 30 OPTS="-rtv --delete-excluded --delete-after --partial" 31 32 # rsync include pattern 33 # Leading slash causes absolute path name match. 34 INCLUDE=( 35 "/4/i386/kde-i18n-Chinese*" 36 # ^ ^ 37 # Quoting is necessary to prevent globbing. 38 ) 39 40 41 # rsync exclude pattern 42 # Temporarily comment out unwanted pkgs using "#" . . . 43 EXCLUDE=( 44 /1 45 /2 46 /3 47 /testing 48 /4/SRPMS 49 /4/ppc 50 /4/x86_64 51 /4/i386/debug 52 "/4/i386/kde-i18n-*" 53 "/4/i386/openoffice.org-langpack-*" 54 "/4/i386/*i586.rpm" 55 "/4/i386/GFS-*" 56 "/4/i386/cman-*" 57 "/4/i386/dlm-*" 58 "/4/i386/gnbd-*" 59 "/4/i386/kernel-smp*" 60 # "/4/i386/kernel-xen*" 61 # "/4/i386/xen-*" 62 ) 63 64 65 init () { 66 # Let pipe command return possible rsync error, e.g., stalled network. 67 set -o pipefail # Newly introduced in Bash, version 3. 68 69 TMP=${TMPDIR:-/tmp}/${0##*/}.$$ # Store refined download list. 70 trap "{ 71 rm -f $TMP 2>/dev/null 72 }" EXIT # Clear temporary file on exit. 73 } 74 75 76 check_pid () { 77 # Check if process exists. 78 if [ -s "$PID_FILE" ]; then 79 echo "PID file exists. Checking ..." 80 PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE) 81 if /bin/ps --pid $PID &>/dev/null; then 82 echo "Process $PID found. ${0##*/} seems to be running!" 83 /usr/bin/logger -t ${0##*/} \ 84 "Process $PID found. ${0##*/} seems to be running!" 85 exit $E_RETURN 86 fi 87 echo "Process $PID not found. Start new process . . ." 88 fi 89 } 90 91 92 # Set overall file update range starting from root or $URL, 93 #+ according to above patterns. 94 set_range () { 95 include= 96 exclude= 97 for p in "${INCLUDE[@]}"; do 98 include="$include --include \"$p\"" 99 done 100 101 for p in "${EXCLUDE[@]}"; do 102 exclude="$exclude --exclude \"$p\"" 103 done 104 } 105 106 107 # Retrieve and refine rsync update list. 108 get_list () { 109 echo $$ > $PID_FILE || { 110 echo "Can't write to pid file $PID_FILE" 111 exit $E_RETURN 112 } 113 114 echo -n "Retrieving and refining update list . . ." 115 116 # Retrieve list -- 'eval' is needed to run rsync as a single command. 117 # $3 and $4 is the date and time of file creation. 118 # $5 is the full package name. 119 previous= 120 pre_file= 121 pre_date=0 122 eval /bin/nice /usr/bin/rsync \ 123 -r $include $exclude $URL | \ 124 egrep '^dr.x|^-r' | \ 125 awk '{print $3, $4, $5}' | \ 126 sort -k3 | \ 127 { while read line; do 128 # Get seconds since epoch, to filter out obsolete pkgs. 129 cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s) 130 # echo $cur_date 131 132 # Get file name. 133 cur_file=$(echo $line | awk '{print $3}') 134 # echo $cur_file 135 136 # Get rpm pkg name from file name, if possible. 137 if [[ $cur_file == *rpm ]]; then 138 pkg_name=$(echo $cur_file | sed -r -e \ 139 's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/') 140 else 141 pkg_name= 142 fi 143 # echo $pkg_name 144 145 if [ -z "$pkg_name" ]; then # If not a rpm file, 146 echo $cur_file >> $TMP #+ then append to download list. 147 elif [ "$pkg_name" != "$previous" ]; then # A new pkg found. 148 echo $pre_file >> $TMP # Output latest file. 149 previous=$pkg_name # Save current. 150 pre_date=$cur_date 151 pre_file=$cur_file 152 elif [ "$cur_date" -gt "$pre_date" ]; then 153 # If same pkg, but newer, 154 pre_date=$cur_date #+ then update latest pointer. 155 pre_file=$cur_file 156 fi 157 done 158 echo $pre_file >> $TMP # TMP contains ALL 159 #+ of refined list now. 160 # echo "subshell=$BASH_SUBSHELL" 161 162 } # Bracket required here to let final "echo $pre_file >> $TMP" 163 # Remained in the same subshell ( 1 ) with the entire loop. 164 165 RET=$? # Get return code of the pipe command. 166 167 [ "$RET" -ne 0 ] && { 168 echo "List retrieving failed with code $RET" 169 exit $E_RETURN 170 } 171 172 echo "done"; echo 173 } 174 175 # Real rsync download part. 176 get_file () { 177 178 echo "Downloading..." 179 /bin/nice /usr/bin/rsync \ 180 $OPTS \ 181 --filter "merge,+/ $TMP" \ 182 --exclude '*' \ 183 $URL $DEST \ 184 | /usr/bin/tee $LOG 185 186 RET=$? 187 188 # --filter merge,+/ is crucial for the intention. 189 # + modifier means include and / means absolute path. 190 # Then sorted list in $TMP will contain ascending dir name and 191 #+ prevent the following --exclude '*' from "shortcutting the circuit." 192 193 echo "Done" 194 195 rm -f $PID_FILE 2>/dev/null 196 197 return $RET 198 } 199 200 # ------- 201 # Main 202 init 203 check_pid 204 set_range 205 get_list 206 get_file 207 RET=$? 208 # ------- 209 210 if [ "$RET" -eq 0 ]; then 211 /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully." 212 else 213 /usr/bin/logger -t ${0##*/} \ 214 "Fedora update mirrored with failure code: $RET" 215 fi 216 217 exit $RET</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="contributed-scripts.html#NIGHTLYBACKUP">Example A-34</A>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Using <AHREF="communications.html#RCPREF">rcp</A>, <AHREF="communications.html#RSYNCREF">rsync</A>, and similar utilities with security implications in a shell script may not be advisable. Consider, instead, using <BCLASS="COMMAND">ssh</B>, <AHREF="communications.html#SCPREF">scp</A>, or an <BCLASS="COMMAND">expect</B> script.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="SSHREF"></A><BCLASS="COMMAND">ssh</B></DT><DD><P><TTCLASS="REPLACEABLE"><I>Secure shell</I></TT>, logs onto a remote host and executes commands there. This secure replacement for <BCLASS="COMMAND">telnet</B>, <BCLASS="COMMAND">rlogin</B>, <BCLASS="COMMAND">rcp</B>, and <BCLASS="COMMAND">rsh</B> uses identity authentication and encryption. See its <AHREF="external.html#MANREF">manpage</A> for details.</P><DIVCLASS="EXAMPLE"><HR><ANAME="REMOTE"></A><P><B>Example 15-43. Using <ICLASS="FIRSTTERM">ssh</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # remote.bash: Using ssh. 3 4 # This example by Michael Zick. 5 # Used with permission. 6 7 8 # Presumptions: 9 # ------------ 10 # fd-2 isn't being captured ( '2>/dev/null' ). 11 # ssh/sshd presumes stderr ('2') will display to user. 12 # 13 # sshd is running on your machine. 14 # For any 'standard' distribution, it probably is, 15 #+ and without any funky ssh-keygen having been done. 16 17 # Try ssh to your machine from the command line: 18 # 19 # $ ssh $HOSTNAME 20 # Without extra set-up you'll be asked for your password. 21 # enter password 22 # when done, $ exit 23 # 24 # Did that work? If so, you're ready for more fun. 25 26 # Try ssh to your machine as 'root': 27 # 28 # $ ssh -l root $HOSTNAME 29 # When asked for password, enter root's, not yours. 30 # Last login: Tue Aug 10 20:25:49 2004 from localhost.localdomain 31 # Enter 'exit' when done. 32 33 # The above gives you an interactive shell. 34 # It is possible for sshd to be set up in a 'single command' mode, 35 #+ but that is beyond the scope of this example. 36 # The only thing to note is that the following will work in 37 #+ 'single command' mode. 38 39 40 # A basic, write stdout (local) command. 41 42 ls -l 43 44 # Now the same basic command on a remote machine. 45 # Pass a different 'USERNAME' 'HOSTNAME' if desired: 46 USER=${USERNAME:-$(whoami)} 47 HOST=${HOSTNAME:-$(hostname)} 48 49 # Now excute the above command line on the remote host, 50 #+ with all transmissions encrypted. 51 52 ssh -l ${USER} ${HOST} " ls -l " 53 54 # The expected result is a listing of your username's home 55 #+ directory on the remote machine. 56 # To see any difference, run this script from somewhere 57 #+ other than your home directory. 58 59 # In other words, the Bash command is passed as a quoted line 60 #+ to the remote shell, which executes it on the remote machine. 61 # In this case, sshd does ' bash -c "ls -l" ' on your behalf. 62 63 # For information on topics such as not having to enter a 64 #+ password/passphrase for every command line, see 65 #+ man ssh 66 #+ man ssh-keygen 67 #+ man sshd_config. 68 69 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Within a loop, <BCLASS="COMMAND">ssh</B> may cause unexpected behavior. According to a <AHREF="http://groups-beta.google.com/group/comp.unix.shell/msg/dcb446b5fff7d230"TARGET="_top"> Usenet post</A> in the comp.unix shell archives, <BCLASS="COMMAND">ssh</B> inherits the loop's <TTCLASS="FILENAME">stdin</TT>. To remedy this, pass <BCLASS="COMMAND">ssh</B> either the <TTCLASS="OPTION">-n</TT> or <TTCLASS="OPTION">-f</TT> option.</P><P>Thanks, Jason Bechtel, for pointing this out.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="SCPREF"></A><BCLASS="COMMAND">scp</B></DT><DD><P><TTCLASS="REPLACEABLE"><I>Secure copy</I></TT>, similar in function to <BCLASS="COMMAND">rcp</B>, copies files between two different networked machines, but does so using authentication, and with a security level similar to <BCLASS="COMMAND">ssh</B>.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMMLOCAL1"></A>Local Network</B></P><DL><DT><ANAME="WRITEREF"></A><BCLASS="COMMAND">write</B></DT><DD><P>This is a utility for terminal-to-terminal communication. It allows sending lines from your terminal (console or <ICLASS="FIRSTTERM">xterm</I>) to that of another user. The <AHREF="system.html#MESGREF">mesg</A> command may, of course, be used to disable write access to a terminal</P><P>Since <BCLASS="COMMAND">write</B> is interactive, it would not normally find use in a script.</P></DD><DT><ANAME="NETCONFIGREF"></A><BCLASS="COMMAND">netconfig</B></DT><DD><P>A command-line utility for configuring a network adapter (using <ICLASS="FIRSTTERM">DHCP</I>). This command is native to Red Hat centric Linux distros.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMMMAIL1"></A>Mail</B></P><DL><DT><BCLASS="COMMAND">mail</B></DT><DD><P>Send or read e-mail messages.</P><P>This stripped-down command-line mail client works fine as a command embedded in a script.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SELFMAILER"></A><P><B>Example 15-44. A script that mails itself</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/sh 2 # self-mailer.sh: Self-mailing script 3 4 adr=${1:-`whoami`} # Default to current user, if not specified. 5 # Typing 'self-mailer.sh wiseguy@superdupergenius.com' 6 #+ sends this script to that addressee. 7 # Just 'self-mailer.sh' (no argument) sends the script 8 #+ to the person invoking it, for example, bozo@localhost.localdomain. 9 # 10 # For more on the ${parameter:-default} construct, 11 #+ see the "Parameter Substitution" section 12 #+ of the "Variables Revisited" chapter. 13 14 # ============================================================================ 15 cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$adr" 16 # ============================================================================ 17 18 # -------------------------------------------- 19 # Greetings from the self-mailing script. 20 # A mischievous person has run this script, 21 #+ which has caused it to mail itself to you. 22 # Apparently, some people have nothing better 23 #+ to do with their time. 24 # -------------------------------------------- 25 26 echo "At `date`, script \"`basename $0`\" mailed to "$adr"." 27 28 exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="MAILTOREF"></A><BCLASS="COMMAND">mailto</B></DT><DD><P>Similar to the <BCLASS="COMMAND">mail</B> command, <BCLASS="COMMAND">mailto</B> sends e-mail messages from the command line or in a script. However, <BCLASS="COMMAND">mailto</B> also permits sending MIME (multimedia) messages.</P></DD><DT><ANAME="VACATIONREF"></A><BCLASS="COMMAND">vacation</B></DT><DD><P>This utility automatically replies to e-mails that the intended recipient is on vacation and temporarily unavailable. It runs on a network, in conjunction with <BCLASS="COMMAND">sendmail</B>, and is not applicable to a dial-up POPmail account.</P></DD></DL></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN12186"HREF="communications.html#AEN12186">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="DAEMONREF"></A></P><P>A <ICLASS="FIRSTTERM">daemon</I> is a background process not attached to a terminal session. Daemons perform designated services either at specified times or explicitly triggered by certain events.</P><P>The word <SPANCLASS="QUOTE">"daemon"</SPAN> means ghost in Greek, and there is certainly something mysterious, almost supernatural, about the way UNIX daemons wander about behind the scenes, silently carrying out their appointed tasks.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="filearchiv.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="terminalccmds.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">File and Archiving Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="external.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Terminal Control Commands</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -