📄 communications.html
字号:
29 OPTS="-rtv --delete-excluded --delete-after --partial" 30 31 # rsync include pattern 32 # Leading slash causes absolute path name match. 33 INCLUDE=( 34 "/4/i386/kde-i18n-Chinese*" 35 # ^ ^ 36 # Quoting is necessary to prevent globbing. 37 ) 38 39 40 # rsync exclude pattern 41 # Temporarily comment out unwanted pkgs using "#" . . . 42 EXCLUDE=( 43 /1 44 /2 45 /3 46 /testing 47 /4/SRPMS 48 /4/ppc 49 /4/x86_64 50 /4/i386/debug 51 "/4/i386/kde-i18n-*" 52 "/4/i386/openoffice.org-langpack-*" 53 "/4/i386/*i586.rpm" 54 "/4/i386/GFS-*" 55 "/4/i386/cman-*" 56 "/4/i386/dlm-*" 57 "/4/i386/gnbd-*" 58 "/4/i386/kernel-smp*" 59 # "/4/i386/kernel-xen*" 60 # "/4/i386/xen-*" 61 ) 62 63 64 init () { 65 # Let pipe command return possible rsync error, e.g., stalled network. 66 set -o pipefail 67 68 TMP=${TMPDIR:-/tmp}/${0##*/}.$$ # Store refined download list. 69 trap "{ 70 rm -f $TMP 2>/dev/null 71 }" EXIT # Clear temporary file on exit. 72 } 73 74 75 check_pid () { 76 # Check if process exists. 77 if [ -s "$PID_FILE" ]; then 78 echo "PID file exists. Checking ..." 79 PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE) 80 if /bin/ps --pid $PID &>/dev/null; then 81 echo "Process $PID found. ${0##*/} seems to be running!" 82 /usr/bin/logger -t ${0##*/} \ 83 "Process $PID found. ${0##*/} seems to be running!" 84 exit $E_RETURN 85 fi 86 echo "Process $PID not found. Start new process . . ." 87 fi 88 } 89 90 91 # Set overall file update range starting from root or $URL, 92 #+ according to above patterns. 93 set_range () { 94 include= 95 exclude= 96 for p in "${INCLUDE[@]}"; do 97 include="$include --include \"$p\"" 98 done 99 100 for p in "${EXCLUDE[@]}"; do 101 exclude="$exclude --exclude \"$p\"" 102 done 103 } 104 105 106 # Retrieve and refine rsync update list. 107 get_list () { 108 echo $$ > $PID_FILE || { 109 echo "Can't write to pid file $PID_FILE" 110 exit $E_RETURN 111 } 112 113 echo -n "Retrieving and refining update list . . ." 114 115 # Retrieve list -- 'eval' is needed to run rsync as a single command. 116 # $3 and $4 is the date and time of file creation. 117 # $5 is the full package name. 118 previous= 119 pre_file= 120 pre_date=0 121 eval /bin/nice /usr/bin/rsync \ 122 -r $include $exclude $URL | \ 123 egrep '^dr.x|^-r' | \ 124 awk '{print $3, $4, $5}' | \ 125 sort -k3 | \ 126 { while read line; do 127 # Get seconds since epoch, to filter out obsolete pkgs. 128 cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s) 129 # echo $cur_date 130 131 # Get file name. 132 cur_file=$(echo $line | awk '{print $3}') 133 # echo $cur_file 134 135 # Get rpm pkg name from file name, if possible. 136 if [[ $cur_file == *rpm ]]; then 137 pkg_name=$(echo $cur_file | sed -r -e \ 138 's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/') 139 else 140 pkg_name= 141 fi 142 # echo $pkg_name 143 144 if [ -z "$pkg_name" ]; then # If not a rpm file, 145 echo $cur_file >> $TMP #+ then append to download list. 146 elif [ "$pkg_name" != "$previous" ]; then # A new pkg found. 147 echo $pre_file >> $TMP # Output latest file. 148 previous=$pkg_name # Save current. 149 pre_date=$cur_date 150 pre_file=$cur_file 151 elif [ "$cur_date" -gt "$pre_date" ]; then # If same pkg, but newer, 152 pre_date=$cur_date #+ then update latest pointer. 153 pre_file=$cur_file 154 fi 155 done 156 echo $pre_file >> $TMP # TMP contains ALL 157 #+ of refined list now. 158 # echo "subshell=$BASH_SUBSHELL" 159 160 } # Bracket required here to let final "echo $pre_file >> $TMP" 161 # Remained in the same subshell ( 1 ) with the entire loop. 162 163 RET=$? # Get return code of the pipe command. 164 165 [ "$RET" -ne 0 ] && { 166 echo "List retrieving failed with code $RET" 167 exit $E_RETURN 168 } 169 170 echo "done"; echo 171 } 172 173 # Real rsync download part. 174 get_file () { 175 176 echo "Downloading..." 177 /bin/nice /usr/bin/rsync \ 178 $OPTS \ 179 --filter "merge,+/ $TMP" \ 180 --exclude '*' \ 181 $URL $DEST \ 182 | /usr/bin/tee $LOG 183 184 RET=$? 185 186 # --filter merge,+/ is crucial for the intention. 187 # + modifier means include and / means absolute path. 188 # Then sorted list in $TMP will contain ascending dir name and 189 #+ prevent the following --exclude '*' from "shortcutting the circuit." 190 191 echo "Done" 192 193 rm -f $PID_FILE 2>/dev/null 194 195 return $RET 196 } 197 198 # ------- 199 # Main 200 init 201 check_pid 202 set_range 203 get_list 204 get_file 205 RET=$? 206 # ------- 207 208 if [ "$RET" -eq 0 ]; then 209 /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully." 210 else 211 /usr/bin/logger -t ${0##*/} "Fedora update mirrored with failure code: $RET" 212 fi 213 214 exit $RET</PRE></TD></TR></TABLE><HR></DIV><P>Using <BCLASS="COMMAND">rcp</B>, <BCLASS="COMMAND">rsync</B>, and similar utilities with security implications in a shell script may not be advisable. Consider, instead, using <BCLASS="COMMAND">ssh</B>, <BCLASS="COMMAND">scp</B>, or an <BCLASS="COMMAND">expect</B> script.</P></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 <ICLASS="EMPHASIS">manpage</I> for details.</P><DIVCLASS="EXAMPLE"><HR><ANAME="REMOTE"></A><P><B>Example 12-40. Using ssh</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><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><BCLASS="COMMAND">netconfig</B></DT><DD><P>A command-line utility for configuring a network adapter (using DHCP). 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 12-41. 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><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><BCLASS="COMMAND">vacation</B></DT><DD><P>This utility automatically replies to e-mails that the intended recipient is on vacation and temporarily unavailable. This 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.AEN9739"HREF="communications.html#AEN9739">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="DAEMONREF"></A></P><P>A <ICLASS="EMPHASIS">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 silently wander about behind the scenes, carrying out their appointed tasks.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="filearchiv.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="terminalccmds.html">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">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 + -