Debian Codegraph @

HTML/networkprogramming.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Network Programming</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="Advanced Topics"
15 HREF="part5.html"><LINK
16 REL="PREVIOUS"
17 TITLE="/proc"
18 HREF="procref1.html"><LINK
19 REL="NEXT"
20 TITLE="Of Zeros and Nulls"
21 HREF="zeros.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="CHAPTER"
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="procref1.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="zeros.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="CHAPTER"
92 ><H1
93 ><A
94 NAME="NETWORKPROGRAMMING"
95 ></A
96 >Chapter 30. Network Programming</H1
97 ><TABLE
98 BORDER="0"
99 WIDTH="100%"
100 CELLSPACING="0"
101 CELLPADDING="0"
102 CLASS="EPIGRAPH"
103 ><TR
104 ><TD
105 WIDTH="45%"
106 >&nbsp;</TD
107 ><TD
108 WIDTH="45%"
109 ALIGN="LEFT"
110 VALIGN="TOP"
111 ><I
112 ><P
113 ><I
114 >The Net's a cross between an elephant and a white
115 elephant sale: it never forgets, and it's always crap.</I
116 ></P
117 ><P
118 ><I
119 >--Nemo</I
120 ></P
121 ></I
122 ></TD
123 ></TR
124 ></TABLE
125 ><P
126 >A Linux system has quite a number of tools for accessing,
127 manipulating, and troubleshooting network connections. We can
128 incorporate some of these tools into scripts -- scripts that
129 expand our knowledge of networking, useful scripts that can
130 facilitate the administration of a network.</P
131 ><P
132 ><A
133 NAME="CGISCRIPT"
134 ></A
135 >Here is a simple CGI
136 script that demonstrates connecting to a remote server.</P
137 ><DIV
138 CLASS="EXAMPLE"
139 ><HR><A
140 NAME="TESTCGI"
141 ></A
142 ><P
143 ><B
144 >Example 30-1. Print the server environment</B
145 ></P
146 ><TABLE
147 BORDER="0"
148 BGCOLOR="#E0E0E0"
149 WIDTH="100%"
150 ><TR
151 ><TD
152 ><PRE
153 CLASS="PROGRAMLISTING"
154 > 1&nbsp;#!/bin/bash
155 2&nbsp;# test-cgi.sh
156 3&nbsp;# by Michael Zick
157 4&nbsp;# Used with permission
158 5&nbsp;
159 6&nbsp;# May have to change the location for your site.
160 7&nbsp;# (At the ISP's servers, Bash may not be in the usual place.)
161 8&nbsp;# Other places: /usr/bin or /usr/local/bin
162 9&nbsp;# Might even try it without any path in sha-bang.
163 10&nbsp;
164 11&nbsp;# Disable filename globbing.
165 12&nbsp;set -f
166 13&nbsp;
167 14&nbsp;# Header tells browser what to expect.
168 15&nbsp;echo Content-type: text/plain
169 16&nbsp;echo
170 17&nbsp;
171 18&nbsp;echo CGI/1.0 test script report:
172 19&nbsp;echo
173 20&nbsp;
174 21&nbsp;echo environment settings:
175 22&nbsp;set
176 23&nbsp;echo
177 24&nbsp;
178 25&nbsp;echo whereis bash?
179 26&nbsp;whereis bash
180 27&nbsp;echo
181 28&nbsp;
182 29&nbsp;
183 30&nbsp;echo who are we?
184 31&nbsp;echo ${BASH_VERSINFO[*]}
185 32&nbsp;echo
186 33&nbsp;
187 34&nbsp;echo argc is $#. argv is "$*".
188 35&nbsp;echo
189 36&nbsp;
190 37&nbsp;# CGI/1.0 expected environment variables.
191 38&nbsp;
192 39&nbsp;echo SERVER_SOFTWARE = $SERVER_SOFTWARE
193 40&nbsp;echo SERVER_NAME = $SERVER_NAME
194 41&nbsp;echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
195 42&nbsp;echo SERVER_PROTOCOL = $SERVER_PROTOCOL
196 43&nbsp;echo SERVER_PORT = $SERVER_PORT
197 44&nbsp;echo REQUEST_METHOD = $REQUEST_METHOD
198 45&nbsp;echo HTTP_ACCEPT = "$HTTP_ACCEPT"
199 46&nbsp;echo PATH_INFO = "$PATH_INFO"
200 47&nbsp;echo PATH_TRANSLATED = "$PATH_TRANSLATED"
201 48&nbsp;echo SCRIPT_NAME = "$SCRIPT_NAME"
202 49&nbsp;echo QUERY_STRING = "$QUERY_STRING"
203 50&nbsp;echo REMOTE_HOST = $REMOTE_HOST
204 51&nbsp;echo REMOTE_ADDR = $REMOTE_ADDR
205 52&nbsp;echo REMOTE_USER = $REMOTE_USER
206 53&nbsp;echo AUTH_TYPE = $AUTH_TYPE
207 54&nbsp;echo CONTENT_TYPE = $CONTENT_TYPE
208 55&nbsp;echo CONTENT_LENGTH = $CONTENT_LENGTH
209 56&nbsp;
210 57&nbsp;exit 0
211 58&nbsp;
212 59&nbsp;# Here document to give short instructions.
213 60&nbsp;:&#60;&#60;-'_test_CGI_'
214 61&nbsp;
215 62&nbsp;1) Drop this in your http://domain.name/cgi-bin directory.
216 63&nbsp;2) Then, open http://domain.name/cgi-bin/test-cgi.sh.
217 64&nbsp;
218 65&nbsp;_test_CGI_</PRE
219 ></TD
220 ></TR
221 ></TABLE
222 ><HR></DIV
223 ><P
224 >For security purposes, it may be helpful to identify the IP
225 addresses a computer is accessing.</P
226 ><P
227 ><A
228 NAME="IPTABLES02"
229 ></A
230 ></P
231 ><DIV
232 CLASS="EXAMPLE"
233 ><HR><A
234 NAME="IPADDRESSES"
235 ></A
236 ><P
237 ><B
238 >Example 30-2. IP addresses</B
239 ></P
240 ><TABLE
241 BORDER="0"
242 BGCOLOR="#E0E0E0"
243 WIDTH="100%"
244 ><TR
245 ><TD
246 ><PRE
247 CLASS="PROGRAMLISTING"
248 > 1&nbsp;#!/bin/bash
249 2&nbsp;# ip-addresses.sh
250 3&nbsp;# List the IP addresses your computer is connected to.
251 4&nbsp;
252 5&nbsp;# Inspired by Greg Bledsoe's ddos.sh script,
253 6&nbsp;# Linux Journal, 09 March 2011.
254 7&nbsp;# URL:
255 8&nbsp;# http://www.linuxjournal.com/content/back-dead-simple-bash-complex-ddos
256 9&nbsp;# Greg licensed his script under the GPL2,
257 10&nbsp;#+ and as a derivative, this script is likewise GPL2.
258 11&nbsp;
259 12&nbsp;connection_type=TCP # Also try UDP.
260 13&nbsp;field=2 # Which field of the output we're interested in.
261 14&nbsp;no_match=LISTEN # Filter out records containing this. Why?
262 15&nbsp;lsof_args=-ni # -i lists Internet-associated files.
263 16&nbsp; # -n preserves numerical IP addresses.
264 17&nbsp; # What happens without the -n option? Try it.
265 18&nbsp;router="[0-9][0-9][0-9][0-9][0-9]-&#62;"
266 19&nbsp;# Delete the router info.
267 20&nbsp;
268 21&nbsp;lsof "$lsof_args" | grep $connection_type | grep -v "$no_match" |
269 22&nbsp; awk '{print $9}' | cut -d : -f $field | sort | uniq |
270 23&nbsp; sed s/"^$router"//
271 24&nbsp;
272 25&nbsp;# Bledsoe's script assigns the output of a filtered IP list,
273 26&nbsp;# (similar to lines 19-22, above) to a variable.
274 27&nbsp;# He checks for multiple connections to a single IP address,
275 28&nbsp;# then uses:
276 29&nbsp;#
277 30&nbsp;# iptables -I INPUT -s $ip -p tcp -j REJECT --reject-with tcp-reset
278 31&nbsp;#
279 32&nbsp;# ... within a 60-second delay loop to bounce packets from DDOS attacks.
280 33&nbsp;
281 34&nbsp;
282 35&nbsp;# Exercise:
283 36&nbsp;# --------
284 37&nbsp;# Use the 'iptables' command to extend this script
285 38&nbsp;#+ to reject connection attempts from well-known spammer IP domains.</PRE
286 ></TD
287 ></TR
288 ></TABLE
289 ><HR></DIV
290 ><P
291 >More examples of network programming:
292 <OL
293 TYPE="1"
294 ><LI
295 ><P
296 ><A
297 HREF="devproc.html#NPREF"
298 >Getting the time from
299 <I
300 CLASS="FIRSTTERM"
301 >nist.gov</I
302 ></A
303 ></P
304 ></LI
305 ><LI
306 ><P
307 ><A
308 HREF="devproc.html#NW001"
309 >Downloading a
310 URL</A
311 ></P
312 ></LI
313 ><LI
314 ><P
315 ><A
316 HREF="system.html#IPSCRIPT0"
317 >A GRE
318 tunnel</A
319 ></P
320 ></LI
321 ><LI
322 ><P
323 ><A
324 HREF="communications.html#PING0"
325 >Checking
326 if an Internet server is up</A
327 ></P
328 ></LI
329 ><LI
330 ><P
331 ><A
332 HREF="communications.html#ISSPAMMER"
333 >Example 16-41</A
334 ></P
335 ></LI
336 ><LI
337 ><P
338 ><A
339 HREF="contributed-scripts.html#ISSPAMMER2"
340 >Example A-28</A
341 ></P
342 ></LI
343 ><LI
344 ><P
345 ><A
346 HREF="contributed-scripts.html#WHX"
347 >Example A-29</A
348 ></P
349 ></LI
350 ><LI
351 ><P
352 ><A
353 HREF="devproc.html#DEVTCP"
354 >Example 29-1</A
355 ></P
356 ></LI
357 ></OL
358 >
359 </P
360 ><P
361 >See also the <A
362 HREF="system.html#NETWORKSYS1"
363 >networking commands</A
364 >
365 in the <A
366 HREF="system.html"
367 >System and
368 Administrative Commands</A
369 > chapter and the <A
370 HREF="communications.html"
371 >communications commands</A
372 > in
373 the <A
374 HREF="external.html"
375 >External Filters, Programs and
376 Commands</A
377 > chapter.</P
378 ></DIV
379 ><DIV
380 CLASS="NAVFOOTER"
381 ><HR
382 ALIGN="LEFT"
383 WIDTH="100%"><TABLE
384 SUMMARY="Footer navigation table"
385 WIDTH="100%"
386 BORDER="0"
387 CELLPADDING="0"
388 CELLSPACING="0"
389 ><TR
390 ><TD
391 WIDTH="33%"
392 ALIGN="left"
393 VALIGN="top"
394 ><A
395 HREF="procref1.html"
396 ACCESSKEY="P"
397 >Prev</A
398 ></TD
399 ><TD
400 WIDTH="34%"
401 ALIGN="center"
402 VALIGN="top"
403 ><A
404 HREF="index.html"
405 ACCESSKEY="H"
406 >Home</A
407 ></TD
408 ><TD
409 WIDTH="33%"
410 ALIGN="right"
411 VALIGN="top"
412 ><A
413 HREF="zeros.html"
414 ACCESSKEY="N"
415 >Next</A
416 ></TD
417 ></TR
418 ><TR
419 ><TD
420 WIDTH="33%"
421 ALIGN="left"
422 VALIGN="top"
423 ><TT
424 CLASS="FILENAME"
425 >/proc</TT
426 ></TD
427 ><TD
428 WIDTH="34%"
429 ALIGN="center"
430 VALIGN="top"
431 ><A
432 HREF="part5.html"
433 ACCESSKEY="U"
434 >Up</A
435 ></TD
436 ><TD
437 WIDTH="33%"
438 ALIGN="right"
439 VALIGN="top"
440 >Of Zeros and Nulls</TD
441 ></TR
442 ></TABLE
443 ></DIV
444 ></BODY
445 ></HTML
446 >