📄 communications.html
字号:
<!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: ;; ->>HEADER<<- 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 #!/bin/bash 2 # spam-lookup.sh: Look up abuse contact to report a spammer. 3 # Thanks, Michael Zick. 4 5 # Check for command-line arg. 6 ARGCOUNT=1 7 E_WRONGARGS=65 8 if [ $# -ne "$ARGCOUNT" ] 9 then 10 echo "Usage: `basename $0` domain-name" 11 exit $E_WRONGARGS 12 fi 13 14 15 dig +short $1.contacts.abuse.net -c in -t txt 16 # Also try: 17 # dig +nssearch $1 18 # Tries to find "authoritative name servers" and display SOA records. 19 20 # The following also works: 21 # whois -h whois.abuse.net $1 22 # ^^ ^^^^^^^^^^^^^^^ Specify host. 23 # Can even lookup multiple spammers with this, i.e." 24 # whois -h whois.abuse.net $spamdomain1 $spamdomain2 . . . 25 26 27 # Exercise: 28 # -------- 29 # Expand the functionality of this script 30 #+ so that it automatically e-mails a notification 31 #+ to the responsible ISP's contact address(es). 32 # Hint: use the "mail" command. 33 34 exit $? 35 36 # spam-lookup.sh chinatietong.com 37 # A known spam domain. 38 39 # "crnet_mgr@chinatietong.com" 40 # "crnet_tec@chinatietong.com" 41 # "postmaster@chinatietong.com" 42 43 44 # For a more elaborate version of this script, 45 #+ 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 #! /bin/bash 2 # is-spammer.sh: Identifying spam domains 3 4 # $Id: is-spammer, v 1.4 2004/09/01 19:37:52 mszick Exp $ 5 # Above line is RCS ID info. 6 # 7 # This is a simplified version of the "is_spammer.bash 8 #+ script in the Contributed Scripts appendix. 9 10 # is-spammer <domain.name> 11 12 # Uses an external program: 'dig' 13 # Tested with version: 9.2.4rc5 14 15 # Uses functions. 16 # Uses IFS to parse strings by assignment into arrays. 17 # And even does something useful: checks e-mail blacklists. 18 19 # Use the domain.name(s) from the text body: 20 # http://www.good_stuff.spammer.biz/just_ignore_everything_else 21 # ^^^^^^^^^^^ 22 # Or the domain.name(s) from any e-mail address: 23 # Really_Good_Offer@spammer.biz 24 # 25 # as the only argument to this script. 26 #(PS: have your Inet connection running) 27 # 28 # So, to invoke this script in the above two instances: 29 # is-spammer.sh spammer.biz 30 31 32 # Whitespace == :Space:Tab:Line Feed:Carriage Return: 33 WSP_IFS=$'\x20'$'\x09'$'\x0A'$'\x0D' 34 35 # No Whitespace == Line Feed:Carriage Return 36 No_WSP=$'\x0A'$'\x0D' 37 38 # Field separator for dotted decimal ip addresses 39 ADR_IFS=${No_WSP}'.' 40 41 # Get the dns text resource record. 42 # get_txt <error_code> <list_query> 43 get_txt() { 44 45 # Parse $1 by assignment at the dots. 46 local -a dns 47 IFS=$ADR_IFS 48 dns=( $1 ) 49 IFS=$WSP_IFS 50 if [ "${dns[0]}" == '127' ] 51 then 52 # See if there is a reason. 53 echo $(dig +short $2 -t txt) 54 fi 55 } 56 57 # Get the dns address resource record. 58 # chk_adr <rev_dns> <list_server> 59 chk_adr() { 60 local reply 61 local server 62 local reason 63 64 server=${1}${2} 65 reply=$( dig +short ${server} ) 66 67 # If reply might be an error code . . . 68 if [ ${#reply} -gt 6 ] 69 then 70 reason=$(get_txt ${reply} ${server} ) 71 reason=${reason:-${reply}} 72 fi 73 echo ${reason:-' not blacklisted.'} 74 } 75 76 # Need to get the IP address from the name. 77 echo 'Get address of: '$1 78 ip_adr=$(dig +short $1) 79 dns_reply=${ip_adr:-' no answer '} 80 echo ' Found address: '${dns_reply} 81 82 # A valid reply is at least 4 digits plus 3 dots. 83 if [ ${#ip_adr} -gt 6 ] 84 then 85 echo 86 declare query 87 88 # Parse by assignment at the dots. 89 declare -a dns 90 IFS=$ADR_IFS 91 dns=( ${ip_adr} ) 92 IFS=$WSP_IFS 93 94 # Reorder octets into dns query order. 95 rev_dns="${dns[3]}"'.'"${dns[2]}"'.'"${dns[1]}"'.'"${dns[0]}"'.' 96 97 # See: http://www.spamhaus.org (Conservative, well maintained) 98 echo -n 'spamhaus.org says: ' 99 echo $(chk_adr ${rev_dns} 'sbl-xbl.spamhaus.org') 100 101 # See: http://ordb.org (Open mail relays) 102 echo -n ' ordb.org says: ' 103 echo $(chk_adr ${rev_dns} 'relays.ordb.org') 104 105 # See: http://www.spamcop.net/ (You can report spammers here) 106 echo -n ' spamcop.net says: ' 107 echo $(chk_adr ${rev_dns} 'bl.spamcop.net') 108 109 # # # other blacklist operations # # # 110 111 # See: http://cbl.abuseat.org. 112 echo -n ' abuseat.org says: ' 113 echo $(chk_adr ${rev_dns} 'cbl.abuseat.org') 114 115 # See: http://dsbl.org/usage (Various mail relays) 116 echo 117 echo 'Distributed Server Listings' 118 echo -n ' list.dsbl.org says: ' 119 echo $(chk_adr ${rev_dns} 'list.dsbl.org') 120 121 echo -n ' multihop.dsbl.org says: ' 122 echo $(chk_adr ${rev_dns} 'multihop.dsbl.org') 123 124 echo -n 'unconfirmed.dsbl.org says: ' 125 echo $(chk_adr ${rev_dns} 'unconfirmed.dsbl.org') 126 127 else 128 echo 129 echo 'Could not use that address.' 130 fi 131 132 exit 0 133 134 # Exercises: 135 # -------- 136 137 # 1) Check arguments to script, 138 # and exit with appropriate error message if necessary. 139 140 # 2) Check if on-line at invocation of script, 141 # and exit with appropriate error message if necessary. 142 143 # 3) Substitute generic variables for "hard-coded" BHL domains. 144 145 # 4) Set a time-out for the script using the "+time=" option 146 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 + -