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

📄 communications.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  27&nbsp;# -t: reserve time  28&nbsp;# -v: verbose  29&nbsp;  30&nbsp;OPTS="-rtv --delete-excluded --delete-after --partial"  31&nbsp;  32&nbsp;# rsync include pattern  33&nbsp;# Leading slash causes absolute path name match.  34&nbsp;INCLUDE=(  35&nbsp;    "/4/i386/kde-i18n-Chinese*"   36&nbsp;#   ^                         ^  37&nbsp;# Quoting is necessary to prevent globbing.  38&nbsp;)   39&nbsp;  40&nbsp;  41&nbsp;# rsync exclude pattern  42&nbsp;# Temporarily comment out unwanted pkgs using "#" . . .  43&nbsp;EXCLUDE=(  44&nbsp;    /1  45&nbsp;    /2  46&nbsp;    /3  47&nbsp;    /testing  48&nbsp;    /4/SRPMS  49&nbsp;    /4/ppc  50&nbsp;    /4/x86_64  51&nbsp;    /4/i386/debug  52&nbsp;   "/4/i386/kde-i18n-*"  53&nbsp;   "/4/i386/openoffice.org-langpack-*"  54&nbsp;   "/4/i386/*i586.rpm"  55&nbsp;   "/4/i386/GFS-*"  56&nbsp;   "/4/i386/cman-*"  57&nbsp;   "/4/i386/dlm-*"  58&nbsp;   "/4/i386/gnbd-*"  59&nbsp;   "/4/i386/kernel-smp*"  60&nbsp;#  "/4/i386/kernel-xen*"   61&nbsp;#  "/4/i386/xen-*"   62&nbsp;)  63&nbsp;  64&nbsp;  65&nbsp;init () {  66&nbsp;    # Let pipe command return possible rsync error, e.g., stalled network.  67&nbsp;    set -o pipefail                  # Newly introduced in Bash, version 3.  68&nbsp;  69&nbsp;    TMP=${TMPDIR:-/tmp}/${0##*/}.$$  # Store refined download list.  70&nbsp;    trap "{  71&nbsp;        rm -f $TMP 2&#62;/dev/null  72&nbsp;    }" EXIT                          # Clear temporary file on exit.  73&nbsp;}  74&nbsp;  75&nbsp;  76&nbsp;check_pid () {  77&nbsp;# Check if process exists.  78&nbsp;    if [ -s "$PID_FILE" ]; then  79&nbsp;        echo "PID file exists. Checking ..."  80&nbsp;        PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE)  81&nbsp;        if /bin/ps --pid $PID &#38;&#62;/dev/null; then  82&nbsp;            echo "Process $PID found. ${0##*/} seems to be running!"  83&nbsp;           /usr/bin/logger -t ${0##*/} \  84&nbsp;                 "Process $PID found. ${0##*/} seems to be running!"  85&nbsp;            exit $E_RETURN  86&nbsp;        fi  87&nbsp;        echo "Process $PID not found. Start new process . . ."  88&nbsp;    fi  89&nbsp;}  90&nbsp;  91&nbsp;  92&nbsp;#  Set overall file update range starting from root or $URL,  93&nbsp;#+ according to above patterns.  94&nbsp;set_range () {  95&nbsp;    include=  96&nbsp;    exclude=  97&nbsp;    for p in "${INCLUDE[@]}"; do  98&nbsp;        include="$include --include \"$p\""  99&nbsp;    done 100&nbsp; 101&nbsp;    for p in "${EXCLUDE[@]}"; do 102&nbsp;        exclude="$exclude --exclude \"$p\"" 103&nbsp;    done 104&nbsp;} 105&nbsp; 106&nbsp; 107&nbsp;# Retrieve and refine rsync update list. 108&nbsp;get_list () { 109&nbsp;    echo $$ &#62; $PID_FILE || { 110&nbsp;        echo "Can't write to pid file $PID_FILE" 111&nbsp;        exit $E_RETURN 112&nbsp;    } 113&nbsp; 114&nbsp;    echo -n "Retrieving and refining update list . . ." 115&nbsp; 116&nbsp;    # Retrieve list -- 'eval' is needed to run rsync as a single command. 117&nbsp;    # $3 and $4 is the date and time of file creation. 118&nbsp;    # $5 is the full package name. 119&nbsp;    previous= 120&nbsp;    pre_file= 121&nbsp;    pre_date=0 122&nbsp;    eval /bin/nice /usr/bin/rsync \ 123&nbsp;        -r $include $exclude $URL | \ 124&nbsp;        egrep '^dr.x|^-r' | \ 125&nbsp;        awk '{print $3, $4, $5}' | \ 126&nbsp;        sort -k3 | \ 127&nbsp;        { while read line; do 128&nbsp;            # Get seconds since epoch, to filter out obsolete pkgs. 129&nbsp;            cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s) 130&nbsp;            #  echo $cur_date 131&nbsp; 132&nbsp;            # Get file name. 133&nbsp;            cur_file=$(echo $line | awk '{print $3}') 134&nbsp;            #  echo $cur_file 135&nbsp; 136&nbsp;            # Get rpm pkg name from file name, if possible. 137&nbsp;            if [[ $cur_file == *rpm ]]; then 138&nbsp;                pkg_name=$(echo $cur_file | sed -r -e \ 139&nbsp;                    's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/') 140&nbsp;            else 141&nbsp;                pkg_name= 142&nbsp;            fi 143&nbsp;            # echo $pkg_name 144&nbsp; 145&nbsp;            if [ -z "$pkg_name" ]; then   #  If not a rpm file, 146&nbsp;                echo $cur_file &#62;&#62; $TMP    #+ then append to download list. 147&nbsp;            elif [ "$pkg_name" != "$previous" ]; then   # A new pkg found. 148&nbsp;                echo $pre_file &#62;&#62; $TMP                  # Output latest file. 149&nbsp;                previous=$pkg_name                      # Save current. 150&nbsp;                pre_date=$cur_date 151&nbsp;                pre_file=$cur_file 152&nbsp;            elif [ "$cur_date" -gt "$pre_date" ]; then 153&nbsp;                                                #  If same pkg, but newer, 154&nbsp;                pre_date=$cur_date              #+ then update latest pointer. 155&nbsp;                pre_file=$cur_file 156&nbsp;            fi 157&nbsp;            done 158&nbsp;            echo $pre_file &#62;&#62; $TMP              #  TMP contains ALL 159&nbsp;                                                #+ of refined list now. 160&nbsp;            # echo "subshell=$BASH_SUBSHELL" 161&nbsp; 162&nbsp;    }       # Bracket required here to let final "echo $pre_file &#62;&#62; $TMP"  163&nbsp;            # Remained in the same subshell ( 1 ) with the entire loop. 164&nbsp; 165&nbsp;    RET=$?  # Get return code of the pipe command. 166&nbsp; 167&nbsp;    [ "$RET" -ne 0 ] &#38;&#38; { 168&nbsp;        echo "List retrieving failed with code $RET" 169&nbsp;        exit $E_RETURN 170&nbsp;    } 171&nbsp; 172&nbsp;    echo "done"; echo 173&nbsp;} 174&nbsp; 175&nbsp;# Real rsync download part. 176&nbsp;get_file () { 177&nbsp; 178&nbsp;    echo "Downloading..." 179&nbsp;    /bin/nice /usr/bin/rsync \ 180&nbsp;        $OPTS \ 181&nbsp;        --filter "merge,+/ $TMP" \ 182&nbsp;        --exclude '*'  \ 183&nbsp;        $URL $DEST     \ 184&nbsp;        | /usr/bin/tee $LOG 185&nbsp; 186&nbsp;    RET=$? 187&nbsp; 188&nbsp;   #  --filter merge,+/ is crucial for the intention.  189&nbsp;   #  + modifier means include and / means absolute path. 190&nbsp;   #  Then sorted list in $TMP will contain ascending dir name and  191&nbsp;   #+ prevent the following --exclude '*' from "shortcutting the circuit."  192&nbsp; 193&nbsp;    echo "Done" 194&nbsp; 195&nbsp;    rm -f $PID_FILE 2&#62;/dev/null 196&nbsp; 197&nbsp;    return $RET 198&nbsp;} 199&nbsp; 200&nbsp;# ------- 201&nbsp;# Main 202&nbsp;init 203&nbsp;check_pid 204&nbsp;set_range 205&nbsp;get_list 206&nbsp;get_file 207&nbsp;RET=$? 208&nbsp;# ------- 209&nbsp; 210&nbsp;if [ "$RET" -eq 0 ]; then 211&nbsp;    /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully." 212&nbsp;else 213&nbsp;    /usr/bin/logger -t ${0##*/} \ 214&nbsp;    "Fedora update mirrored with failure code: $RET" 215&nbsp;fi 216&nbsp; 217&nbsp;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&nbsp;#!/bin/bash   2&nbsp;# remote.bash: Using ssh.   3&nbsp;   4&nbsp;# This example by Michael Zick.   5&nbsp;# Used with permission.   6&nbsp;   7&nbsp;   8&nbsp;#   Presumptions:   9&nbsp;#   ------------  10&nbsp;#   fd-2 isn't being captured ( '2&#62;/dev/null' ).  11&nbsp;#   ssh/sshd presumes stderr ('2') will display to user.  12&nbsp;#  13&nbsp;#   sshd is running on your machine.  14&nbsp;#   For any 'standard' distribution, it probably is,  15&nbsp;#+  and without any funky ssh-keygen having been done.  16&nbsp;  17&nbsp;# Try ssh to your machine from the command line:  18&nbsp;#  19&nbsp;# $ ssh $HOSTNAME  20&nbsp;# Without extra set-up you'll be asked for your password.  21&nbsp;#   enter password  22&nbsp;#   when done,  $ exit  23&nbsp;#  24&nbsp;# Did that work? If so, you're ready for more fun.  25&nbsp;  26&nbsp;# Try ssh to your machine as 'root':  27&nbsp;#  28&nbsp;#   $  ssh -l root $HOSTNAME  29&nbsp;#   When asked for password, enter root's, not yours.  30&nbsp;#          Last login: Tue Aug 10 20:25:49 2004 from localhost.localdomain  31&nbsp;#   Enter 'exit' when done.  32&nbsp;  33&nbsp;#  The above gives you an interactive shell.  34&nbsp;#  It is possible for sshd to be set up in a 'single command' mode,  35&nbsp;#+ but that is beyond the scope of this example.  36&nbsp;#  The only thing to note is that the following will work in  37&nbsp;#+ 'single command' mode.  38&nbsp;  39&nbsp;  40&nbsp;# A basic, write stdout (local) command.  41&nbsp;  42&nbsp;ls -l  43&nbsp;  44&nbsp;# Now the same basic command on a remote machine.  45&nbsp;# Pass a different 'USERNAME' 'HOSTNAME' if desired:  46&nbsp;USER=${USERNAME:-$(whoami)}  47&nbsp;HOST=${HOSTNAME:-$(hostname)}  48&nbsp;  49&nbsp;#  Now excute the above command line on the remote host,  50&nbsp;#+ with all transmissions encrypted.  51&nbsp;  52&nbsp;ssh -l ${USER} ${HOST} " ls -l "  53&nbsp;  54&nbsp;#  The expected result is a listing of your username's home  55&nbsp;#+ directory on the remote machine.  56&nbsp;#  To see any difference, run this script from somewhere  57&nbsp;#+ other than your home directory.  58&nbsp;  59&nbsp;#  In other words, the Bash command is passed as a quoted line  60&nbsp;#+ to the remote shell, which executes it on the remote machine.  61&nbsp;#  In this case, sshd does  ' bash -c "ls -l" '   on your behalf.  62&nbsp;  63&nbsp;#  For information on topics such as not having to enter a  64&nbsp;#+ password/passphrase for every command line, see  65&nbsp;#+    man ssh  66&nbsp;#+    man ssh-keygen  67&nbsp;#+    man sshd_config.  68&nbsp;  69&nbsp;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&nbsp;#!/bin/sh   2&nbsp;# self-mailer.sh: Self-mailing script   3&nbsp;   4&nbsp;adr=${1:-`whoami`}     # Default to current user, if not specified.   5&nbsp;#  Typing 'self-mailer.sh wiseguy@superdupergenius.com'   6&nbsp;#+ sends this script to that addressee.   7&nbsp;#  Just 'self-mailer.sh' (no argument) sends the script   8&nbsp;#+ to the person invoking it, for example, bozo@localhost.localdomain.   9&nbsp;#  10&nbsp;#  For more on the ${parameter:-default} construct,  11&nbsp;#+ see the "Parameter Substitution" section  12&nbsp;#+ of the "Variables Revisited" chapter.  13&nbsp;  14&nbsp;# ============================================================================  15&nbsp;  cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$adr"  16&nbsp;# ============================================================================  17&nbsp;  18&nbsp;# --------------------------------------------  19&nbsp;#  Greetings from the self-mailing script.  20&nbsp;#  A mischievous person has run this script,  21&nbsp;#+ which has caused it to mail itself to you.  22&nbsp;#  Apparently, some people have nothing better  23&nbsp;#+ to do with their time.  24&nbsp;# --------------------------------------------  25&nbsp;  26&nbsp;echo "At `date`, script \"`basename $0`\" mailed to "$adr"."  27&nbsp;  28&nbsp;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 + -