| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Communications Commands</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="External Filters, Programs and Commands" |
| 15 | HREF="external.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="File and Archiving Commands" |
| 18 | HREF="filearchiv.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Terminal Control Commands" |
| 21 | HREF="terminalccmds.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="SECT1" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="filearchiv.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | >Chapter 16. External Filters, Programs and Commands</TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="terminalccmds.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="SECT1" |
| 92 | ><H1 |
| 93 | CLASS="SECT1" |
| 94 | ><A |
| 95 | NAME="COMMUNICATIONS" |
| 96 | ></A |
| 97 | >16.6. Communications Commands</H1 |
| 98 | ><P |
| 99 | >Certain of the following commands find use in |
| 100 | network data transfer and analysis, as well as in |
| 101 | <A |
| 102 | HREF="writingscripts.html#CSPAMMERS" |
| 103 | >chasing spammers</A |
| 104 | >.</P |
| 105 | ><DIV |
| 106 | CLASS="VARIABLELIST" |
| 107 | ><P |
| 108 | ><B |
| 109 | ><A |
| 110 | NAME="COMMUNINFO1" |
| 111 | ></A |
| 112 | >Information and Statistics</B |
| 113 | ></P |
| 114 | ><DL |
| 115 | ><DT |
| 116 | ><A |
| 117 | NAME="HOSTREF" |
| 118 | ></A |
| 119 | ><B |
| 120 | CLASS="COMMAND" |
| 121 | >host</B |
| 122 | ></DT |
| 123 | ><DD |
| 124 | ><P |
| 125 | >Searches for information about an Internet host by name or |
| 126 | IP address, using DNS.</P |
| 127 | ><P |
| 128 | > <TABLE |
| 129 | BORDER="0" |
| 130 | BGCOLOR="#E0E0E0" |
| 131 | WIDTH="90%" |
| 132 | ><TR |
| 133 | ><TD |
| 134 | ><PRE |
| 135 | CLASS="SCREEN" |
| 136 | > <TT |
| 137 | CLASS="PROMPT" |
| 138 | >bash$ </TT |
| 139 | ><TT |
| 140 | CLASS="USERINPUT" |
| 141 | ><B |
| 142 | >host surfacemail.com</B |
| 143 | ></TT |
| 144 | > |
| 145 | <TT |
| 146 | CLASS="COMPUTEROUTPUT" |
| 147 | >surfacemail.com. has address 202.92.42.236</TT |
| 148 | > |
| 149 | </PRE |
| 150 | ></TD |
| 151 | ></TR |
| 152 | ></TABLE |
| 153 | > |
| 154 | </P |
| 155 | ></DD |
| 156 | ><DT |
| 157 | ><A |
| 158 | NAME="IPCALCREF" |
| 159 | ></A |
| 160 | ><B |
| 161 | CLASS="COMMAND" |
| 162 | >ipcalc</B |
| 163 | ></DT |
| 164 | ><DD |
| 165 | ><P |
| 166 | >Displays IP information for a host. |
| 167 | With the <TT |
| 168 | CLASS="OPTION" |
| 169 | >-h</TT |
| 170 | > option, |
| 171 | <B |
| 172 | CLASS="COMMAND" |
| 173 | >ipcalc</B |
| 174 | > does a reverse DNS lookup, finding |
| 175 | the name of the host (server) from the IP address.</P |
| 176 | ><P |
| 177 | > <TABLE |
| 178 | BORDER="0" |
| 179 | BGCOLOR="#E0E0E0" |
| 180 | WIDTH="90%" |
| 181 | ><TR |
| 182 | ><TD |
| 183 | ><PRE |
| 184 | CLASS="SCREEN" |
| 185 | > <TT |
| 186 | CLASS="PROMPT" |
| 187 | >bash$ </TT |
| 188 | ><TT |
| 189 | CLASS="USERINPUT" |
| 190 | ><B |
| 191 | >ipcalc -h 202.92.42.236</B |
| 192 | ></TT |
| 193 | > |
| 194 | <TT |
| 195 | CLASS="COMPUTEROUTPUT" |
| 196 | >HOSTNAME=surfacemail.com</TT |
| 197 | > |
| 198 | </PRE |
| 199 | ></TD |
| 200 | ></TR |
| 201 | ></TABLE |
| 202 | > |
| 203 | </P |
| 204 | ></DD |
| 205 | ><DT |
| 206 | ><A |
| 207 | NAME="NSLOOKUPREF" |
| 208 | ></A |
| 209 | ><B |
| 210 | CLASS="COMMAND" |
| 211 | >nslookup</B |
| 212 | ></DT |
| 213 | ><DD |
| 214 | ><P |
| 215 | >Do an Internet <SPAN |
| 216 | CLASS="QUOTE" |
| 217 | >"name server lookup"</SPAN |
| 218 | > |
| 219 | on a host by IP address. This is essentially equivalent |
| 220 | to <B |
| 221 | CLASS="COMMAND" |
| 222 | >ipcalc -h</B |
| 223 | > or <B |
| 224 | CLASS="COMMAND" |
| 225 | >dig -x |
| 226 | </B |
| 227 | >. The command may be run either interactively |
| 228 | or noninteractively, i.e., from within a script.</P |
| 229 | ><P |
| 230 | >The <B |
| 231 | CLASS="COMMAND" |
| 232 | >nslookup</B |
| 233 | > command has allegedly |
| 234 | been <SPAN |
| 235 | CLASS="QUOTE" |
| 236 | >"deprecated,"</SPAN |
| 237 | > but it is still useful.</P |
| 238 | ><P |
| 239 | > <TABLE |
| 240 | BORDER="0" |
| 241 | BGCOLOR="#E0E0E0" |
| 242 | WIDTH="90%" |
| 243 | ><TR |
| 244 | ><TD |
| 245 | ><PRE |
| 246 | CLASS="SCREEN" |
| 247 | > <TT |
| 248 | CLASS="PROMPT" |
| 249 | >bash$ </TT |
| 250 | ><TT |
| 251 | CLASS="USERINPUT" |
| 252 | ><B |
| 253 | >nslookup -sil 66.97.104.180</B |
| 254 | ></TT |
| 255 | > |
| 256 | <TT |
| 257 | CLASS="COMPUTEROUTPUT" |
| 258 | >nslookup kuhleersparnis.ch |
| 259 | Server: 135.116.137.2 |
| 260 | Address: 135.116.137.2#53 |
| 261 | |
| 262 | Non-authoritative answer: |
| 263 | Name: kuhleersparnis.ch</TT |
| 264 | > |
| 265 | </PRE |
| 266 | ></TD |
| 267 | ></TR |
| 268 | ></TABLE |
| 269 | > |
| 270 | </P |
| 271 | ></DD |
| 272 | ><DT |
| 273 | ><A |
| 274 | NAME="DIGREF" |
| 275 | ></A |
| 276 | ><B |
| 277 | CLASS="COMMAND" |
| 278 | >dig</B |
| 279 | ></DT |
| 280 | ><DD |
| 281 | ><P |
| 282 | ><B |
| 283 | CLASS="COMMAND" |
| 284 | >D</B |
| 285 | >omain <B |
| 286 | CLASS="COMMAND" |
| 287 | >I</B |
| 288 | >nformation |
| 289 | <B |
| 290 | CLASS="COMMAND" |
| 291 | >G</B |
| 292 | >roper. Similar to |
| 293 | <B |
| 294 | CLASS="COMMAND" |
| 295 | >nslookup</B |
| 296 | >, <I |
| 297 | CLASS="FIRSTTERM" |
| 298 | >dig</I |
| 299 | > does |
| 300 | an Internet <I |
| 301 | CLASS="FIRSTTERM" |
| 302 | >name server lookup</I |
| 303 | > on a host. |
| 304 | May be run from the command-line or from within a script.</P |
| 305 | ><P |
| 306 | >Some interesting options to <I |
| 307 | CLASS="FIRSTTERM" |
| 308 | >dig</I |
| 309 | > are |
| 310 | <TT |
| 311 | CLASS="OPTION" |
| 312 | >+time=N</TT |
| 313 | > for setting a query timeout to |
| 314 | <TT |
| 315 | CLASS="PARAMETER" |
| 316 | ><I |
| 317 | >N</I |
| 318 | ></TT |
| 319 | > seconds, <TT |
| 320 | CLASS="OPTION" |
| 321 | >+nofail</TT |
| 322 | > for |
| 323 | continuing to query servers until a reply is received, and |
| 324 | <TT |
| 325 | CLASS="OPTION" |
| 326 | >-x</TT |
| 327 | > for doing a reverse address lookup.</P |
| 328 | ><P |
| 329 | >Compare the output of <B |
| 330 | CLASS="COMMAND" |
| 331 | >dig -x</B |
| 332 | > with |
| 333 | <B |
| 334 | CLASS="COMMAND" |
| 335 | >ipcalc -h</B |
| 336 | > and |
| 337 | <B |
| 338 | CLASS="COMMAND" |
| 339 | >nslookup</B |
| 340 | >.</P |
| 341 | ><P |
| 342 | > <TABLE |
| 343 | BORDER="0" |
| 344 | BGCOLOR="#E0E0E0" |
| 345 | WIDTH="90%" |
| 346 | ><TR |
| 347 | ><TD |
| 348 | ><PRE |
| 349 | CLASS="SCREEN" |
| 350 | > <TT |
| 351 | CLASS="PROMPT" |
| 352 | >bash$ </TT |
| 353 | ><TT |
| 354 | CLASS="USERINPUT" |
| 355 | ><B |
| 356 | >dig -x 81.9.6.2</B |
| 357 | ></TT |
| 358 | > |
| 359 | <TT |
| 360 | CLASS="COMPUTEROUTPUT" |
| 361 | >;; Got answer: |
| 362 | ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 11649 |
| 363 | ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0 |
| 364 | |
| 365 | ;; QUESTION SECTION: |
| 366 | ;2.6.9.81.in-addr.arpa. IN PTR |
| 367 | |
| 368 | ;; AUTHORITY SECTION: |
| 369 | 6.9.81.in-addr.arpa. 3600 IN SOA ns.eltel.net. noc.eltel.net. |
| 370 | 2002031705 900 600 86400 3600 |
| 371 | |
| 372 | ;; Query time: 537 msec |
| 373 | ;; SERVER: 135.116.137.2#53(135.116.137.2) |
| 374 | ;; WHEN: Wed Jun 26 08:35:24 2002 |
| 375 | ;; MSG SIZE rcvd: 91</TT |
| 376 | > |
| 377 | </PRE |
| 378 | ></TD |
| 379 | ></TR |
| 380 | ></TABLE |
| 381 | > |
| 382 | </P |
| 383 | ><P |
| 384 | ><A |
| 385 | NAME="SPAMLOOKUP_0" |
| 386 | ></A |
| 387 | ></P |
| 388 | ><DIV |
| 389 | CLASS="EXAMPLE" |
| 390 | ><HR><A |
| 391 | NAME="SPAMLOOKUP" |
| 392 | ></A |
| 393 | ><P |
| 394 | ><B |
| 395 | >Example 16-40. Finding out where to report a spammer</B |
| 396 | ></P |
| 397 | ><TABLE |
| 398 | BORDER="0" |
| 399 | BGCOLOR="#E0E0E0" |
| 400 | WIDTH="90%" |
| 401 | ><TR |
| 402 | ><TD |
| 403 | ><PRE |
| 404 | CLASS="PROGRAMLISTING" |
| 405 | > 1 #!/bin/bash |
| 406 | 2 # spam-lookup.sh: Look up abuse contact to report a spammer. |
| 407 | 3 # Thanks, Michael Zick. |
| 408 | 4 |
| 409 | 5 # Check for command-line arg. |
| 410 | 6 ARGCOUNT=1 |
| 411 | 7 E_WRONGARGS=85 |
| 412 | 8 if [ $# -ne "$ARGCOUNT" ] |
| 413 | 9 then |
| 414 | 10 echo "Usage: `basename $0` domain-name" |
| 415 | 11 exit $E_WRONGARGS |
| 416 | 12 fi |
| 417 | 13 |
| 418 | 14 |
| 419 | 15 dig +short $1.contacts.abuse.net -c in -t txt |
| 420 | 16 # Also try: |
| 421 | 17 # dig +nssearch $1 |
| 422 | 18 # Tries to find "authoritative name servers" and display SOA records. |
| 423 | 19 |
| 424 | 20 # The following also works: |
| 425 | 21 # whois -h whois.abuse.net $1 |
| 426 | 22 # ^^ ^^^^^^^^^^^^^^^ Specify host. |
| 427 | 23 # Can even lookup multiple spammers with this, i.e." |
| 428 | 24 # whois -h whois.abuse.net $spamdomain1 $spamdomain2 . . . |
| 429 | 25 |
| 430 | 26 |
| 431 | 27 # Exercise: |
| 432 | 28 # -------- |
| 433 | 29 # Expand the functionality of this script |
| 434 | 30 #+ so that it automatically e-mails a notification |
| 435 | 31 #+ to the responsible ISP's contact address(es). |
| 436 | 32 # Hint: use the "mail" command. |
| 437 | 33 |
| 438 | 34 exit $? |
| 439 | 35 |
| 440 | 36 # spam-lookup.sh chinatietong.com |
| 441 | 37 # A known spam domain. |
| 442 | 38 |
| 443 | 39 # "[email protected]" |
| 444 | 40 # "[email protected]" |
| 445 | 41 # "[email protected]" |
| 446 | 42 |
| 447 | 43 |
| 448 | 44 # For a more elaborate version of this script, |
| 449 | 45 #+ see the SpamViz home page, http://www.spamviz.net/index.html.</PRE |
| 450 | ></TD |
| 451 | ></TR |
| 452 | ></TABLE |
| 453 | ><HR></DIV |
| 454 | ><P |
| 455 | ><A |
| 456 | NAME="ISSPAMMER_0" |
| 457 | ></A |
| 458 | ></P |
| 459 | ><DIV |
| 460 | CLASS="EXAMPLE" |
| 461 | ><HR><A |
| 462 | NAME="ISSPAMMER" |
| 463 | ></A |
| 464 | ><P |
| 465 | ><B |
| 466 | >Example 16-41. Analyzing a spam domain</B |
| 467 | ></P |
| 468 | ><TABLE |
| 469 | BORDER="0" |
| 470 | BGCOLOR="#E0E0E0" |
| 471 | WIDTH="90%" |
| 472 | ><TR |
| 473 | ><TD |
| 474 | ><PRE |
| 475 | CLASS="PROGRAMLISTING" |
| 476 | > 1 #! /bin/bash |
| 477 | 2 # is-spammer.sh: Identifying spam domains |
| 478 | 3 |
| 479 | 4 # $Id: is-spammer, v 1.4 2004/09/01 19:37:52 mszick Exp $ |
| 480 | 5 # Above line is RCS ID info. |
| 481 | 6 # |
| 482 | 7 # This is a simplified version of the "is_spammer.bash |
| 483 | 8 #+ script in the Contributed Scripts appendix. |
| 484 | 9 |
| 485 | 10 # is-spammer <domain.name> |
| 486 | 11 |
| 487 | 12 # Uses an external program: 'dig' |
| 488 | 13 # Tested with version: 9.2.4rc5 |
| 489 | 14 |
| 490 | 15 # Uses functions. |
| 491 | 16 # Uses IFS to parse strings by assignment into arrays. |
| 492 | 17 # And even does something useful: checks e-mail blacklists. |
| 493 | 18 |
| 494 | 19 # Use the domain.name(s) from the text body: |
| 495 | 20 # http://www.good_stuff.spammer.biz/just_ignore_everything_else |
| 496 | 21 # ^^^^^^^^^^^ |
| 497 | 22 # Or the domain.name(s) from any e-mail address: |
| 498 | 23 # [email protected] |
| 499 | 24 # |
| 500 | 25 # as the only argument to this script. |
| 501 | 26 #(PS: have your Inet connection running) |
| 502 | 27 # |
| 503 | 28 # So, to invoke this script in the above two instances: |
| 504 | 29 # is-spammer.sh spammer.biz |
| 505 | 30 |
| 506 | 31 |
| 507 | 32 # Whitespace == :Space:Tab:Line Feed:Carriage Return: |
| 508 | 33 WSP_IFS=$'\x20'$'\x09'$'\x0A'$'\x0D' |
| 509 | 34 |
| 510 | 35 # No Whitespace == Line Feed:Carriage Return |
| 511 | 36 No_WSP=$'\x0A'$'\x0D' |
| 512 | 37 |
| 513 | 38 # Field separator for dotted decimal ip addresses |
| 514 | 39 ADR_IFS=${No_WSP}'.' |
| 515 | 40 |
| 516 | 41 # Get the dns text resource record. |
| 517 | 42 # get_txt <error_code> <list_query> |
| 518 | 43 get_txt() { |
| 519 | 44 |
| 520 | 45 # Parse $1 by assignment at the dots. |
| 521 | 46 local -a dns |
| 522 | 47 IFS=$ADR_IFS |
| 523 | 48 dns=( $1 ) |
| 524 | 49 IFS=$WSP_IFS |
| 525 | 50 if [ "${dns[0]}" == '127' ] |
| 526 | 51 then |
| 527 | 52 # See if there is a reason. |
| 528 | 53 echo $(dig +short $2 -t txt) |
| 529 | 54 fi |
| 530 | 55 } |
| 531 | 56 |
| 532 | 57 # Get the dns address resource record. |
| 533 | 58 # chk_adr <rev_dns> <list_server> |
| 534 | 59 chk_adr() { |
| 535 | 60 local reply |
| 536 | 61 local server |
| 537 | 62 local reason |
| 538 | 63 |
| 539 | 64 server=${1}${2} |
| 540 | 65 reply=$( dig +short ${server} ) |
| 541 | 66 |
| 542 | 67 # If reply might be an error code . . . |
| 543 | 68 if [ ${#reply} -gt 6 ] |
| 544 | 69 then |
| 545 | 70 reason=$(get_txt ${reply} ${server} ) |
| 546 | 71 reason=${reason:-${reply}} |
| 547 | 72 fi |
| 548 | 73 echo ${reason:-' not blacklisted.'} |
| 549 | 74 } |
| 550 | 75 |
| 551 | 76 # Need to get the IP address from the name. |
| 552 | 77 echo 'Get address of: '$1 |
| 553 | 78 ip_adr=$(dig +short $1) |
| 554 | 79 dns_reply=${ip_adr:-' no answer '} |
| 555 | 80 echo ' Found address: '${dns_reply} |
| 556 | 81 |
| 557 | 82 # A valid reply is at least 4 digits plus 3 dots. |
| 558 | 83 if [ ${#ip_adr} -gt 6 ] |
| 559 | 84 then |
| 560 | 85 echo |
| 561 | 86 declare query |
| 562 | 87 |
| 563 | 88 # Parse by assignment at the dots. |
| 564 | 89 declare -a dns |
| 565 | 90 IFS=$ADR_IFS |
| 566 | 91 dns=( ${ip_adr} ) |
| 567 | 92 IFS=$WSP_IFS |
| 568 | 93 |
| 569 | 94 # Reorder octets into dns query order. |
| 570 | 95 rev_dns="${dns[3]}"'.'"${dns[2]}"'.'"${dns[1]}"'.'"${dns[0]}"'.' |
| 571 | 96 |
| 572 | 97 # See: http://www.spamhaus.org (Conservative, well maintained) |
| 573 | 98 echo -n 'spamhaus.org says: ' |
| 574 | 99 echo $(chk_adr ${rev_dns} 'sbl-xbl.spamhaus.org') |
| 575 | 100 |
| 576 | 101 # See: http://ordb.org (Open mail relays) |
| 577 | 102 echo -n ' ordb.org says: ' |
| 578 | 103 echo $(chk_adr ${rev_dns} 'relays.ordb.org') |
| 579 | 104 |
| 580 | 105 # See: http://www.spamcop.net/ (You can report spammers here) |
| 581 | 106 echo -n ' spamcop.net says: ' |
| 582 | 107 echo $(chk_adr ${rev_dns} 'bl.spamcop.net') |
| 583 | 108 |
| 584 | 109 # # # other blacklist operations # # # |
| 585 | 110 |
| 586 | 111 # See: http://cbl.abuseat.org. |
| 587 | 112 echo -n ' abuseat.org says: ' |
| 588 | 113 echo $(chk_adr ${rev_dns} 'cbl.abuseat.org') |
| 589 | 114 |
| 590 | 115 # See: http://dsbl.org/usage (Various mail relays) |
| 591 | 116 echo |
| 592 | 117 echo 'Distributed Server Listings' |
| 593 | 118 echo -n ' list.dsbl.org says: ' |
| 594 | 119 echo $(chk_adr ${rev_dns} 'list.dsbl.org') |
| 595 | 120 |
| 596 | 121 echo -n ' multihop.dsbl.org says: ' |
| 597 | 122 echo $(chk_adr ${rev_dns} 'multihop.dsbl.org') |
| 598 | 123 |
| 599 | 124 echo -n 'unconfirmed.dsbl.org says: ' |
| 600 | 125 echo $(chk_adr ${rev_dns} 'unconfirmed.dsbl.org') |
| 601 | 126 |
| 602 | 127 else |
| 603 | 128 echo |
| 604 | 129 echo 'Could not use that address.' |
| 605 | 130 fi |
| 606 | 131 |
| 607 | 132 exit 0 |
| 608 | 133 |
| 609 | 134 # Exercises: |
| 610 | 135 # -------- |
| 611 | 136 |
| 612 | 137 # 1) Check arguments to script, |
| 613 | 138 # and exit with appropriate error message if necessary. |
| 614 | 139 |
| 615 | 140 # 2) Check if on-line at invocation of script, |
| 616 | 141 # and exit with appropriate error message if necessary. |
| 617 | 142 |
| 618 | 143 # 3) Substitute generic variables for "hard-coded" BHL domains. |
| 619 | 144 |
| 620 | 145 # 4) Set a time-out for the script using the "+time=" option |
| 621 | 146 to the 'dig' command.</PRE |
| 622 | ></TD |
| 623 | ></TR |
| 624 | ></TABLE |
| 625 | ><HR></DIV |
| 626 | ><P |
| 627 | >For a much more elaborate version of the above script, see |
| 628 | <A |
| 629 | HREF="contributed-scripts.html#ISSPAMMER2" |
| 630 | >Example A-28</A |
| 631 | >.</P |
| 632 | ></DD |
| 633 | ><DT |
| 634 | ><A |
| 635 | NAME="TRACEROUTEREF" |
| 636 | ></A |
| 637 | ><B |
| 638 | CLASS="COMMAND" |
| 639 | >traceroute</B |
| 640 | ></DT |
| 641 | ><DD |
| 642 | ><P |
| 643 | >Trace the route taken by packets sent to a remote host. This |
| 644 | command works within a LAN, WAN, or over the |
| 645 | Internet. The remote host may be specified by an IP |
| 646 | address. The output of this command may be filtered |
| 647 | by <A |
| 648 | HREF="textproc.html#GREPREF" |
| 649 | >grep</A |
| 650 | > or <A |
| 651 | HREF="sedawk.html#SEDREF" |
| 652 | >sed</A |
| 653 | > in a pipe.</P |
| 654 | ><P |
| 655 | > <TABLE |
| 656 | BORDER="0" |
| 657 | BGCOLOR="#E0E0E0" |
| 658 | WIDTH="90%" |
| 659 | ><TR |
| 660 | ><TD |
| 661 | ><PRE |
| 662 | CLASS="SCREEN" |
| 663 | > <TT |
| 664 | CLASS="PROMPT" |
| 665 | >bash$ </TT |
| 666 | ><TT |
| 667 | CLASS="USERINPUT" |
| 668 | ><B |
| 669 | >traceroute 81.9.6.2</B |
| 670 | ></TT |
| 671 | > |
| 672 | <TT |
| 673 | CLASS="COMPUTEROUTPUT" |
| 674 | >traceroute to 81.9.6.2 (81.9.6.2), 30 hops max, 38 byte packets |
| 675 | 1 tc43.xjbnnbrb.com (136.30.178.8) 191.303 ms 179.400 ms 179.767 ms |
| 676 | 2 or0.xjbnnbrb.com (136.30.178.1) 179.536 ms 179.534 ms 169.685 ms |
| 677 | 3 192.168.11.101 (192.168.11.101) 189.471 ms 189.556 ms * |
| 678 | ...</TT |
| 679 | > |
| 680 | </PRE |
| 681 | ></TD |
| 682 | ></TR |
| 683 | ></TABLE |
| 684 | > |
| 685 | </P |
| 686 | ></DD |
| 687 | ><DT |
| 688 | ><A |
| 689 | NAME="PINGREF" |
| 690 | ></A |
| 691 | ><B |
| 692 | CLASS="COMMAND" |
| 693 | >ping</B |
| 694 | ></DT |
| 695 | ><DD |
| 696 | ><P |
| 697 | >Broadcast an <TT |
| 698 | CLASS="REPLACEABLE" |
| 699 | ><I |
| 700 | >ICMP |
| 701 | ECHO_REQUEST</I |
| 702 | ></TT |
| 703 | > packet to another machine, |
| 704 | either on a local or remote network. This is a |
| 705 | diagnostic tool for testing network connections, |
| 706 | and it should be used with caution.</P |
| 707 | ><P |
| 708 | > <TABLE |
| 709 | BORDER="0" |
| 710 | BGCOLOR="#E0E0E0" |
| 711 | WIDTH="90%" |
| 712 | ><TR |
| 713 | ><TD |
| 714 | ><PRE |
| 715 | CLASS="SCREEN" |
| 716 | > <TT |
| 717 | CLASS="PROMPT" |
| 718 | >bash$ </TT |
| 719 | ><TT |
| 720 | CLASS="USERINPUT" |
| 721 | ><B |
| 722 | >ping localhost</B |
| 723 | ></TT |
| 724 | > |
| 725 | <TT |
| 726 | CLASS="COMPUTEROUTPUT" |
| 727 | >PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data. |
| 728 | 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=709 usec |
| 729 | 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=286 usec |
| 730 | |
| 731 | --- localhost.localdomain ping statistics --- |
| 732 | 2 packets transmitted, 2 packets received, 0% packet loss |
| 733 | round-trip min/avg/max/mdev = 0.286/0.497/0.709/0.212 ms</TT |
| 734 | > |
| 735 | </PRE |
| 736 | ></TD |
| 737 | ></TR |
| 738 | ></TABLE |
| 739 | > |
| 740 | </P |
| 741 | ><P |
| 742 | >A successful <I |
| 743 | CLASS="FIRSTTERM" |
| 744 | >ping</I |
| 745 | > returns |
| 746 | an <A |
| 747 | HREF="exit-status.html#EXITSTATUSREF" |
| 748 | >exit status</A |
| 749 | > of |
| 750 | <SPAN |
| 751 | CLASS="ERRORCODE" |
| 752 | >0</SPAN |
| 753 | >. This can be tested for in a |
| 754 | script.</P |
| 755 | ><P |
| 756 | ><A |
| 757 | NAME="PING0" |
| 758 | ></A |
| 759 | ></P |
| 760 | ><P |
| 761 | ><TABLE |
| 762 | BORDER="0" |
| 763 | BGCOLOR="#E0E0E0" |
| 764 | WIDTH="90%" |
| 765 | ><TR |
| 766 | ><TD |
| 767 | ><PRE |
| 768 | CLASS="PROGRAMLISTING" |
| 769 | > 1 HNAME=news-15.net # Notorious spammer. |
| 770 | 2 # HNAME=$HOST # Debug: test for localhost. |
| 771 | 3 count=2 # Send only two pings. |
| 772 | 4 |
| 773 | 5 if [[ `ping -c $count "$HNAME"` ]] |
| 774 | 6 then |
| 775 | 7 echo ""$HNAME" still up and broadcasting spam your way." |
| 776 | 8 else |
| 777 | 9 echo ""$HNAME" seems to be down. Pity." |
| 778 | 10 fi</PRE |
| 779 | ></TD |
| 780 | ></TR |
| 781 | ></TABLE |
| 782 | ></P |
| 783 | ></DD |
| 784 | ><DT |
| 785 | ><A |
| 786 | NAME="WHOISREF" |
| 787 | ></A |
| 788 | ><B |
| 789 | CLASS="COMMAND" |
| 790 | >whois</B |
| 791 | ></DT |
| 792 | ><DD |
| 793 | ><P |
| 794 | >Perform a DNS (Domain Name System) lookup. |
| 795 | The <TT |
| 796 | CLASS="OPTION" |
| 797 | >-h</TT |
| 798 | > option permits specifying which |
| 799 | particular <I |
| 800 | CLASS="FIRSTTERM" |
| 801 | >whois</I |
| 802 | > server to query. See |
| 803 | <A |
| 804 | HREF="othertypesv.html#EX18" |
| 805 | >Example 4-6</A |
| 806 | > and <A |
| 807 | HREF="communications.html#SPAMLOOKUP" |
| 808 | >Example 16-40</A |
| 809 | >.</P |
| 810 | ></DD |
| 811 | ><DT |
| 812 | ><A |
| 813 | NAME="FINGERREF" |
| 814 | ></A |
| 815 | ><B |
| 816 | CLASS="COMMAND" |
| 817 | >finger</B |
| 818 | ></DT |
| 819 | ><DD |
| 820 | ><P |
| 821 | >Retrieve information about users on a |
| 822 | network. Optionally, this command can display |
| 823 | a user's <TT |
| 824 | CLASS="FILENAME" |
| 825 | >~/.plan</TT |
| 826 | >, |
| 827 | <TT |
| 828 | CLASS="FILENAME" |
| 829 | >~/.project</TT |
| 830 | >, and |
| 831 | <TT |
| 832 | CLASS="FILENAME" |
| 833 | >~/.forward</TT |
| 834 | > files, if present.</P |
| 835 | ><P |
| 836 | > <TABLE |
| 837 | BORDER="0" |
| 838 | BGCOLOR="#E0E0E0" |
| 839 | WIDTH="90%" |
| 840 | ><TR |
| 841 | ><TD |
| 842 | ><PRE |
| 843 | CLASS="SCREEN" |
| 844 | > <TT |
| 845 | CLASS="PROMPT" |
| 846 | >bash$ </TT |
| 847 | ><TT |
| 848 | CLASS="USERINPUT" |
| 849 | ><B |
| 850 | >finger</B |
| 851 | ></TT |
| 852 | > |
| 853 | <TT |
| 854 | CLASS="COMPUTEROUTPUT" |
| 855 | >Login Name Tty Idle Login Time Office Office Phone |
| 856 | bozo Bozo Bozeman tty1 8 Jun 25 16:59 (:0) |
| 857 | bozo Bozo Bozeman ttyp0 Jun 25 16:59 (:0.0) |
| 858 | bozo Bozo Bozeman ttyp1 Jun 25 17:07 (:0.0)</TT |
| 859 | > |
| 860 | |
| 861 | |
| 862 | |
| 863 | <TT |
| 864 | CLASS="PROMPT" |
| 865 | >bash$ </TT |
| 866 | ><TT |
| 867 | CLASS="USERINPUT" |
| 868 | ><B |
| 869 | >finger bozo</B |
| 870 | ></TT |
| 871 | > |
| 872 | <TT |
| 873 | CLASS="COMPUTEROUTPUT" |
| 874 | >Login: bozo Name: Bozo Bozeman |
| 875 | Directory: /home/bozo Shell: /bin/bash |
| 876 | Office: 2355 Clown St., 543-1234 |
| 877 | On since Fri Aug 31 20:13 (MST) on tty1 1 hour 38 minutes idle |
| 878 | On since Fri Aug 31 20:13 (MST) on pts/0 12 seconds idle |
| 879 | On since Fri Aug 31 20:13 (MST) on pts/1 |
| 880 | On since Fri Aug 31 20:31 (MST) on pts/2 1 hour 16 minutes idle |
| 881 | Mail last read Tue Jul 3 10:08 2007 (MST) |
| 882 | No Plan.</TT |
| 883 | > |
| 884 | </PRE |
| 885 | ></TD |
| 886 | ></TR |
| 887 | ></TABLE |
| 888 | > |
| 889 | </P |
| 890 | ><P |
| 891 | >Out of security considerations, many networks disable |
| 892 | <B |
| 893 | CLASS="COMMAND" |
| 894 | >finger</B |
| 895 | > and its associated daemon. |
| 896 | <A |
| 897 | NAME="AEN13320" |
| 898 | HREF="#FTN.AEN13320" |
| 899 | >[1]</A |
| 900 | > |
| 901 | </P |
| 902 | ></DD |
| 903 | ><DT |
| 904 | ><A |
| 905 | NAME="CHFNREF" |
| 906 | ></A |
| 907 | ><B |
| 908 | CLASS="COMMAND" |
| 909 | >chfn</B |
| 910 | ></DT |
| 911 | ><DD |
| 912 | ><P |
| 913 | >Change information disclosed by the |
| 914 | <B |
| 915 | CLASS="COMMAND" |
| 916 | >finger</B |
| 917 | > command.</P |
| 918 | ></DD |
| 919 | ><DT |
| 920 | ><A |
| 921 | NAME="VRFYREF" |
| 922 | ></A |
| 923 | ><B |
| 924 | CLASS="COMMAND" |
| 925 | >vrfy</B |
| 926 | ></DT |
| 927 | ><DD |
| 928 | ><P |
| 929 | >Verify an Internet e-mail address.</P |
| 930 | ><P |
| 931 | >This command seems to be missing from newer Linux |
| 932 | distros.</P |
| 933 | ></DD |
| 934 | ></DL |
| 935 | ></DIV |
| 936 | ><DIV |
| 937 | CLASS="VARIABLELIST" |
| 938 | ><P |
| 939 | ><B |
| 940 | ><A |
| 941 | NAME="COMMREMOTE1" |
| 942 | ></A |
| 943 | >Remote Host Access</B |
| 944 | ></P |
| 945 | ><DL |
| 946 | ><DT |
| 947 | ><A |
| 948 | NAME="RXREF" |
| 949 | ></A |
| 950 | ><B |
| 951 | CLASS="COMMAND" |
| 952 | >sx</B |
| 953 | >, <B |
| 954 | CLASS="COMMAND" |
| 955 | >rx</B |
| 956 | ></DT |
| 957 | ><DD |
| 958 | ><P |
| 959 | >The <B |
| 960 | CLASS="COMMAND" |
| 961 | >sx</B |
| 962 | > and <B |
| 963 | CLASS="COMMAND" |
| 964 | >rx</B |
| 965 | > |
| 966 | command set serves to transfer files to and from a remote |
| 967 | host using the <I |
| 968 | CLASS="FIRSTTERM" |
| 969 | >xmodem</I |
| 970 | > protocol. These |
| 971 | are generally part of a communications package, such as |
| 972 | <B |
| 973 | CLASS="COMMAND" |
| 974 | >minicom</B |
| 975 | >.</P |
| 976 | ></DD |
| 977 | ><DT |
| 978 | ><A |
| 979 | NAME="RZREF" |
| 980 | ></A |
| 981 | ><B |
| 982 | CLASS="COMMAND" |
| 983 | >sz</B |
| 984 | >, <B |
| 985 | CLASS="COMMAND" |
| 986 | >rz</B |
| 987 | ></DT |
| 988 | ><DD |
| 989 | ><P |
| 990 | >The <B |
| 991 | CLASS="COMMAND" |
| 992 | >sz</B |
| 993 | > and <B |
| 994 | CLASS="COMMAND" |
| 995 | >rz</B |
| 996 | > |
| 997 | command set serves to transfer files to and from a remote |
| 998 | host using the <I |
| 999 | CLASS="FIRSTTERM" |
| 1000 | >zmodem</I |
| 1001 | > protocol. |
| 1002 | <I |
| 1003 | CLASS="FIRSTTERM" |
| 1004 | >Zmodem</I |
| 1005 | > has certain advantages over |
| 1006 | <I |
| 1007 | CLASS="FIRSTTERM" |
| 1008 | >xmodem</I |
| 1009 | >, such as faster transmission |
| 1010 | rate and resumption of interrupted file transfers. |
| 1011 | Like <B |
| 1012 | CLASS="COMMAND" |
| 1013 | >sx</B |
| 1014 | > and <B |
| 1015 | CLASS="COMMAND" |
| 1016 | >rx</B |
| 1017 | >, |
| 1018 | these are generally part of a communications package.</P |
| 1019 | ></DD |
| 1020 | ><DT |
| 1021 | ><A |
| 1022 | NAME="FTPREF" |
| 1023 | ></A |
| 1024 | ><B |
| 1025 | CLASS="COMMAND" |
| 1026 | >ftp</B |
| 1027 | ></DT |
| 1028 | ><DD |
| 1029 | ><P |
| 1030 | >Utility and protocol for uploading / downloading |
| 1031 | files to or from a remote host. An ftp session can be automated |
| 1032 | in a script (see <A |
| 1033 | HREF="here-docs.html#EX72" |
| 1034 | >Example 19-6</A |
| 1035 | > and <A |
| 1036 | HREF="contributed-scripts.html#ENCRYPTEDPW" |
| 1037 | >Example A-4</A |
| 1038 | >).</P |
| 1039 | ></DD |
| 1040 | ><DT |
| 1041 | ><A |
| 1042 | NAME="UUCPREF" |
| 1043 | ></A |
| 1044 | ><B |
| 1045 | CLASS="COMMAND" |
| 1046 | >uucp</B |
| 1047 | >, <A |
| 1048 | NAME="UUXREF" |
| 1049 | ></A |
| 1050 | ><B |
| 1051 | CLASS="COMMAND" |
| 1052 | >uux</B |
| 1053 | >, <A |
| 1054 | NAME="CUREF" |
| 1055 | ></A |
| 1056 | ><B |
| 1057 | CLASS="COMMAND" |
| 1058 | >cu</B |
| 1059 | ></DT |
| 1060 | ><DD |
| 1061 | ><P |
| 1062 | ><B |
| 1063 | CLASS="COMMAND" |
| 1064 | >uucp</B |
| 1065 | >: <I |
| 1066 | CLASS="FIRSTTERM" |
| 1067 | >UNIX to UNIX |
| 1068 | copy</I |
| 1069 | >. This is a communications package for |
| 1070 | transferring files between UNIX servers. A shell script |
| 1071 | is an effective way to handle a <B |
| 1072 | CLASS="COMMAND" |
| 1073 | >uucp</B |
| 1074 | > |
| 1075 | command sequence.</P |
| 1076 | ><P |
| 1077 | >Since the advent of the Internet and e-mail, |
| 1078 | <B |
| 1079 | CLASS="COMMAND" |
| 1080 | >uucp</B |
| 1081 | > seems to have faded into obscurity, |
| 1082 | but it still exists and remains perfectly workable in |
| 1083 | situations where an Internet connection is not available |
| 1084 | or appropriate. The advantage of <B |
| 1085 | CLASS="COMMAND" |
| 1086 | >uucp</B |
| 1087 | > |
| 1088 | is that it is fault-tolerant, so even if there is a service |
| 1089 | interruption the copy operation will resume where it left |
| 1090 | off when the connection is restored.</P |
| 1091 | ><P |
| 1092 | >---</P |
| 1093 | ><P |
| 1094 | ><B |
| 1095 | CLASS="COMMAND" |
| 1096 | >uux</B |
| 1097 | >: <I |
| 1098 | CLASS="FIRSTTERM" |
| 1099 | >UNIX to UNIX |
| 1100 | execute</I |
| 1101 | >. Execute a command on a remote system. |
| 1102 | This command is part of the <B |
| 1103 | CLASS="COMMAND" |
| 1104 | >uucp</B |
| 1105 | > |
| 1106 | package.</P |
| 1107 | ><P |
| 1108 | >---</P |
| 1109 | ><P |
| 1110 | ><B |
| 1111 | CLASS="COMMAND" |
| 1112 | >cu</B |
| 1113 | >: <B |
| 1114 | CLASS="COMMAND" |
| 1115 | >C</B |
| 1116 | >all |
| 1117 | <B |
| 1118 | CLASS="COMMAND" |
| 1119 | >U</B |
| 1120 | >p a remote system and connect as a |
| 1121 | simple terminal. It is a sort of dumbed-down version of |
| 1122 | <A |
| 1123 | HREF="communications.html#TELNETREF" |
| 1124 | >telnet</A |
| 1125 | >. This command is |
| 1126 | part of the <B |
| 1127 | CLASS="COMMAND" |
| 1128 | >uucp</B |
| 1129 | > package.</P |
| 1130 | ></DD |
| 1131 | ><DT |
| 1132 | ><A |
| 1133 | NAME="TELNETREF" |
| 1134 | ></A |
| 1135 | ><B |
| 1136 | CLASS="COMMAND" |
| 1137 | >telnet</B |
| 1138 | ></DT |
| 1139 | ><DD |
| 1140 | ><P |
| 1141 | >Utility and protocol for connecting to a remote host.</P |
| 1142 | ><DIV |
| 1143 | CLASS="CAUTION" |
| 1144 | ><TABLE |
| 1145 | CLASS="CAUTION" |
| 1146 | WIDTH="90%" |
| 1147 | BORDER="0" |
| 1148 | ><TR |
| 1149 | ><TD |
| 1150 | WIDTH="25" |
| 1151 | ALIGN="CENTER" |
| 1152 | VALIGN="TOP" |
| 1153 | ><IMG |
| 1154 | SRC="common/caution.png" |
| 1155 | HSPACE="5" |
| 1156 | ALT="Caution"></TD |
| 1157 | ><TD |
| 1158 | ALIGN="LEFT" |
| 1159 | VALIGN="TOP" |
| 1160 | ><P |
| 1161 | >The <I |
| 1162 | CLASS="FIRSTTERM" |
| 1163 | >telnet</I |
| 1164 | > protocol |
| 1165 | contains security holes and should therefore probably be |
| 1166 | avoided. Its use within a shell script is |
| 1167 | <SPAN |
| 1168 | CLASS="emphasis" |
| 1169 | ><I |
| 1170 | CLASS="EMPHASIS" |
| 1171 | >not</I |
| 1172 | ></SPAN |
| 1173 | > recommended.</P |
| 1174 | ></TD |
| 1175 | ></TR |
| 1176 | ></TABLE |
| 1177 | ></DIV |
| 1178 | ></DD |
| 1179 | ><DT |
| 1180 | ><A |
| 1181 | NAME="WGETREF" |
| 1182 | ></A |
| 1183 | ><B |
| 1184 | CLASS="COMMAND" |
| 1185 | >wget</B |
| 1186 | ></DT |
| 1187 | ><DD |
| 1188 | ><P |
| 1189 | >The <B |
| 1190 | CLASS="COMMAND" |
| 1191 | >wget</B |
| 1192 | > utility |
| 1193 | <I |
| 1194 | CLASS="FIRSTTERM" |
| 1195 | >noninteractively</I |
| 1196 | > retrieves or |
| 1197 | downloads files from a Web or ftp site. It works well in a |
| 1198 | script.</P |
| 1199 | ><P |
| 1200 | ><TABLE |
| 1201 | BORDER="0" |
| 1202 | BGCOLOR="#E0E0E0" |
| 1203 | WIDTH="90%" |
| 1204 | ><TR |
| 1205 | ><TD |
| 1206 | ><PRE |
| 1207 | CLASS="PROGRAMLISTING" |
| 1208 | > 1 wget -p http://www.xyz23.com/file01.html |
| 1209 | 2 # The -p or --page-requisite option causes wget to fetch all files |
| 1210 | 3 #+ required to display the specified page. |
| 1211 | 4 |
| 1212 | 5 wget -r ftp://ftp.xyz24.net/~bozo/project_files/ -O $SAVEFILE |
| 1213 | 6 # The -r option recursively follows and retrieves all links |
| 1214 | 7 #+ on the specified site. |
| 1215 | 8 |
| 1216 | 9 wget -c ftp://ftp.xyz25.net/bozofiles/filename.tar.bz2 |
| 1217 | 10 # The -c option lets wget resume an interrupted download. |
| 1218 | 11 # This works with ftp servers and many HTTP sites.</PRE |
| 1219 | ></TD |
| 1220 | ></TR |
| 1221 | ></TABLE |
| 1222 | ></P |
| 1223 | ><DIV |
| 1224 | CLASS="EXAMPLE" |
| 1225 | ><HR><A |
| 1226 | NAME="QUOTEFETCH" |
| 1227 | ></A |
| 1228 | ><P |
| 1229 | ><B |
| 1230 | >Example 16-42. Getting a stock quote</B |
| 1231 | ></P |
| 1232 | ><TABLE |
| 1233 | BORDER="0" |
| 1234 | BGCOLOR="#E0E0E0" |
| 1235 | WIDTH="90%" |
| 1236 | ><TR |
| 1237 | ><TD |
| 1238 | ><PRE |
| 1239 | CLASS="PROGRAMLISTING" |
| 1240 | > 1 #!/bin/bash |
| 1241 | 2 # quote-fetch.sh: Download a stock quote. |
| 1242 | 3 |
| 1243 | 4 |
| 1244 | 5 E_NOPARAMS=86 |
| 1245 | 6 |
| 1246 | 7 if [ -z "$1" ] # Must specify a stock (symbol) to fetch. |
| 1247 | 8 then echo "Usage: `basename $0` stock-symbol" |
| 1248 | 9 exit $E_NOPARAMS |
| 1249 | 10 fi |
| 1250 | 11 |
| 1251 | 12 stock_symbol=$1 |
| 1252 | 13 |
| 1253 | 14 file_suffix=.html |
| 1254 | 15 # Fetches an HTML file, so name it appropriately. |
| 1255 | 16 URL='http://finance.yahoo.com/q?s=' |
| 1256 | 17 # Yahoo finance board, with stock query suffix. |
| 1257 | 18 |
| 1258 | 19 # ----------------------------------------------------------- |
| 1259 | 20 wget -O ${stock_symbol}${file_suffix} "${URL}${stock_symbol}" |
| 1260 | 21 # ----------------------------------------------------------- |
| 1261 | 22 |
| 1262 | 23 |
| 1263 | 24 # To look up stuff on http://search.yahoo.com: |
| 1264 | 25 # ----------------------------------------------------------- |
| 1265 | 26 # URL="http://search.yahoo.com/search?fr=ush-news&p=${query}" |
| 1266 | 27 # wget -O "$savefilename" "${URL}" |
| 1267 | 28 # ----------------------------------------------------------- |
| 1268 | 29 # Saves a list of relevant URLs. |
| 1269 | 30 |
| 1270 | 31 exit $? |
| 1271 | 32 |
| 1272 | 33 # Exercises: |
| 1273 | 34 # --------- |
| 1274 | 35 # |
| 1275 | 36 # 1) Add a test to ensure the user running the script is on-line. |
| 1276 | 37 # (Hint: parse the output of 'ps -ax' for "ppp" or "connect." |
| 1277 | 38 # |
| 1278 | 39 # 2) Modify this script to fetch the local weather report, |
| 1279 | 40 #+ taking the user's zip code as an argument.</PRE |
| 1280 | ></TD |
| 1281 | ></TR |
| 1282 | ></TABLE |
| 1283 | ><HR></DIV |
| 1284 | ><P |
| 1285 | >See also <A |
| 1286 | HREF="contributed-scripts.html#WGETTER2" |
| 1287 | >Example A-30</A |
| 1288 | > and <A |
| 1289 | HREF="contributed-scripts.html#BASHPODDER" |
| 1290 | >Example A-31</A |
| 1291 | >.</P |
| 1292 | ></DD |
| 1293 | ><DT |
| 1294 | ><A |
| 1295 | NAME="LYNXREF" |
| 1296 | ></A |
| 1297 | ><B |
| 1298 | CLASS="COMMAND" |
| 1299 | >lynx</B |
| 1300 | ></DT |
| 1301 | ><DD |
| 1302 | ><P |
| 1303 | >The <B |
| 1304 | CLASS="COMMAND" |
| 1305 | >lynx</B |
| 1306 | > Web and file browser |
| 1307 | can be used inside a script (with the |
| 1308 | <TT |
| 1309 | CLASS="OPTION" |
| 1310 | >-dump</TT |
| 1311 | > option) to retrieve a file from a Web or |
| 1312 | ftp site noninteractively.</P |
| 1313 | ><P |
| 1314 | > <TABLE |
| 1315 | BORDER="0" |
| 1316 | BGCOLOR="#E0E0E0" |
| 1317 | WIDTH="90%" |
| 1318 | ><TR |
| 1319 | ><TD |
| 1320 | ><PRE |
| 1321 | CLASS="PROGRAMLISTING" |
| 1322 | > 1 lynx -dump http://www.xyz23.com/file01.html >$SAVEFILE</PRE |
| 1323 | ></TD |
| 1324 | ></TR |
| 1325 | ></TABLE |
| 1326 | > |
| 1327 | </P |
| 1328 | ><P |
| 1329 | >With the <TT |
| 1330 | CLASS="OPTION" |
| 1331 | >-traversal</TT |
| 1332 | > option, |
| 1333 | <B |
| 1334 | CLASS="COMMAND" |
| 1335 | >lynx</B |
| 1336 | > starts at the HTTP URL specified |
| 1337 | as an argument, then <SPAN |
| 1338 | CLASS="QUOTE" |
| 1339 | >"crawls"</SPAN |
| 1340 | > through all |
| 1341 | links located on that particular server. Used together |
| 1342 | with the <TT |
| 1343 | CLASS="OPTION" |
| 1344 | >-crawl</TT |
| 1345 | > option, outputs page text |
| 1346 | to a log file.</P |
| 1347 | ></DD |
| 1348 | ><DT |
| 1349 | ><A |
| 1350 | NAME="RLOGINREF" |
| 1351 | ></A |
| 1352 | ><B |
| 1353 | CLASS="COMMAND" |
| 1354 | >rlogin</B |
| 1355 | ></DT |
| 1356 | ><DD |
| 1357 | ><P |
| 1358 | ><TT |
| 1359 | CLASS="REPLACEABLE" |
| 1360 | ><I |
| 1361 | >Remote login</I |
| 1362 | ></TT |
| 1363 | >, initates a |
| 1364 | session on a remote host. This command has security issues, |
| 1365 | so use <A |
| 1366 | HREF="communications.html#SSHREF" |
| 1367 | >ssh</A |
| 1368 | > instead.</P |
| 1369 | ></DD |
| 1370 | ><DT |
| 1371 | ><A |
| 1372 | NAME="RSHREF" |
| 1373 | ></A |
| 1374 | ><B |
| 1375 | CLASS="COMMAND" |
| 1376 | >rsh</B |
| 1377 | ></DT |
| 1378 | ><DD |
| 1379 | ><P |
| 1380 | ><TT |
| 1381 | CLASS="REPLACEABLE" |
| 1382 | ><I |
| 1383 | >Remote shell</I |
| 1384 | ></TT |
| 1385 | >, executes |
| 1386 | command(s) on a remote host. This has security issues, |
| 1387 | so use <B |
| 1388 | CLASS="COMMAND" |
| 1389 | >ssh</B |
| 1390 | > instead.</P |
| 1391 | ></DD |
| 1392 | ><DT |
| 1393 | ><A |
| 1394 | NAME="RCPREF" |
| 1395 | ></A |
| 1396 | ><B |
| 1397 | CLASS="COMMAND" |
| 1398 | >rcp</B |
| 1399 | ></DT |
| 1400 | ><DD |
| 1401 | ><P |
| 1402 | ><TT |
| 1403 | CLASS="REPLACEABLE" |
| 1404 | ><I |
| 1405 | >Remote copy</I |
| 1406 | ></TT |
| 1407 | >, copies files |
| 1408 | between two different networked machines.</P |
| 1409 | ></DD |
| 1410 | ><DT |
| 1411 | ><A |
| 1412 | NAME="RSYNCREF" |
| 1413 | ></A |
| 1414 | ><B |
| 1415 | CLASS="COMMAND" |
| 1416 | >rsync</B |
| 1417 | ></DT |
| 1418 | ><DD |
| 1419 | ><P |
| 1420 | ><TT |
| 1421 | CLASS="REPLACEABLE" |
| 1422 | ><I |
| 1423 | >Remote synchronize</I |
| 1424 | ></TT |
| 1425 | >, updates |
| 1426 | (synchronizes) files |
| 1427 | between two different networked machines.</P |
| 1428 | ><P |
| 1429 | > <TABLE |
| 1430 | BORDER="0" |
| 1431 | BGCOLOR="#E0E0E0" |
| 1432 | WIDTH="90%" |
| 1433 | ><TR |
| 1434 | ><TD |
| 1435 | ><PRE |
| 1436 | CLASS="SCREEN" |
| 1437 | > <TT |
| 1438 | CLASS="PROMPT" |
| 1439 | >bash$ </TT |
| 1440 | ><TT |
| 1441 | CLASS="USERINPUT" |
| 1442 | ><B |
| 1443 | >rsync -a ~/sourcedir/*txt /node1/subdirectory/</B |
| 1444 | ></TT |
| 1445 | > |
| 1446 | </PRE |
| 1447 | ></TD |
| 1448 | ></TR |
| 1449 | ></TABLE |
| 1450 | > |
| 1451 | </P |
| 1452 | ><DIV |
| 1453 | CLASS="EXAMPLE" |
| 1454 | ><HR><A |
| 1455 | NAME="FC4UPD" |
| 1456 | ></A |
| 1457 | ><P |
| 1458 | ><B |
| 1459 | >Example 16-43. Updating FC4</B |
| 1460 | ></P |
| 1461 | ><TABLE |
| 1462 | BORDER="0" |
| 1463 | BGCOLOR="#E0E0E0" |
| 1464 | WIDTH="90%" |
| 1465 | ><TR |
| 1466 | ><TD |
| 1467 | ><PRE |
| 1468 | CLASS="PROGRAMLISTING" |
| 1469 | > 1 #!/bin/bash |
| 1470 | 2 # fc4upd.sh |
| 1471 | 3 |
| 1472 | 4 # Script author: Frank Wang. |
| 1473 | 5 # Slight stylistic modifications by ABS Guide author. |
| 1474 | 6 # Used in ABS Guide with permission. |
| 1475 | 7 |
| 1476 | 8 |
| 1477 | 9 # Download Fedora Core 4 update from mirror site using rsync. |
| 1478 | 10 # Should also work for newer Fedora Cores -- 5, 6, . . . |
| 1479 | 11 # Only download latest package if multiple versions exist, |
| 1480 | 12 #+ to save space. |
| 1481 | 13 |
| 1482 | 14 URL=rsync://distro.ibiblio.org/fedora-linux-core/updates/ |
| 1483 | 15 # URL=rsync://ftp.kddilabs.jp/fedora/core/updates/ |
| 1484 | 16 # URL=rsync://rsync.planetmirror.com/fedora-linux-core/updates/ |
| 1485 | 17 |
| 1486 | 18 DEST=${1:-/var/www/html/fedora/updates/} |
| 1487 | 19 LOG=/tmp/repo-update-$(/bin/date +%Y-%m-%d).txt |
| 1488 | 20 PID_FILE=/var/run/${0##*/}.pid |
| 1489 | 21 |
| 1490 | 22 E_RETURN=85 # Something unexpected happened. |
| 1491 | 23 |
| 1492 | 24 |
| 1493 | 25 # General rsync options |
| 1494 | 26 # -r: recursive download |
| 1495 | 27 # -t: reserve time |
| 1496 | 28 # -v: verbose |
| 1497 | 29 |
| 1498 | 30 OPTS="-rtv --delete-excluded --delete-after --partial" |
| 1499 | 31 |
| 1500 | 32 # rsync include pattern |
| 1501 | 33 # Leading slash causes absolute path name match. |
| 1502 | 34 INCLUDE=( |
| 1503 | 35 "/4/i386/kde-i18n-Chinese*" |
| 1504 | 36 # ^ ^ |
| 1505 | 37 # Quoting is necessary to prevent globbing. |
| 1506 | 38 ) |
| 1507 | 39 |
| 1508 | 40 |
| 1509 | 41 # rsync exclude pattern |
| 1510 | 42 # Temporarily comment out unwanted pkgs using "#" . . . |
| 1511 | 43 EXCLUDE=( |
| 1512 | 44 /1 |
| 1513 | 45 /2 |
| 1514 | 46 /3 |
| 1515 | 47 /testing |
| 1516 | 48 /4/SRPMS |
| 1517 | 49 /4/ppc |
| 1518 | 50 /4/x86_64 |
| 1519 | 51 /4/i386/debug |
| 1520 | 52 "/4/i386/kde-i18n-*" |
| 1521 | 53 "/4/i386/openoffice.org-langpack-*" |
| 1522 | 54 "/4/i386/*i586.rpm" |
| 1523 | 55 "/4/i386/GFS-*" |
| 1524 | 56 "/4/i386/cman-*" |
| 1525 | 57 "/4/i386/dlm-*" |
| 1526 | 58 "/4/i386/gnbd-*" |
| 1527 | 59 "/4/i386/kernel-smp*" |
| 1528 | 60 # "/4/i386/kernel-xen*" |
| 1529 | 61 # "/4/i386/xen-*" |
| 1530 | 62 ) |
| 1531 | 63 |
| 1532 | 64 |
| 1533 | 65 init () { |
| 1534 | 66 # Let pipe command return possible rsync error, e.g., stalled network. |
| 1535 | 67 set -o pipefail # Newly introduced in Bash, version 3. |
| 1536 | 68 |
| 1537 | 69 TMP=${TMPDIR:-/tmp}/${0##*/}.$$ # Store refined download list. |
| 1538 | 70 trap "{ |
| 1539 | 71 rm -f $TMP 2>/dev/null |
| 1540 | 72 }" EXIT # Clear temporary file on exit. |
| 1541 | 73 } |
| 1542 | 74 |
| 1543 | 75 |
| 1544 | 76 check_pid () { |
| 1545 | 77 # Check if process exists. |
| 1546 | 78 if [ -s "$PID_FILE" ]; then |
| 1547 | 79 echo "PID file exists. Checking ..." |
| 1548 | 80 PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE) |
| 1549 | 81 if /bin/ps --pid $PID &>/dev/null; then |
| 1550 | 82 echo "Process $PID found. ${0##*/} seems to be running!" |
| 1551 | 83 /usr/bin/logger -t ${0##*/} \ |
| 1552 | 84 "Process $PID found. ${0##*/} seems to be running!" |
| 1553 | 85 exit $E_RETURN |
| 1554 | 86 fi |
| 1555 | 87 echo "Process $PID not found. Start new process . . ." |
| 1556 | 88 fi |
| 1557 | 89 } |
| 1558 | 90 |
| 1559 | 91 |
| 1560 | 92 # Set overall file update range starting from root or $URL, |
| 1561 | 93 #+ according to above patterns. |
| 1562 | 94 set_range () { |
| 1563 | 95 include= |
| 1564 | 96 exclude= |
| 1565 | 97 for p in "${INCLUDE[@]}"; do |
| 1566 | 98 include="$include --include \"$p\"" |
| 1567 | 99 done |
| 1568 | 100 |
| 1569 | 101 for p in "${EXCLUDE[@]}"; do |
| 1570 | 102 exclude="$exclude --exclude \"$p\"" |
| 1571 | 103 done |
| 1572 | 104 } |
| 1573 | 105 |
| 1574 | 106 |
| 1575 | 107 # Retrieve and refine rsync update list. |
| 1576 | 108 get_list () { |
| 1577 | 109 echo $$ > $PID_FILE || { |
| 1578 | 110 echo "Can't write to pid file $PID_FILE" |
| 1579 | 111 exit $E_RETURN |
| 1580 | 112 } |
| 1581 | 113 |
| 1582 | 114 echo -n "Retrieving and refining update list . . ." |
| 1583 | 115 |
| 1584 | 116 # Retrieve list -- 'eval' is needed to run rsync as a single command. |
| 1585 | 117 # $3 and $4 is the date and time of file creation. |
| 1586 | 118 # $5 is the full package name. |
| 1587 | 119 previous= |
| 1588 | 120 pre_file= |
| 1589 | 121 pre_date=0 |
| 1590 | 122 eval /bin/nice /usr/bin/rsync \ |
| 1591 | 123 -r $include $exclude $URL | \ |
| 1592 | 124 egrep '^dr.x|^-r' | \ |
| 1593 | 125 awk '{print $3, $4, $5}' | \ |
| 1594 | 126 sort -k3 | \ |
| 1595 | 127 { while read line; do |
| 1596 | 128 # Get seconds since epoch, to filter out obsolete pkgs. |
| 1597 | 129 cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s) |
| 1598 | 130 # echo $cur_date |
| 1599 | 131 |
| 1600 | 132 # Get file name. |
| 1601 | 133 cur_file=$(echo $line | awk '{print $3}') |
| 1602 | 134 # echo $cur_file |
| 1603 | 135 |
| 1604 | 136 # Get rpm pkg name from file name, if possible. |
| 1605 | 137 if [[ $cur_file == *rpm ]]; then |
| 1606 | 138 pkg_name=$(echo $cur_file | sed -r -e \ |
| 1607 | 139 's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/') |
| 1608 | 140 else |
| 1609 | 141 pkg_name= |
| 1610 | 142 fi |
| 1611 | 143 # echo $pkg_name |
| 1612 | 144 |
| 1613 | 145 if [ -z "$pkg_name" ]; then # If not a rpm file, |
| 1614 | 146 echo $cur_file >> $TMP #+ then append to download list. |
| 1615 | 147 elif [ "$pkg_name" != "$previous" ]; then # A new pkg found. |
| 1616 | 148 echo $pre_file >> $TMP # Output latest file. |
| 1617 | 149 previous=$pkg_name # Save current. |
| 1618 | 150 pre_date=$cur_date |
| 1619 | 151 pre_file=$cur_file |
| 1620 | 152 elif [ "$cur_date" -gt "$pre_date" ]; then |
| 1621 | 153 # If same pkg, but newer, |
| 1622 | 154 pre_date=$cur_date #+ then update latest pointer. |
| 1623 | 155 pre_file=$cur_file |
| 1624 | 156 fi |
| 1625 | 157 done |
| 1626 | 158 echo $pre_file >> $TMP # TMP contains ALL |
| 1627 | 159 #+ of refined list now. |
| 1628 | 160 # echo "subshell=$BASH_SUBSHELL" |
| 1629 | 161 |
| 1630 | 162 } # Bracket required here to let final "echo $pre_file >> $TMP" |
| 1631 | 163 # Remained in the same subshell ( 1 ) with the entire loop. |
| 1632 | 164 |
| 1633 | 165 RET=$? # Get return code of the pipe command. |
| 1634 | 166 |
| 1635 | 167 [ "$RET" -ne 0 ] && { |
| 1636 | 168 echo "List retrieving failed with code $RET" |
| 1637 | 169 exit $E_RETURN |
| 1638 | 170 } |
| 1639 | 171 |
| 1640 | 172 echo "done"; echo |
| 1641 | 173 } |
| 1642 | 174 |
| 1643 | 175 # Real rsync download part. |
| 1644 | 176 get_file () { |
| 1645 | 177 |
| 1646 | 178 echo "Downloading..." |
| 1647 | 179 /bin/nice /usr/bin/rsync \ |
| 1648 | 180 $OPTS \ |
| 1649 | 181 --filter "merge,+/ $TMP" \ |
| 1650 | 182 --exclude '*' \ |
| 1651 | 183 $URL $DEST \ |
| 1652 | 184 | /usr/bin/tee $LOG |
| 1653 | 185 |
| 1654 | 186 RET=$? |
| 1655 | 187 |
| 1656 | 188 # --filter merge,+/ is crucial for the intention. |
| 1657 | 189 # + modifier means include and / means absolute path. |
| 1658 | 190 # Then sorted list in $TMP will contain ascending dir name and |
| 1659 | 191 #+ prevent the following --exclude '*' from "shortcutting the circuit." |
| 1660 | 192 |
| 1661 | 193 echo "Done" |
| 1662 | 194 |
| 1663 | 195 rm -f $PID_FILE 2>/dev/null |
| 1664 | 196 |
| 1665 | 197 return $RET |
| 1666 | 198 } |
| 1667 | 199 |
| 1668 | 200 # ------- |
| 1669 | 201 # Main |
| 1670 | 202 init |
| 1671 | 203 check_pid |
| 1672 | 204 set_range |
| 1673 | 205 get_list |
| 1674 | 206 get_file |
| 1675 | 207 RET=$? |
| 1676 | 208 # ------- |
| 1677 | 209 |
| 1678 | 210 if [ "$RET" -eq 0 ]; then |
| 1679 | 211 /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully." |
| 1680 | 212 else |
| 1681 | 213 /usr/bin/logger -t ${0##*/} \ |
| 1682 | 214 "Fedora update mirrored with failure code: $RET" |
| 1683 | 215 fi |
| 1684 | 216 |
| 1685 | 217 exit $RET</PRE |
| 1686 | ></TD |
| 1687 | ></TR |
| 1688 | ></TABLE |
| 1689 | ><HR></DIV |
| 1690 | ><P |
| 1691 | >See also <A |
| 1692 | HREF="contributed-scripts.html#NIGHTLYBACKUP" |
| 1693 | >Example A-32</A |
| 1694 | >.</P |
| 1695 | ><DIV |
| 1696 | CLASS="NOTE" |
| 1697 | ><TABLE |
| 1698 | CLASS="NOTE" |
| 1699 | WIDTH="90%" |
| 1700 | BORDER="0" |
| 1701 | ><TR |
| 1702 | ><TD |
| 1703 | WIDTH="25" |
| 1704 | ALIGN="CENTER" |
| 1705 | VALIGN="TOP" |
| 1706 | ><IMG |
| 1707 | SRC="common/note.png" |
| 1708 | HSPACE="5" |
| 1709 | ALT="Note"></TD |
| 1710 | ><TD |
| 1711 | ALIGN="LEFT" |
| 1712 | VALIGN="TOP" |
| 1713 | ><P |
| 1714 | >Using <A |
| 1715 | HREF="communications.html#RCPREF" |
| 1716 | >rcp</A |
| 1717 | >, <A |
| 1718 | HREF="communications.html#RSYNCREF" |
| 1719 | >rsync</A |
| 1720 | >, and similar |
| 1721 | utilities with security implications in a shell |
| 1722 | script may not be advisable. Consider, instead, using |
| 1723 | <B |
| 1724 | CLASS="COMMAND" |
| 1725 | >ssh</B |
| 1726 | >, <A |
| 1727 | HREF="communications.html#SCPREF" |
| 1728 | >scp</A |
| 1729 | >, |
| 1730 | or an <B |
| 1731 | CLASS="COMMAND" |
| 1732 | >expect</B |
| 1733 | > script.</P |
| 1734 | ></TD |
| 1735 | ></TR |
| 1736 | ></TABLE |
| 1737 | ></DIV |
| 1738 | ></DD |
| 1739 | ><DT |
| 1740 | ><A |
| 1741 | NAME="SSHREF" |
| 1742 | ></A |
| 1743 | ><B |
| 1744 | CLASS="COMMAND" |
| 1745 | >ssh</B |
| 1746 | ></DT |
| 1747 | ><DD |
| 1748 | ><P |
| 1749 | ><TT |
| 1750 | CLASS="REPLACEABLE" |
| 1751 | ><I |
| 1752 | >Secure shell</I |
| 1753 | ></TT |
| 1754 | >, logs onto |
| 1755 | a remote host and executes commands there. This |
| 1756 | secure replacement for <B |
| 1757 | CLASS="COMMAND" |
| 1758 | >telnet</B |
| 1759 | >, |
| 1760 | <B |
| 1761 | CLASS="COMMAND" |
| 1762 | >rlogin</B |
| 1763 | >, <B |
| 1764 | CLASS="COMMAND" |
| 1765 | >rcp</B |
| 1766 | >, and |
| 1767 | <B |
| 1768 | CLASS="COMMAND" |
| 1769 | >rsh</B |
| 1770 | > uses identity authentication |
| 1771 | and encryption. See its <A |
| 1772 | HREF="external.html#MANREF" |
| 1773 | >manpage</A |
| 1774 | > |
| 1775 | for details.</P |
| 1776 | ><DIV |
| 1777 | CLASS="EXAMPLE" |
| 1778 | ><HR><A |
| 1779 | NAME="REMOTE" |
| 1780 | ></A |
| 1781 | ><P |
| 1782 | ><B |
| 1783 | >Example 16-44. Using <I |
| 1784 | CLASS="FIRSTTERM" |
| 1785 | >ssh</I |
| 1786 | ></B |
| 1787 | ></P |
| 1788 | ><TABLE |
| 1789 | BORDER="0" |
| 1790 | BGCOLOR="#E0E0E0" |
| 1791 | WIDTH="90%" |
| 1792 | ><TR |
| 1793 | ><TD |
| 1794 | ><PRE |
| 1795 | CLASS="PROGRAMLISTING" |
| 1796 | > 1 #!/bin/bash |
| 1797 | 2 # remote.bash: Using ssh. |
| 1798 | 3 |
| 1799 | 4 # This example by Michael Zick. |
| 1800 | 5 # Used with permission. |
| 1801 | 6 |
| 1802 | 7 |
| 1803 | 8 # Presumptions: |
| 1804 | 9 # ------------ |
| 1805 | 10 # fd-2 isn't being captured ( '2>/dev/null' ). |
| 1806 | 11 # ssh/sshd presumes stderr ('2') will display to user. |
| 1807 | 12 # |
| 1808 | 13 # sshd is running on your machine. |
| 1809 | 14 # For any 'standard' distribution, it probably is, |
| 1810 | 15 #+ and without any funky ssh-keygen having been done. |
| 1811 | 16 |
| 1812 | 17 # Try ssh to your machine from the command-line: |
| 1813 | 18 # |
| 1814 | 19 # $ ssh $HOSTNAME |
| 1815 | 20 # Without extra set-up you'll be asked for your password. |
| 1816 | 21 # enter password |
| 1817 | 22 # when done, $ exit |
| 1818 | 23 # |
| 1819 | 24 # Did that work? If so, you're ready for more fun. |
| 1820 | 25 |
| 1821 | 26 # Try ssh to your machine as 'root': |
| 1822 | 27 # |
| 1823 | 28 # $ ssh -l root $HOSTNAME |
| 1824 | 29 # When asked for password, enter root's, not yours. |
| 1825 | 30 # Last login: Tue Aug 10 20:25:49 2004 from localhost.localdomain |
| 1826 | 31 # Enter 'exit' when done. |
| 1827 | 32 |
| 1828 | 33 # The above gives you an interactive shell. |
| 1829 | 34 # It is possible for sshd to be set up in a 'single command' mode, |
| 1830 | 35 #+ but that is beyond the scope of this example. |
| 1831 | 36 # The only thing to note is that the following will work in |
| 1832 | 37 #+ 'single command' mode. |
| 1833 | 38 |
| 1834 | 39 |
| 1835 | 40 # A basic, write stdout (local) command. |
| 1836 | 41 |
| 1837 | 42 ls -l |
| 1838 | 43 |
| 1839 | 44 # Now the same basic command on a remote machine. |
| 1840 | 45 # Pass a different 'USERNAME' 'HOSTNAME' if desired: |
| 1841 | 46 USER=${USERNAME:-$(whoami)} |
| 1842 | 47 HOST=${HOSTNAME:-$(hostname)} |
| 1843 | 48 |
| 1844 | 49 # Now excute the above command-line on the remote host, |
| 1845 | 50 #+ with all transmissions encrypted. |
| 1846 | 51 |
| 1847 | 52 ssh -l ${USER} ${HOST} " ls -l " |
| 1848 | 53 |
| 1849 | 54 # The expected result is a listing of your username's home |
| 1850 | 55 #+ directory on the remote machine. |
| 1851 | 56 # To see any difference, run this script from somewhere |
| 1852 | 57 #+ other than your home directory. |
| 1853 | 58 |
| 1854 | 59 # In other words, the Bash command is passed as a quoted line |
| 1855 | 60 #+ to the remote shell, which executes it on the remote machine. |
| 1856 | 61 # In this case, sshd does ' bash -c "ls -l" ' on your behalf. |
| 1857 | 62 |
| 1858 | 63 # For information on topics such as not having to enter a |
| 1859 | 64 #+ password/passphrase for every command-line, see |
| 1860 | 65 #+ man ssh |
| 1861 | 66 #+ man ssh-keygen |
| 1862 | 67 #+ man sshd_config. |
| 1863 | 68 |
| 1864 | 69 exit 0</PRE |
| 1865 | ></TD |
| 1866 | ></TR |
| 1867 | ></TABLE |
| 1868 | ><HR></DIV |
| 1869 | ><DIV |
| 1870 | CLASS="CAUTION" |
| 1871 | ><TABLE |
| 1872 | CLASS="CAUTION" |
| 1873 | WIDTH="90%" |
| 1874 | BORDER="0" |
| 1875 | ><TR |
| 1876 | ><TD |
| 1877 | WIDTH="25" |
| 1878 | ALIGN="CENTER" |
| 1879 | VALIGN="TOP" |
| 1880 | ><IMG |
| 1881 | SRC="common/caution.png" |
| 1882 | HSPACE="5" |
| 1883 | ALT="Caution"></TD |
| 1884 | ><TD |
| 1885 | ALIGN="LEFT" |
| 1886 | VALIGN="TOP" |
| 1887 | ><P |
| 1888 | >Within a loop, <B |
| 1889 | CLASS="COMMAND" |
| 1890 | >ssh</B |
| 1891 | > may cause |
| 1892 | unexpected behavior. According to a <A |
| 1893 | HREF="http://groups-beta.google.com/group/comp.unix.shell/msg/dcb446b5fff7d230" |
| 1894 | TARGET="_top" |
| 1895 | > Usenet post</A |
| 1896 | > in the comp.unix shell archives, |
| 1897 | <B |
| 1898 | CLASS="COMMAND" |
| 1899 | >ssh</B |
| 1900 | > inherits the loop's |
| 1901 | <TT |
| 1902 | CLASS="FILENAME" |
| 1903 | >stdin</TT |
| 1904 | >. To remedy this, pass |
| 1905 | <B |
| 1906 | CLASS="COMMAND" |
| 1907 | >ssh</B |
| 1908 | > either the <TT |
| 1909 | CLASS="OPTION" |
| 1910 | >-n</TT |
| 1911 | > |
| 1912 | or <TT |
| 1913 | CLASS="OPTION" |
| 1914 | >-f</TT |
| 1915 | > option.</P |
| 1916 | ><P |
| 1917 | >Thanks, Jason Bechtel, for pointing this out.</P |
| 1918 | ></TD |
| 1919 | ></TR |
| 1920 | ></TABLE |
| 1921 | ></DIV |
| 1922 | ></DD |
| 1923 | ><DT |
| 1924 | ><A |
| 1925 | NAME="SCPREF" |
| 1926 | ></A |
| 1927 | ><B |
| 1928 | CLASS="COMMAND" |
| 1929 | >scp</B |
| 1930 | ></DT |
| 1931 | ><DD |
| 1932 | ><P |
| 1933 | ><TT |
| 1934 | CLASS="REPLACEABLE" |
| 1935 | ><I |
| 1936 | >Secure copy</I |
| 1937 | ></TT |
| 1938 | >, similar in |
| 1939 | function to <B |
| 1940 | CLASS="COMMAND" |
| 1941 | >rcp</B |
| 1942 | >, copies files between |
| 1943 | two different networked machines, but does so using |
| 1944 | authentication, and with a security level similar to |
| 1945 | <B |
| 1946 | CLASS="COMMAND" |
| 1947 | >ssh</B |
| 1948 | >.</P |
| 1949 | ></DD |
| 1950 | ></DL |
| 1951 | ></DIV |
| 1952 | ><DIV |
| 1953 | CLASS="VARIABLELIST" |
| 1954 | ><P |
| 1955 | ><B |
| 1956 | ><A |
| 1957 | NAME="COMMLOCAL1" |
| 1958 | ></A |
| 1959 | >Local Network</B |
| 1960 | ></P |
| 1961 | ><DL |
| 1962 | ><DT |
| 1963 | ><A |
| 1964 | NAME="WRITEREF" |
| 1965 | ></A |
| 1966 | ><B |
| 1967 | CLASS="COMMAND" |
| 1968 | >write</B |
| 1969 | ></DT |
| 1970 | ><DD |
| 1971 | ><P |
| 1972 | >This is a utility for terminal-to-terminal communication. |
| 1973 | It allows sending lines from your terminal (console or |
| 1974 | <I |
| 1975 | CLASS="FIRSTTERM" |
| 1976 | >xterm</I |
| 1977 | >) to that of another user. The |
| 1978 | <A |
| 1979 | HREF="system.html#MESGREF" |
| 1980 | >mesg</A |
| 1981 | > command may, of course, |
| 1982 | be used to disable write access to a terminal</P |
| 1983 | ><P |
| 1984 | >Since <B |
| 1985 | CLASS="COMMAND" |
| 1986 | >write</B |
| 1987 | > is interactive, it |
| 1988 | would not normally find use in a script.</P |
| 1989 | ></DD |
| 1990 | ><DT |
| 1991 | ><A |
| 1992 | NAME="NETCONFIGREF" |
| 1993 | ></A |
| 1994 | ><B |
| 1995 | CLASS="COMMAND" |
| 1996 | >netconfig</B |
| 1997 | ></DT |
| 1998 | ><DD |
| 1999 | ><P |
| 2000 | >A command-line utility for configuring a network adapter |
| 2001 | (using <I |
| 2002 | CLASS="FIRSTTERM" |
| 2003 | >DHCP</I |
| 2004 | >). This command is native |
| 2005 | to Red Hat centric Linux distros.</P |
| 2006 | ></DD |
| 2007 | ></DL |
| 2008 | ></DIV |
| 2009 | ><DIV |
| 2010 | CLASS="VARIABLELIST" |
| 2011 | ><P |
| 2012 | ><B |
| 2013 | ><A |
| 2014 | NAME="COMMMAIL1" |
| 2015 | ></A |
| 2016 | >Mail</B |
| 2017 | ></P |
| 2018 | ><DL |
| 2019 | ><DT |
| 2020 | ><B |
| 2021 | CLASS="COMMAND" |
| 2022 | >mail</B |
| 2023 | ></DT |
| 2024 | ><DD |
| 2025 | ><P |
| 2026 | >Send or read e-mail messages.</P |
| 2027 | ><P |
| 2028 | >This stripped-down command-line mail client |
| 2029 | works fine as a command embedded in a script.</P |
| 2030 | ><DIV |
| 2031 | CLASS="EXAMPLE" |
| 2032 | ><HR><A |
| 2033 | NAME="SELFMAILER" |
| 2034 | ></A |
| 2035 | ><P |
| 2036 | ><B |
| 2037 | >Example 16-45. A script that mails itself</B |
| 2038 | ></P |
| 2039 | ><TABLE |
| 2040 | BORDER="0" |
| 2041 | BGCOLOR="#E0E0E0" |
| 2042 | WIDTH="90%" |
| 2043 | ><TR |
| 2044 | ><TD |
| 2045 | ><PRE |
| 2046 | CLASS="PROGRAMLISTING" |
| 2047 | > 1 #!/bin/sh |
| 2048 | 2 # self-mailer.sh: Self-mailing script |
| 2049 | 3 |
| 2050 | 4 adr=${1:-`whoami`} # Default to current user, if not specified. |
| 2051 | 5 # Typing 'self-mailer.sh [email protected]' |
| 2052 | 6 #+ sends this script to that addressee. |
| 2053 | 7 # Just 'self-mailer.sh' (no argument) sends the script |
| 2054 | 8 #+ to the person invoking it, for example, [email protected]. |
| 2055 | 9 # |
| 2056 | 10 # For more on the ${parameter:-default} construct, |
| 2057 | 11 #+ see the "Parameter Substitution" section |
| 2058 | 12 #+ of the "Variables Revisited" chapter. |
| 2059 | 13 |
| 2060 | 14 # ============================================================================ |
| 2061 | 15 cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$adr" |
| 2062 | 16 # ============================================================================ |
| 2063 | 17 |
| 2064 | 18 # -------------------------------------------- |
| 2065 | 19 # Greetings from the self-mailing script. |
| 2066 | 20 # A mischievous person has run this script, |
| 2067 | 21 #+ which has caused it to mail itself to you. |
| 2068 | 22 # Apparently, some people have nothing better |
| 2069 | 23 #+ to do with their time. |
| 2070 | 24 # -------------------------------------------- |
| 2071 | 25 |
| 2072 | 26 echo "At `date`, script \"`basename $0`\" mailed to "$adr"." |
| 2073 | 27 |
| 2074 | 28 exit 0 |
| 2075 | 29 |
| 2076 | 30 # Note that the "mailx" command (in "send" mode) may be substituted |
| 2077 | 31 #+ for "mail" ... but with somewhat different options.</PRE |
| 2078 | ></TD |
| 2079 | ></TR |
| 2080 | ></TABLE |
| 2081 | ><HR></DIV |
| 2082 | ></DD |
| 2083 | ><DT |
| 2084 | ><A |
| 2085 | NAME="MAILTOREF" |
| 2086 | ></A |
| 2087 | ><B |
| 2088 | CLASS="COMMAND" |
| 2089 | >mailto</B |
| 2090 | ></DT |
| 2091 | ><DD |
| 2092 | ><P |
| 2093 | >Similar to the <B |
| 2094 | CLASS="COMMAND" |
| 2095 | >mail</B |
| 2096 | > command, |
| 2097 | <B |
| 2098 | CLASS="COMMAND" |
| 2099 | >mailto</B |
| 2100 | > sends e-mail messages |
| 2101 | from the command-line or in a script. However, |
| 2102 | <B |
| 2103 | CLASS="COMMAND" |
| 2104 | >mailto</B |
| 2105 | > also permits sending MIME |
| 2106 | (multimedia) messages.</P |
| 2107 | ></DD |
| 2108 | ><DT |
| 2109 | ><A |
| 2110 | NAME="MAILSTATSREF" |
| 2111 | ></A |
| 2112 | ><B |
| 2113 | CLASS="COMMAND" |
| 2114 | >mailstats</B |
| 2115 | ></DT |
| 2116 | ><DD |
| 2117 | ><P |
| 2118 | >Show <I |
| 2119 | CLASS="FIRSTTERM" |
| 2120 | >mail statistics</I |
| 2121 | >. This command |
| 2122 | may be invoked only by <I |
| 2123 | CLASS="FIRSTTERM" |
| 2124 | >root</I |
| 2125 | >.</P |
| 2126 | ><P |
| 2127 | > <TABLE |
| 2128 | BORDER="0" |
| 2129 | BGCOLOR="#E0E0E0" |
| 2130 | WIDTH="90%" |
| 2131 | ><TR |
| 2132 | ><TD |
| 2133 | ><PRE |
| 2134 | CLASS="SCREEN" |
| 2135 | > <TT |
| 2136 | CLASS="PROMPT" |
| 2137 | >root# </TT |
| 2138 | ><TT |
| 2139 | CLASS="USERINPUT" |
| 2140 | ><B |
| 2141 | >mailstats</B |
| 2142 | ></TT |
| 2143 | > |
| 2144 | <TT |
| 2145 | CLASS="COMPUTEROUTPUT" |
| 2146 | >Statistics from Tue Jan 1 20:32:08 2008 |
| 2147 | M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis msgsqur Mailer |
| 2148 | 4 1682 24118K 0 0K 0 0 0 esmtp |
| 2149 | 9 212 640K 1894 25131K 0 0 0 local |
| 2150 | ===================================================================== |
| 2151 | T 1894 24758K 1894 25131K 0 0 0 |
| 2152 | C 414 0</TT |
| 2153 | > |
| 2154 | </PRE |
| 2155 | ></TD |
| 2156 | ></TR |
| 2157 | ></TABLE |
| 2158 | > |
| 2159 | </P |
| 2160 | ></DD |
| 2161 | ><DT |
| 2162 | ><A |
| 2163 | NAME="VACATIONREF" |
| 2164 | ></A |
| 2165 | ><B |
| 2166 | CLASS="COMMAND" |
| 2167 | >vacation</B |
| 2168 | ></DT |
| 2169 | ><DD |
| 2170 | ><P |
| 2171 | >This utility automatically replies to e-mails that |
| 2172 | the intended recipient is on vacation and temporarily |
| 2173 | unavailable. It runs on a network, in conjunction with |
| 2174 | <B |
| 2175 | CLASS="COMMAND" |
| 2176 | >sendmail</B |
| 2177 | >, and is not applicable to a |
| 2178 | dial-up POPmail account.</P |
| 2179 | ></DD |
| 2180 | ></DL |
| 2181 | ></DIV |
| 2182 | ></DIV |
| 2183 | ><H3 |
| 2184 | CLASS="FOOTNOTES" |
| 2185 | >Notes</H3 |
| 2186 | ><TABLE |
| 2187 | BORDER="0" |
| 2188 | CLASS="FOOTNOTES" |
| 2189 | WIDTH="100%" |
| 2190 | ><TR |
| 2191 | ><TD |
| 2192 | ALIGN="LEFT" |
| 2193 | VALIGN="TOP" |
| 2194 | WIDTH="5%" |
| 2195 | ><A |
| 2196 | NAME="FTN.AEN13320" |
| 2197 | HREF="communications.html#AEN13320" |
| 2198 | >[1]</A |
| 2199 | ></TD |
| 2200 | ><TD |
| 2201 | ALIGN="LEFT" |
| 2202 | VALIGN="TOP" |
| 2203 | WIDTH="95%" |
| 2204 | ><P |
| 2205 | ><A |
| 2206 | NAME="DAEMONREF" |
| 2207 | ></A |
| 2208 | ></P |
| 2209 | ><P |
| 2210 | >A <I |
| 2211 | CLASS="FIRSTTERM" |
| 2212 | >daemon</I |
| 2213 | > is a background |
| 2214 | process not attached to a terminal session. Daemons |
| 2215 | perform designated services either at specified times |
| 2216 | or explicitly triggered by certain events.</P |
| 2217 | ><P |
| 2218 | >The word <SPAN |
| 2219 | CLASS="QUOTE" |
| 2220 | >"daemon"</SPAN |
| 2221 | > means ghost in |
| 2222 | Greek, and there is certainly something mysterious, |
| 2223 | almost supernatural, about the way UNIX daemons |
| 2224 | wander about behind the scenes, silently carrying |
| 2225 | out their appointed tasks.</P |
| 2226 | ></TD |
| 2227 | ></TR |
| 2228 | ></TABLE |
| 2229 | ><DIV |
| 2230 | CLASS="NAVFOOTER" |
| 2231 | ><HR |
| 2232 | ALIGN="LEFT" |
| 2233 | WIDTH="100%"><TABLE |
| 2234 | SUMMARY="Footer navigation table" |
| 2235 | WIDTH="100%" |
| 2236 | BORDER="0" |
| 2237 | CELLPADDING="0" |
| 2238 | CELLSPACING="0" |
| 2239 | ><TR |
| 2240 | ><TD |
| 2241 | WIDTH="33%" |
| 2242 | ALIGN="left" |
| 2243 | VALIGN="top" |
| 2244 | ><A |
| 2245 | HREF="filearchiv.html" |
| 2246 | ACCESSKEY="P" |
| 2247 | >Prev</A |
| 2248 | ></TD |
| 2249 | ><TD |
| 2250 | WIDTH="34%" |
| 2251 | ALIGN="center" |
| 2252 | VALIGN="top" |
| 2253 | ><A |
| 2254 | HREF="index.html" |
| 2255 | ACCESSKEY="H" |
| 2256 | >Home</A |
| 2257 | ></TD |
| 2258 | ><TD |
| 2259 | WIDTH="33%" |
| 2260 | ALIGN="right" |
| 2261 | VALIGN="top" |
| 2262 | ><A |
| 2263 | HREF="terminalccmds.html" |
| 2264 | ACCESSKEY="N" |
| 2265 | >Next</A |
| 2266 | ></TD |
| 2267 | ></TR |
| 2268 | ><TR |
| 2269 | ><TD |
| 2270 | WIDTH="33%" |
| 2271 | ALIGN="left" |
| 2272 | VALIGN="top" |
| 2273 | >File and Archiving Commands</TD |
| 2274 | ><TD |
| 2275 | WIDTH="34%" |
| 2276 | ALIGN="center" |
| 2277 | VALIGN="top" |
| 2278 | ><A |
| 2279 | HREF="external.html" |
| 2280 | ACCESSKEY="U" |
| 2281 | >Up</A |
| 2282 | ></TD |
| 2283 | ><TD |
| 2284 | WIDTH="33%" |
| 2285 | ALIGN="right" |
| 2286 | VALIGN="top" |
| 2287 | >Terminal Control Commands</TD |
| 2288 | ></TR |
| 2289 | ></TABLE |
| 2290 | ></DIV |
| 2291 | ></BODY |
| 2292 | ></HTML |
| 2293 | > |