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