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

📄 communications.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Communications Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="External Filters, Programs and Commands"HREF="external.html"><LINKREL="PREVIOUS"TITLE="File and Archiving Commands"HREF="filearchiv.html"><LINKREL="NEXT"TITLE="Terminal Control Commands"HREF="terminalccmds.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="filearchiv.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="terminalccmds.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="COMMUNICATIONS"></A>15.6. Communications Commands</H1><P>Certain of the following commands find use in <AHREF="writingscripts.html#CSPAMMERS">chasing spammers</A>, as well as in	  network data transfer and analysis.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMMUNINFO1"></A>Information and Statistics</B></P><DL><DT><ANAME="HOSTREF"></A><BCLASS="COMMAND">host</B></DT><DD><P>Searches for information about an Internet host by name or	      IP address, using DNS.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>host surfacemail.com</B></TT> <TTCLASS="COMPUTEROUTPUT">surfacemail.com. has address 202.92.42.236</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="IPCALCREF"></A><BCLASS="COMMAND">ipcalc</B></DT><DD><P>Displays IP information for a host.	      With the <TTCLASS="OPTION">-h</TT> option,	      <BCLASS="COMMAND">ipcalc</B> does a reverse DNS lookup, finding	      the name of the host (server) from the IP address.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ipcalc -h 202.92.42.236</B></TT> <TTCLASS="COMPUTEROUTPUT">HOSTNAME=surfacemail.com</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="NSLOOKUPREF"></A><BCLASS="COMMAND">nslookup</B></DT><DD><P>Do an Internet <SPANCLASS="QUOTE">"name server lookup"</SPAN>	      on a host by IP address. This is essentially equivalent	      to <BCLASS="COMMAND">ipcalc -h</B> or <BCLASS="COMMAND">dig -x	      </B>. The command may be run either interactively	      or noninteractively, i.e., from within a script.</P><P>The <BCLASS="COMMAND">nslookup</B> command has allegedly	      been <SPANCLASS="QUOTE">"deprecated,"</SPAN> but it is still useful.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>nslookup -sil 66.97.104.180</B></TT> <TTCLASS="COMPUTEROUTPUT">nslookup kuhleersparnis.ch Server:         135.116.137.2 Address:        135.116.137.2#53 Non-authoritative answer: Name:   kuhleersparnis.ch</TT> 	      </PRE></TD></TR></TABLE>	  </P></DD><DT><ANAME="DIGREF"></A><BCLASS="COMMAND">dig</B></DT><DD><P><BCLASS="COMMAND">D</B>omain <BCLASS="COMMAND">I</B>nformation	      <BCLASS="COMMAND">G</B>roper. Similar to	      <BCLASS="COMMAND">nslookup</B>, <BCLASS="COMMAND">dig</B> does	      an Internet <SPANCLASS="QUOTE">"name server lookup"</SPAN> on a host.	      May be run either interactively or noninteractively, i.e.,	      from within a script.</P><P>Some interesting options to <BCLASS="COMMAND">dig</B> are	      <TTCLASS="OPTION">+time=N</TT> for setting a query timeout to	      <TTCLASS="PARAMETER"><I>N</I></TT> seconds, <TTCLASS="OPTION">+nofail</TT> for	      continuing to query servers until a reply is received, and	      <TTCLASS="OPTION">-x</TT> for doing a reverse address lookup.</P><P>Compare the output of <BCLASS="COMMAND">dig -x</B> with	      <BCLASS="COMMAND">ipcalc -h</B> and	      <BCLASS="COMMAND">nslookup</B>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>dig -x 81.9.6.2</B></TT> <TTCLASS="COMPUTEROUTPUT">;; Got answer: ;; -&#62;&#62;HEADER&#60;&#60;- opcode: QUERY, status: NXDOMAIN, id: 11649 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0 ;; QUESTION SECTION: ;2.6.9.81.in-addr.arpa.         IN      PTR ;; AUTHORITY SECTION: 6.9.81.in-addr.arpa.    3600    IN      SOA     ns.eltel.net. noc.eltel.net. 2002031705 900 600 86400 3600 ;; Query time: 537 msec ;; SERVER: 135.116.137.2#53(135.116.137.2) ;; WHEN: Wed Jun 26 08:35:24 2002 ;; MSG SIZE  rcvd: 91</TT> 	      </PRE></TD></TR></TABLE>	  </P><P><ANAME="SPAMLOOKUP_0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="SPAMLOOKUP"></A><P><B>Example 15-39. Finding out where to report a spammer</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# spam-lookup.sh: Look up abuse contact to report a spammer.   3&nbsp;# Thanks, Michael Zick.   4&nbsp;   5&nbsp;# Check for command-line arg.   6&nbsp;ARGCOUNT=1   7&nbsp;E_WRONGARGS=65   8&nbsp;if [ $# -ne "$ARGCOUNT" ]   9&nbsp;then  10&nbsp;  echo "Usage: `basename $0` domain-name"  11&nbsp;  exit $E_WRONGARGS  12&nbsp;fi  13&nbsp;  14&nbsp;  15&nbsp;dig +short $1.contacts.abuse.net -c in -t txt  16&nbsp;# Also try:  17&nbsp;#     dig +nssearch $1  18&nbsp;#     Tries to find "authoritative name servers" and display SOA records.  19&nbsp;  20&nbsp;# The following also works:  21&nbsp;#     whois -h whois.abuse.net $1  22&nbsp;#           ^^ ^^^^^^^^^^^^^^^  Specify host.    23&nbsp;#     Can even lookup multiple spammers with this, i.e."  24&nbsp;#     whois -h whois.abuse.net $spamdomain1 $spamdomain2 . . .  25&nbsp;  26&nbsp;  27&nbsp;#  Exercise:  28&nbsp;#  --------  29&nbsp;#  Expand the functionality of this script  30&nbsp;#+ so that it automatically e-mails a notification  31&nbsp;#+ to the responsible ISP's contact address(es).  32&nbsp;#  Hint: use the "mail" command.  33&nbsp;  34&nbsp;exit $?  35&nbsp;  36&nbsp;# spam-lookup.sh chinatietong.com  37&nbsp;#                A known spam domain.  38&nbsp;  39&nbsp;# "crnet_mgr@chinatietong.com"  40&nbsp;# "crnet_tec@chinatietong.com"  41&nbsp;# "postmaster@chinatietong.com"  42&nbsp;  43&nbsp;  44&nbsp;#  For a more elaborate version of this script,  45&nbsp;#+ see the SpamViz home page, http://www.spamviz.net/index.html.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="ISSPAMMER_0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="ISSPAMMER"></A><P><B>Example 15-40. Analyzing a spam domain</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#! /bin/bash   2&nbsp;# is-spammer.sh: Identifying spam domains   3&nbsp;   4&nbsp;# $Id: is-spammer, v 1.4 2004/09/01 19:37:52 mszick Exp $   5&nbsp;# Above line is RCS ID info.   6&nbsp;#   7&nbsp;#  This is a simplified version of the "is_spammer.bash   8&nbsp;#+ script in the Contributed Scripts appendix.   9&nbsp;  10&nbsp;# is-spammer &#60;domain.name&#62;  11&nbsp;  12&nbsp;# Uses an external program: 'dig'  13&nbsp;# Tested with version: 9.2.4rc5  14&nbsp;  15&nbsp;# Uses functions.  16&nbsp;# Uses IFS to parse strings by assignment into arrays.  17&nbsp;# And even does something useful: checks e-mail blacklists.  18&nbsp;  19&nbsp;# Use the domain.name(s) from the text body:  20&nbsp;# http://www.good_stuff.spammer.biz/just_ignore_everything_else  21&nbsp;#                       ^^^^^^^^^^^  22&nbsp;# Or the domain.name(s) from any e-mail address:  23&nbsp;# Really_Good_Offer@spammer.biz  24&nbsp;#  25&nbsp;# as the only argument to this script.  26&nbsp;#(PS: have your Inet connection running)  27&nbsp;#  28&nbsp;# So, to invoke this script in the above two instances:  29&nbsp;#       is-spammer.sh spammer.biz  30&nbsp;  31&nbsp;  32&nbsp;# Whitespace == :Space:Tab:Line Feed:Carriage Return:  33&nbsp;WSP_IFS=$'\x20'$'\x09'$'\x0A'$'\x0D'  34&nbsp;  35&nbsp;# No Whitespace == Line Feed:Carriage Return  36&nbsp;No_WSP=$'\x0A'$'\x0D'  37&nbsp;  38&nbsp;# Field separator for dotted decimal ip addresses  39&nbsp;ADR_IFS=${No_WSP}'.'  40&nbsp;  41&nbsp;# Get the dns text resource record.  42&nbsp;# get_txt &#60;error_code&#62; &#60;list_query&#62;  43&nbsp;get_txt() {  44&nbsp;  45&nbsp;    # Parse $1 by assignment at the dots.  46&nbsp;    local -a dns  47&nbsp;    IFS=$ADR_IFS  48&nbsp;    dns=( $1 )  49&nbsp;    IFS=$WSP_IFS  50&nbsp;    if [ "${dns[0]}" == '127' ]  51&nbsp;    then  52&nbsp;        # See if there is a reason.  53&nbsp;        echo $(dig +short $2 -t txt)  54&nbsp;    fi  55&nbsp;}  56&nbsp;  57&nbsp;# Get the dns address resource record.  58&nbsp;# chk_adr &#60;rev_dns&#62; &#60;list_server&#62;  59&nbsp;chk_adr() {  60&nbsp;    local reply  61&nbsp;    local server  62&nbsp;    local reason  63&nbsp;  64&nbsp;    server=${1}${2}  65&nbsp;    reply=$( dig +short ${server} )  66&nbsp;  67&nbsp;    # If reply might be an error code . . .  68&nbsp;    if [ ${#reply} -gt 6 ]  69&nbsp;    then  70&nbsp;        reason=$(get_txt ${reply} ${server} )  71&nbsp;        reason=${reason:-${reply}}  72&nbsp;    fi  73&nbsp;    echo ${reason:-' not blacklisted.'}  74&nbsp;}  75&nbsp;  76&nbsp;# Need to get the IP address from the name.  77&nbsp;echo 'Get address of: '$1  78&nbsp;ip_adr=$(dig +short $1)  79&nbsp;dns_reply=${ip_adr:-' no answer '}  80&nbsp;echo ' Found address: '${dns_reply}  81&nbsp;  82&nbsp;# A valid reply is at least 4 digits plus 3 dots.  83&nbsp;if [ ${#ip_adr} -gt 6 ]  84&nbsp;then  85&nbsp;    echo  86&nbsp;    declare query  87&nbsp;  88&nbsp;    # Parse by assignment at the dots.  89&nbsp;    declare -a dns  90&nbsp;    IFS=$ADR_IFS  91&nbsp;    dns=( ${ip_adr} )  92&nbsp;    IFS=$WSP_IFS  93&nbsp;  94&nbsp;    # Reorder octets into dns query order.  95&nbsp;    rev_dns="${dns[3]}"'.'"${dns[2]}"'.'"${dns[1]}"'.'"${dns[0]}"'.'  96&nbsp;  97&nbsp;# See: http://www.spamhaus.org (Conservative, well maintained)  98&nbsp;    echo -n 'spamhaus.org says: '  99&nbsp;    echo $(chk_adr ${rev_dns} 'sbl-xbl.spamhaus.org') 100&nbsp; 101&nbsp;# See: http://ordb.org (Open mail relays) 102&nbsp;    echo -n '   ordb.org  says: ' 103&nbsp;    echo $(chk_adr ${rev_dns} 'relays.ordb.org') 104&nbsp; 105&nbsp;# See: http://www.spamcop.net/ (You can report spammers here) 106&nbsp;    echo -n ' spamcop.net says: ' 107&nbsp;    echo $(chk_adr ${rev_dns} 'bl.spamcop.net') 108&nbsp; 109&nbsp;# # # other blacklist operations # # # 110&nbsp; 111&nbsp;# See: http://cbl.abuseat.org. 112&nbsp;    echo -n ' abuseat.org says: ' 113&nbsp;    echo $(chk_adr ${rev_dns} 'cbl.abuseat.org') 114&nbsp; 115&nbsp;# See: http://dsbl.org/usage (Various mail relays) 116&nbsp;    echo 117&nbsp;    echo 'Distributed Server Listings' 118&nbsp;    echo -n '       list.dsbl.org says: ' 119&nbsp;    echo $(chk_adr ${rev_dns} 'list.dsbl.org') 120&nbsp; 121&nbsp;    echo -n '   multihop.dsbl.org says: ' 122&nbsp;    echo $(chk_adr ${rev_dns} 'multihop.dsbl.org') 123&nbsp; 124&nbsp;    echo -n 'unconfirmed.dsbl.org says: ' 125&nbsp;    echo $(chk_adr ${rev_dns} 'unconfirmed.dsbl.org') 126&nbsp; 127&nbsp;else 128&nbsp;    echo 129&nbsp;    echo 'Could not use that address.' 130&nbsp;fi 131&nbsp; 132&nbsp;exit 0 133&nbsp; 134&nbsp;# Exercises: 135&nbsp;# -------- 136&nbsp; 137&nbsp;# 1) Check arguments to script, 138&nbsp;#    and exit with appropriate error message if necessary. 139&nbsp; 140&nbsp;# 2) Check if on-line at invocation of script, 141&nbsp;#    and exit with appropriate error message if necessary. 142&nbsp; 143&nbsp;# 3) Substitute generic variables for "hard-coded" BHL domains. 144&nbsp; 145&nbsp;# 4) Set a time-out for the script using the "+time=" option 146&nbsp;     to the 'dig' command.</PRE></TD></TR></TABLE><HR></DIV><P>For a much more elaborate version of the above script, see 	    <AHREF="contributed-scripts.html#ISSPAMMER2">Example A-30</A>.</P></DD><DT><ANAME="TRACEROUTEREF"></A><BCLASS="COMMAND">traceroute</B></DT><DD><P>Trace the route taken by packets sent to a remote host. This	      command works within a LAN, WAN, or over the	      Internet. The remote host may be specified by an IP	      address. The output of this command may be filtered	      by <AHREF="textproc.html#GREPREF">grep</A> or <AHREF="sedawk.html#SEDREF">sed</A> in a pipe.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>traceroute 81.9.6.2</B></TT> <TTCLASS="COMPUTEROUTPUT">traceroute to 81.9.6.2 (81.9.6.2), 30 hops max, 38 byte packets 1  tc43.xjbnnbrb.com (136.30.178.8)  191.303 ms  179.400 ms  179.767 ms 2  or0.xjbnnbrb.com (136.30.178.1)  179.536 ms  179.534 ms  169.685 ms 3  192.168.11.101 (192.168.11.101)  189.471 ms  189.556 ms * ...</TT> 	      </PRE></TD></TR></TABLE>	  </P></DD><DT><ANAME="PINGREF"></A><BCLASS="COMMAND">ping</B></DT><DD><P>Broadcast an <SPANCLASS="QUOTE">"ICMP ECHO_REQUEST"</SPAN> packet to	      another machine, either on a local or remote network. This	      is a diagnostic tool for testing network connections,	      and it should be used with caution.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ping localhost</B></TT> <TTCLASS="COMPUTEROUTPUT">PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data. 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=709 usec 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=286 usec --- localhost.localdomain ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max/mdev = 0.286/0.497/0.709/0.212 ms</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>A successful <BCLASS="COMMAND">ping</B> returns an <AHREF="exit-status.html#EXITSTATUSREF">exit status</A

⌨️ 快捷键说明

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