1. Trang chủ
  2. » Công Nghệ Thông Tin

Principles of Network and System Administration 2nd phần 10 pps

64 372 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 64
Dung lượng 421,33 KB

Nội dung

B.2 PERL 571 Similarly we can search for lines containing a string. Here is the grep program written in Perl #!/local/bin/perl # # grep as a perl program # # Check arguments etc while (<>) print if (/$ARGV[1]/); The operator /search-string/ returns true if the search string is a substring of the default variable $ . To search an arbitrary string, we write if (teststring =~ /search-string/); Here teststring is searched for occurrences of search-string and the result is true if one is found. In Perl you can use regular expressions to search for text patterns. Note however that, like all regular expression dialects, Perl has its own conventions. For example the dollar sign does not mean ‘match the end of line’ in Perl, instead one uses the \n symbol. Here is an example program which illustrates the use of regular expressions in Perl: #!/local/bin/perl # # Test regular expressions in perl # # NB - careful with $ * symbols etc. Use ’’ quotes since # the shell interprets these! # open (FILE,"regex_test"); $regex = $ARGV[$#ARGV]; # Looking for $ARGV[$#ARGV] in file while (<FILE>) if (/$regex/) print; 572 APPENDIX B. PROGRAMMING AND COMPILING This can be tested with the following patterns: .* prints every line (matches everything) . all lines except those containing only blanks (. doesn’t match ws/white-space) [a-z] matches any line containing lowercase [^a-z] matches any line containing something which is not lowercase a–z [A-Za-z] matches any line containing letters of any kind [0-9] match any line containing numbers #.* line containing a hash symbol followed by anything ^#.* line starting with hash symbol (first char) ;\n match line ending in a semi-colon Try running this program with the test data on the following file which is called regex test in the example program. # A line beginning with a hash symbol JUST UPPERCASE LETTERS just lowercase letters Letters and numbers 123456 123456 A line ending with a semi-colon; Line with a comment # COMMENT Generate WWW pages auto-magically The following program scans through the password database and builds a stan- dardized html-page for each user it finds there. It fills in the name of the user in each case. Note the use of the << operator for extended input, already used in the context of the shell. This allows us to format a whole passage of text, inserting variables at strategic places, and avoid having to print over many lines. #!/local/bin/perl # # Build a default home page for each user in /etc/passwd # # B.2 PERL 573 $true = 1; $false = 0; # First build an associated array of users and full names setpwent(); while ($true) { ($name,$passwd,$uid,$gid,$quota,$comment,$fullname) = getpwent; $FullName{$name} = $fullname; print "$name - $FullName{$name}\n"; last if ($name eq ""); } print "\n"; # Now make a unique filename for each page and open a file foreach $user (sort keys(%FullName)) { next if ($user eq ""); print "Making page for $user\n"; $outputfile = "$user.html"; open (OUT,"> $outputfile") || die "Can’t open $outputfile\n"; &MakePage; close (OUT); } #################################################################### sub MakePage { print OUT <<ENDMARKER; <HTML> <BODY> <HEAD><TITLE>$FullName{$user}’s Home Page</TITLE></HEAD> <H1>$FullName{$user}’s Home Page</H1> Hi welcome to my home page. In case you hadn’t got it yet my name is: $FullName{$user} I study at <a href=http://www.iu.hioslo.no>Oslo College</a>. </BODY> 574 APPENDIX B. PROGRAMMING AND COMPILING </HTML> ENDMARKER } Summary Perl is a superior alternative to the shell which has much of the power of C and is therefore ideal for simple and even more complex system programming tasks. A Perl program is more efficient than a shell script since it avoids large overheads associated with forking new processes and setting up pipes. The resident memory image of a Perl program is often smaller than that of a shell script when all of the sub-programs of a shell script are taken into account. We have barely scratched the surface of Perl here. If you intend to be a system administrator for Unix or NT systems, you could do much worse than to read the Perl book [316] and learn Perl inside out. B.3 WWW and CGI programming CGI stands for the Common Gateway Interface. It is the name given to scripts which can be executed from within pages of the World Wide Web. Although it is possible to use any language in CGI programs (hence the word ‘common’), the usual choice is Perl, because of the ease with which Perl can handle text. The CGI interface is pretty unintelligent, in order to be as general as possible, so we need to do a bit of work in order to make scripts work. Permissions The key thing about the WWW which often causes a lot of confusion is that the WWW service runs with a user ID of nobody or www. The purpose of this is to ensure that no web user has the right to read or write files unless they are opened very explicitly to the world by the user who owns them. In order for files to be readable on the WWW, they must have file mode 644 and they must lie in a directory which has mode 755.InorderforaCGIprogram to be executable, it must have permission 755 and in order for such a program to write to a file in a user’s directory, it must be possible for the file to be created (if necessary) and everyone must be able to write to it. That means that files which are written to by the WWW must have mode 666 and must either exist already or lie in a directory with permission 777. 1 Protocols CGI script programs communicate with WWW browsers using a very simple protocol. It goes like this: • A web page sends data to a script using the ‘forms’ interface. Those data are concatenated into a single line. The data in separate fields of a form are 1 You could also set the sticky bit 1777 in order to prevent malicious users from deleting your file. B.3 WWW AND CGI PROGRAMMING 575 separated by & signs. New lines are replaced by the text %0D%0A,whichisthe DOS ASCII representation of a newline, and spaces are replaced by + symbols. • A CGI script reads this single line of text on the standard input. • The CGI script replies to the web browser. The first line of the reply must be a line which tells the browser what mime-type the data are sent in. Usually, a CGI script replies in HTML code, in which case the first line in the reply must be: Content-type: text/html This must be followed by a blank line. HTML coding of forms To start a CGI program from a web page we use a form which is a part of the HTML code enclosed with the parentheses <FORM method="POST" ACTION="/cgi-script-alias/program.pl"> </FORM> The method ‘post’ means that the data which get typed into this form will be piped into the CGI program via its standard input. The ‘action’ specifies which program you want to start. Note that you cannot simply use the absolute path of the file, for security reasons. You must use something called a ‘script alias’ to tell the web browser where to find the program. If you do not have a script alias defined for you personally, then you need to get one from your system administrator. By using a script alias, no one from outside your site can see where your files are located, only that you have a ‘cgi-bin’ area somewhere on your system. Within these parentheses, you can arrange to collect different kinds of input. The simplest kind of input is just a button which starts the CGI program. This has the form <INPUT TYPE="submit" VALUE="Start my program"> This code creates a button. When you click on it the program in your ‘action’ string gets started. More generally, you will want to create input boxes where you can type in data. To create a single-line input field, you use the following syntax: <INPUT NAME="variable-name" SIZE=40> This creates a single-line text field of width 40 characters. This is not the limit on the length of the string which can be typed into the field, only a limit on the amount which is visible at any time. It is for visual formatting only. The NAME field is used to identify the data in the CGI script. The string you enter here will be sent to the CGI script in the form variable-name=value of input Another type of input is a text area. This is a larger box where one can type in text on several lines. The syntax is <TEXTAREA NAME="variable-name" ROW=50 COLS=50> 576 APPENDIX B. PROGRAMMING AND COMPILING which means ‘create a text area of fifty rows by fifty columns with a prompt to the left of the box’. Again, the size has only to do with the visual formatting, not to do with limits on the amount of text which can be entered. As an example, let’s create a WWW page with a complete form which can be used to make a guest book, or order form. <HTML> <HEAD> <TITLE>Example form</TITLE> <! Comment: Mark Burgess, 27-Jan-1997 > <LINK REV="made" HREF="mailto:mark@iu.hioslo.no"> </HEAD> <BODY> <CENTER><H1>Write in my guest book </H1></CENTER> <HR> <CENTER><H2>Please leave a comment using the form below.</H2><P> <FORM method="POST" ACTION="/cgi-bin-mark/comment.pl"> Your Name/E-mail: <INPUT NAME="variable1" SIZE=40> <BR><BR> <P> <TEXTAREA NAME="variable2" cols=50 rows=8></TEXTAREA> <P> <INPUT TYPE=submit VALUE="Add message to book"> <INPUT TYPE=reset VALUE="Clear message"> </FORM> <P> </BODY> </HTML> The reset button clears the form. When the submit button is pressed, the CGI program is activated. Interpreting data from forms To interpret and respond to the data in a form, we must write a program which satisfies the protocol above, see section 2.6.5. We use Perl as a script language. The simplest valid CGI script is the following. #!/local/bin/perl # # Reply with proper protocol # B.3 WWW AND CGI PROGRAMMING 577 print "Content-type: text/html\n\n"; # # Get the data from the form # $input = <STDIN>; # # and echo them back # print $input, "\n Done! \n"; Although rather banal, this script is a useful starting point for CGI programming, because it shows you just how the input arrives at the script from the HTML form. The data arrive all in a single, enormously long line, full of funny characters. The first job of any script is to decode this line. Before looking at how to decode the data, we should make an important point about the protocol line. If a web browser does not get this ‘content-type’ line from the CGI script it returns with an error: 500 Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error. Error: HTTPd: malformed header from script www/cgi-bin/comment.pl Before finishing your CGI script, you will probably encounter this error several times. A common reason for getting the error is a syntax error in your script. If your program contains an error, the first thing a browser gets in return is not the ‘content-type’ line, but an error message. The browser does not pass on this error message, it just prints the uninformative message above. If you can get the above script to work, then you are ready to decode the data which are sent to the script. The first thing is to use Perl to split the long line into an array of lines, by splitting on &. We can also convert all of the + symbols back into spaces. The script now looks like this: #!/local/bin/perl # # Reply with proper protocol # 578 APPENDIX B. PROGRAMMING AND COMPILING print "Content-type: text/html\n\n"; # # Get the data from the form # $input = <STDIN>; # # and echo them back # print "$input\n\n\n"; $input =~ s/\+/ /g; # # Now split the lines and convert # @array = split(‘&’,$input); foreach $var ( @array ) { print "$var\n"; } print "Done! \n"; We now have a series of elements in our array. The output from this script is something like this: variable1=Mark+Burgess&variable2=%0D%0AI+just+called+to+say+ (wrap) %0D%0A hey+pig%2C+nothing%27s+working+out+the+way+I+planned variable1=Mark Burgess variable2=%0D%0AI just called to say (wrap) %0D%0A hey pig%2Cnothing%27s working out the way I planned Done! As you can see, all control characters are converted into the form %XX.Weshould now try to do something with these. Since we are usually not interested in keeping new lines, or any other control codes, we can simply null-out these with a line of the form $input =~ s/% //g; The regular expression % matches anything beginning with a percent symbol followed by two characters. The resulting output is then free of these symbols. We can then separate the variable contents from their names by splitting the input. Hereisthecompletecode: B.3 WWW AND CGI PROGRAMMING 579 #!/local/bin/perl # # Reply with proper protocol # print "Content-type: text/html\n\n"; # # Get the data from the form # $input = <STDIN>; # # and echo them back # print "$input\n\n\n"; $input =~ s/% //g; $input =~ s/\+/ /g; @array = split(‘&’,$input); foreach $var ( @array ) { print "$var<br>"; } print "<hr>\n"; ($name,$variable1) = split("variable1=",$array[0]); ($name,$variable2) = split("variable2=",$array[1]); print "<br>var1 = $variable1<br>"; print "<br>var2 = $variable2<br>"; print "<br>Done! \n"; and the output variable1=Mark+Burgess&variable2=%0D%0AI+just+called+to+say (wrap) + %0D%0A hey+pig%2C+nothing%27s+working+out+the+way+I+planned variable1=Mark Burgess variable2=I just called to say hey pig nothings working (wrap) out the way I planned 580 APPENDIX B. PROGRAMMING AND COMPILING var1 = Mark Burgess var2 = I just called to say hey pig nothings working out (wrap) the way I planned Done! [...]... 1994 [108 ] R Evard An analysis of unix system configuration Proceedings of the Eleventh Systems Administration Conference (LISA XI) (USENIX Association: Berkeley, CA), page 179, 1997 [109 ] R Evard and R Leslie Soft: a software environment abstraction mechanism Proceedings of the Eighth Systems Administration Conference (LISA VIII) (USENIX Association: Berkeley, CA), page 65, 1994 [ 110] Host factory software... Distributed Systems: Operations and Management (DSOM 2003), 2003 [52] M Burgess Theory of Network and System Administration J Wiley & Sons, Chichester, 2004 [53] M Burgess and G Canright Scalability of peer configuration management in partially reliable and ad hoc networks Proceedings of the VII IFIP/IEEE IM Conference on Network Management, page 293, 2003 [54] M Burgess, H Haugerud, T Reitan, and S Straumsnes... [83] M.S Cyganik System administration in the andrew file system Proceedings of the Workshop on Large Installation Systems Administration (USENIX Association: Berkeley, CA), page 67, 1988 [84] G.E da Silveria A configuration distribution system for heterogeneous networks Proceedings of the Twelfth Systems Administration Conference (LISA XII) (USENIX Association: Berkeley, CA), page 109 , 1998 [85] M Dagenais,... (Sun Microsystems) or Alpha (Digital/Compaq) chip sets • X11: The Unix windows system Appendix E Recommended reading 1 The Practice of System Administration, T Limoncelli and C Hogan, Addison Wesley, 2002 2 Unix System Administration Handbook, E Nemeth, G Synder, S Seebass and T.R Hein, Prentice Hall, 2001 3 Essential System Administration, Æ Frisch, O’Reilly & Assoc, 2002 4 Windows NT: User Administration, ... and a group of workstations and common resources like printers, and so on Many magazines think of enterprise management as the network model, but when people talk about enterprise management they are really thinking of small businesses with fairly uniform systems • FQHN: Fully qualified host name The name of a host which is a sum of its unqualified name and its domain name, e.g host.domain.country, of. .. Proceedings of the Large Installation System Administration Workshop (USENIX Association: Berkeley, CA, 1987), page 24, 1987 [9] E Anderson and D Patterson Extensible, scalable monitoring for clusters of computers Proceedings of the Eleventh Systems Administration Conference (LISA XI) (USENIX Association: Berkeley, CA), page 9, 1997 [10] P Anderson Managing program binaries in a heterogeneous unix network. .. Proceedings of the Eighth Systems Administration Conference (LISA VIII) (USENIX Association: Berkeley, CA), page 75, 1994 [99] R Elling and M Long User-setup: a system for custom configuration of user environments, or helping users help themselves Proceedings of the Sixth Systems Administration Conference (LISA VI) (USENIX Association: Berkeley, CA), page 215, 1992 [100 ] R Emmaus, T.V Erlandsen, and G.J... Proceedings of the Fifth Large Installation Systems Administration Conference (LISA V) (USENIX Association: Berkeley, CA), page 1, 1991 [11] P Anderson Effective use of personal workstation disks in an nfs network Proceedings of the Sixth Systems Administration Conference (LISA VI) (USENIX Association: Berkeley, CA), page 1, 1992 [12] P Anderson Towards a high level machine configuration system Proceedings of. .. Fisk Automating the administration of heterogeneous lans Proceedings of the Tenth Systems Administration Conference (LISA X) (USENIX Association: Berkeley, CA), page 181, 1996 [117] M Fletcher Doit: a network software management tool Proceedings of the Sixth Systems Administration Conference (LISA VI) (USENIX Association: Berkeley, CA), page 189, 1992 [118] S Forrest, S Hofmeyr, and A Somayaji 40:88,... normality ACM Transactions on Computing Systems, 20:125–160, 2001 [55] M Burgess and R Ralston Distributed resource administration using cfengine Software Practice and Experience, 27 :108 3, 1997 [56] M Burgess and F.E Sandnes Predictable configuration management in a randomized scheduling framework IFIP/IEEE 12th International Workshop on Distributed Systems: Operations and Management (DSOM 2001), page 293, . PROGRAMMING AND COMPILING </HTML> ENDMARKER } Summary Perl is a superior alternative to the shell which has much of the power of C and is therefore ideal for simple and even more complex system. with forking new processes and setting up pipes. The resident memory image of a Perl program is often smaller than that of a shell script when all of the sub-programs of a shell script are taken. scratched the surface of Perl here. If you intend to be a system administrator for Unix or NT systems, you could do much worse than to read the Perl book [316] and learn Perl inside out. B.3 WWW and CGI programming CGI

Ngày đăng: 13/08/2014, 22:21

TỪ KHÓA LIÊN QUAN