Lecture 3 Perl & FreeBSD administration

2,501 views
2,395 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,501
On SlideShare
0
From Embeds
0
Number of Embeds
121
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture 3 Perl & FreeBSD administration

  1. 1. Mohammed Farragmfarrag@freebsd.orghttp://wiki.freebsd.org/MohammedFarrag<br />
  2. 2. Perl Programming Language.<br />FreeBSD Administration<br />Overview<br />
  3. 3. Every UNIX program in Perl should start with<br />#!/usr/bin/perl<br />Every variable should start with $ whatever it points to.<br />Arrays are manipulated using @ .<br />Hashes are manipulated using %.<br />Comments start with #.<br />Perl Programming Language<br />
  4. 4. Scalar is a number or string.<br />Examples<br /> $scalarint = 42;<br /> $scalarfp = 1.01;<br /> $scalarstr = "A string is a scalar";<br />Scalars<br />
  5. 5. An array is an indexed list of values with a consistent order.<br />Names of arrays are prefixed with @. <br />Indices counting from zero.<br />Examples<br /> @first_array = (1, 2, 3, 4); <br /> @second_array = ('one', '2', 'three', '4', '5');<br />Arrays<br />
  6. 6. Hashes, also called associative arrays, are tables of key-value pairs. <br />Names of hashes are prefixed with %. <br />Example:<br /> %hash = ('Mouse', 'Jerry', 'Cat', 'Tom', 'Dog', 'Spike');<br />%hash = (Mouse => 'Jerry', Cat => 'Tom', Dog => 'Spike');<br />You can access ‘Spike’ using $hash{Dog}<br />Hashes<br />
  7. 7. A reference is an immutable pointer to a value in memory, which may be any data type: scalar, array, hash, and so on.<br /> $scalarref = $scalar;<br /> $arrayref = @array;<br />To get to the original value, we dereference the reference by prefixing it with the symbol of the underlying data type.<br />$$reftostring;<br />@$arrayref;<br />References<br />
  8. 8.
  9. 9. A subroutine is a block that is declared for reuse with the sub keyword. <br />Examples<br /> sub red_october {<br /> print "A simple subn";<br /> }<br />To call this subroutine, we can now just write any of the following:<br />red_october;<br /> &red_october;<br />red_october();<br />Subroutine<br />
  10. 10. A package is a Perl namespace, a logical subdivision of compiled code in which variables and subroutines can reside.<br />The usekeyword by contrast loads a module at compile time, before other code is compiled, and calls an import routine in the just-loaded module to import variables and subroutines into the package of the caller. <br /> use File::Basename 'basename','dirname';<br />Packages<br />
  11. 11. Warnings<br /> Warnings can be enabled in three ways: through the -w option, the special variable $^W, or the use warnings pragma.<br />Strictness<br /> This enables additional checks that our code must pass at compile time before the interpreter will execute it. This is almost always a good thing to do.<br /> strict modes: vars, refs, and subs.<br />Warnings and Strictness<br />
  12. 12. The crypt function performs a one-way transform of the string passed; it is identical to (and implemented using) the C library crypt on Unix systems, which implements a variation of the Data Encryption Standard (DES) algorithm.<br />@characters = (0..9, 'a'..'z' ,'A'..'Z', '.', '/');<br />$encrypted = crypt($password, @characters[rand 64, rand 64]);<br />Password Encryption: crypt<br />
  13. 13. # check password<br />die "Wrong!" unless crypt($entered, $encrypted) eq $encrypted;<br />crypt – cont’d<br />
  14. 14.
  15. 15.
  16. 16.
  17. 17.
  18. 18.
  19. 19. $good_egg = "Humpty" . "Dumpty"; <br /> # produces "HumptyDumpty“<br />"abc" x 3; # produces 'abcabcabc'<br />String and List Operators<br />
  20. 20. Number of elements in an array using scalar<br /> $count = scalar(@array); <br />Last index in an array using $#<br />$highest = $#array; # $highest = scalar(@array)-1<br />Append an element to the array<br />$array[scalar(@array)] <br />push @array<br />Insert at the beginning<br />unshift @array<br />Truncating and resizing<br />@array = @array[0..3];<br />Array Manipulation<br />
  21. 21. first: Return the first element of the list for which a condition is true.<br />max, min: Return the highest and lowest numerical value, respectively.<br />maxstr, minstr: Return the highest and lowest alphabetical value, respectively.<br />sum: Return the sum of the list evaluated numerically.<br />shuffle: Return the list in a random order.<br />reduce: Reduce a list to a single value by arbitrary criteria.<br />Array Manipulation - cont’d<br />
  22. 22. Use <STDIN> to receive the input from the user.<br />Keyboard Handling<br />
  23. 23. Example<br /> 1 #!/usr/bin/perl<br /> 2 # Fig. 2.17: fig02_17.pl<br /> 3 # Using if statements with relational and equality operators<br /> 4 <br /> 5 print "Please enter first integer: ";<br /> 6 $number1 = <STDIN>;<br /> 7 chomp $number1;<br /> 8 <br /> 9 print "Please enter second integer: ";<br /> 10 $number2 = <STDIN>;<br /> 11 chomp $number2;<br /> 12 <br /> 13 print "The integers satisfy these relationships:n";<br /> 14 <br /> 15 if ( $number1 == $number2 ) {<br /> 16 print "$number1 is equal to $number2.n";<br /> 17 }<br /> 18 <br /> 19 if ( $number1 != $number2 ) {<br /> 20 print "$number1 is not equal to $number2.n";<br /> 21 }<br /> 22 <br /> 23 if ( $number1 < $number2 ) {<br /> 24 print "$number1 is less than $number2.n";<br /> 25 }<br /> 26 <br /> 27 if ( $number1 > $number2 ) {<br /> 28 print "$number1 is greater than $number2.n";<br /> 29 }<br /> 30 <br />
  24. 24. Array Looping Example<br /> 1 #!/usr/bin/perl<br /> 2 # Fig. 4.3: fig04_03.pl<br /> 3 # Looping through an array with the for repetition structure.<br /> 4 <br /> 5 @array = ( "Hello", 283, "there", 16.439 );<br /> 6 <br /> 7 # display every element of the array<br /> 8 for ( $i = 0; $i < 4; ++$i ) {<br /> 9 print "$i $array[ $i ]n";<br /> 10 }<br />0 Hello<br />1 283<br />2 there<br />3 16.439<br />
  25. 25. Data Hierarchy<br />Sally<br />Black<br />Tom<br />Blue<br />File<br />Judy<br />Green<br />Iris<br />Orange<br />Randy<br />Red<br />Record<br />Judy<br />Green<br />Field<br />J u d y<br />Byte (ASCII character J)<br />01001010<br />Bit<br />1<br />
  26. 26. Open<br />Seek<br />Truncate<br />Print<br />close<br />File Processing<br />
  27. 27.
  28. 28. File Manipulation<br /> 1 #!usr/bin/perl<br /> 2 # Fig. 10.6: fig10_06.pl<br /> 3 # Demonstrating writing to and reading from a file.<br /> 4 <br /> 5 use strict;<br /> 6 use warnings;<br /> 7 <br /> 8 print( "Opening file for outputn" );<br /> 9 open( OUTFILE, ">file.txt" ) <br /> 10 or die( "Can't find file.txt : $!" );<br /> 11 print( "Outputting to filen" );<br /> 12 print( OUTFILE "There was an old ladyn" );<br /> 13 close( OUTFILE ) or die( "Can not close file.txt: $!" );<br /> 14 <br /> 15 print "The file now contains:n";<br /> 16 open( INFILE, "file.txt" ) <br /> 17 or die( "Can not open file.txt: $!" );<br /> 18 print while ( <INFILE> );<br /> 19 close( INFILE ) or die( "Can not close file.txt: $!" );<br /> 20 <br /> 21 print( "nAppend to the end of the filen" );<br /> 22 open( OUTFILE, ">>file.txt" ) <br /> 23 or die( "Can not open file.txt: $!" );<br /> 24 print( OUTFILE "who lived in a shoe.n" );<br /> 25 close( OUTFILE ) or die( "Can not close file.txt: $!" );<br /> 26 <br /> 27 print( "It now reads:n" );<br />
  29. 29. 28 open( INFILE, "file.txt" ) <br /> 29 or die( "Can not open file.txt: $!" );<br /> 30 print while ( <INFILE> );<br /> 31 close( INFILE ) or die( "Can not close file.txt: $!" );<br />Opening file for output<br />Outputting to file<br />The file now contains:<br />There was an old lady<br /> <br />Append to the end of the file<br />It now reads:<br />There was an old lady<br />who lived in a shoe.<br />
  30. 30. Exclusive Locking Example<br /> 1 #!/usr/bin/perl<br /> 2 # Fig. 10.11: fig10_11.pl<br /> 3 # Logs visitors to web site.<br /> 4 <br /> 5 use strict;<br /> 6 use warnings;<br /> 7 use CGI qw( :standard );<br /> 8 use Fcntl qw( :flock );<br /> 9 <br /> 10 my @vars = <br /> 11 qw( REMOTE_ADDR REMOTE_PORT REQUEST_URI QUERY_STRING );<br /> 12 my @stuff = @ENV{ @vars };<br /> 13 my $info = join( " | ", @stuff );<br /> 14 <br /> 15 open( FILE, "+>>log.txt" )<br /> 16 or die( "Could not open log.txt: $!" );<br /> 17 flock( FILE, LOCK_EX )<br /> 18 or die( "Could not get exclusive lock: $!" );<br /> 19 print( FILE "$infonn" );<br /> 20 flock( FILE, LOCK_UN ) or die( "Could not unlock file: $!" );<br /> 21 <br /> 22 close( FILE );<br /> 23 <br /> 24 if ( $stuff[3] ne "" ) {<br /> 25 print( header( -Refresh=> '5; URL=http://www.google.com' ) );<br /> 26 print( start_html( "log.txt" ) );<br /> 27 print( h1( "Welcome to Deitel & Associates, $stuff[3]!n" ) );<br /> 28 print( p( i( "You will now be redirected to our home page." ) ) );<br /> 29 }<br />
  31. 31. The @ARGV array contains all the arguments that were passed to our program when it was started. <br />The definition of what defines an “argument” depends not on Perl, but on the shell that was used to start our program. However, in most cases spaces separate arguments from one another.<br />The @ARGV Array<br />
  32. 32. perlmyscript -u sventek -p cuckoo<br />Example<br />
  33. 33. # number of arguments passed<br /> scalar(@ARGV); <br /># highest element = no. of arguments -1<br /> $#ARGV; <br />
  34. 34. 1 #!/usr/bin/perl<br /> 2 # Fig. 11.2: fig11_02.pl<br /> 3 # A program that uses file tests<br /> 4 <br /> 5 use strict;<br /> 6 use warnings;<br /> 7 <br /> 8 foreachmy $file ( @ARGV ) {<br /> 9 print( "Checking $file: " );<br /> 10 <br /> 11 if ( -e $file ) { # does file exist?<br /> 12 print( "$file exists!n" );<br /> 13 <br /> 14 if ( -f $file ) { # is the file a plain file?<br /> 15 print( "The file $file is:" );<br /> 16 print( " executable" ) if ( -x $file ); # executable?<br /> 17 print( " readable" ) if ( -r $file ); # readable?<br /> 18 print( " writable" ) if ( -w $file ); # writable?<br /> 19 print( "n" );<br /> 20 print( "It is ", -s $file, " bytes.n" ); # size<br /> 21 my @time = timeconv( -A $file ); # accessed<br /> 22 print( "Last accessed at $time[0] days, ",<br /> 23 "$time[1] hours, $time[2] minutes ",<br /> 24 "and $time[3] seconds.n" );<br /> 25 @time = timeconv( -M $file ); # modified<br /> 26 print( "Last modified at $time[0] days, ",<br /> 27 "$time[1] hours, $time[2] minutes, ",<br /> 28 "and $time[3] seconds ago.n" );<br /> 29 }<br />
  35. 35. 2 ways for concurrency<br />Process<br />Each task has its own separate memory space<br />Less portable<br />Multithreading<br />Use the same memory process<br />The race condition<br />The timing of threads<br />Synchronization<br />Not possible with processes<br />Each knows when its data area is manipulated<br />Process Management<br />
  36. 36. ways to fork<br />Call the systems fork command<br />Not always available<br />Fork<br />Parent process<br />Calls the fork function<br />Child process<br />Created by the fork function<br />Each has a process id or pid<br />Parent returns child pid and undef if unsuccessful<br />Child returns 0<br />The fork command<br />
  37. 37. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.1: fig18_01.pl<br /> 3 # Using fork to create child processes.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 my ( $start, $middle, $end, $pid );<br /> 9 $| = 1;<br /> 10 <br /> 11 $start = time();<br /> 12 <br />A call is made to the fork function cloning the current process<br />13if ( $pid = fork() ) {<br /> 14 sleep( 1 );<br /> 15 print( "Parent executing.n" );<br /> 16 sleep( 2 );<br /> 17 print( "Parent finished.n" );<br /> 18 }<br />19elsif ( defined( $pid ) ) {<br />Executes in the child process<br /> 20 sleep( 1 );<br /> 21 print( "Child executing.n" );<br />22 sleep( 2 );<br />Sleep is used to slow the program down<br /> 23 print( "Child finished.n" );<br /> 24 exit();<br /> 25 }<br /> 26 else {<br /> 27 die( "Could not fork" );<br /> 28 }<br /> 29 <br />
  38. 38. 30 $middle = time();<br /> 31 <br /> 32 print( "That took " );<br /> 33 print( $middle - $start );<br />The time is used to compare using a process and not<br /> 34 print( " seconds with fork.n" );<br /> 35 <br /> 36 sleep( 3 );<br /> 37 sleep( 3 );<br />38$end = time();<br /> 39 <br /> 40 print( "That took " );<br /> 41 print( $end - $middle );<br /> 42 print( " seconds without fork.n" );<br /> 43 print( "Total of ", ( $end - $start ), " seconds.n" );<br />Parent executing.<br />Child executing.<br />Parent finished.<br />Child finished.<br />That took 3 seconds with fork.<br />That took 6 seconds without fork.<br />Total of 9 seconds.<br />
  39. 39. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.2: fig18_02.pl<br /> 3 # Demonstrates the wait function.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 my ( $pid, $pid2 );<br /> 9 $| = 1;<br /> 10 <br />Create two forks for a total of three processes<br />11if ( ( $pid = fork() ) && ( $pid2 = fork() ) ) {<br /> 12 print( "I have to wait for my kids.n" );<br /> 13 my $straggler = wait();<br />14 print( "Finally $straggler finished, now I can go.n" );<br /> 15 }<br />16elsif ( $pid && defined( $pid2 ) ) {<br />Will not print until all the children are done<br /> 17 sleep( 2 );<br /> 18 print( "Kid 2: So is mine...n" );<br /> 19 sleep( 4 );<br /> 20 exit();<br />Child 1<br /> 21 }<br />22elsif ( defined( $pid ) ) {<br /> 23 sleep( 1 );<br />Child 2<br /> 24 print( "Kid 1: My parent is patient...n" );<br /> 25 sleep( 2 );<br /> 26 }<br /> 27 else {<br /> 28 die( "Forking problems: " );<br /> 29 }<br />I have to wait for my kids.<br />Kid 1: My parent is patient...<br />Kid 2: So is mine...<br />Finally 296 finished, now I can go.<br />
  40. 40. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.3: fig18_03.pl<br /> 3 # Demonstrating the waitpid function.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 my ( $pid, $pid2 );<br /> 9 $| = 1;<br /> 10 <br />Since two fork methods are called then there will be 3 processes<br />11if ( ( $pid = fork() ) && ( $pid2 = fork() ) ) {<br /> 12 print( "I have to wait for my kids.n" );<br />13my $straggler = waitpid( $pid, 0 );<br /> 14 print( "Finally $straggler finished, now I can go.n" );<br /> 15 }<br /> 16 elsif ( $pid && defined( $pid2 ) ) {<br />The waitpid function will cause the the third process to wait until the first process is finished<br /> 17 sleep( 2 );<br /> 18 print( "Kid 2: Mine is not...n" );<br /> 19 sleep( 4 );<br /> 20 print( "Kid 2: Hey! Wait for me!!!n" );<br /> 21 exit();<br /> 22 }<br /> 23 elsif ( defined( $pid ) ) {<br /> 24 sleep( 1 );<br /> 25 print( "Kid 1: My parent is patient...n" );<br /> 26 sleep( 2 );<br /> 27 }<br /> 28 else {<br /> 29 die( "Forking problems: " );<br /> 30 }<br />
  41. 41. I have to wait for my kids.<br />Kid 1: My parent is patient...<br />Kid 2: Mine is not...<br />Finally 269 finished, now I can go.<br />Kid 2: Hey! Wait for me!!!<br />
  42. 42. The exec function<br />Used to execute other programs from a program<br />Uses system shell to invoke a program<br />Now program replaces current program’s process<br />When new program terminates so does the old one<br />Arguments passed is the program opened<br />All wild cards that work on the system are valid<br />If a list is passed<br />First is the program<br />The rest of the list is arguments to pass to that command without system processing<br />The system and exec functions<br />
  43. 43. The system function<br />Same as exec function<br />The only difference is that the process is returned to the program when the opened program terminates<br />This is done using a fork function and making one wait<br />The system and exec functions (II)<br />
  44. 44. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.4: fig18_04.pl<br /> 3 # Uses the system function to clear the screen.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 print( "What file would you like to create? " );<br />Used to determine the OS that the program is running in. Will only work in Windows or when the command clear works<br /> 9 chomp( my $file = <STDIN> );<br /> 10 <br />11( $^O =~ /Win/ ) ? system( "cls" ) : system( "clear" );<br /> 12 <br /> 13 print( "Type the text you wish to be in this file.n" );<br /> 14 print( "Type clear on a blank line to start over.n" );<br /> 15 print( "Type quit on a blank line when you are finished.n" );<br /> 16 open( FILE, ">$file" ) or die( "Cannot open: $!" );<br /> 17 <br />Creates a loop that will continue until the user enters “quit”<br /> 18while ( <STDIN> ) {<br /> 19 lastif ( /quit/ );<br /> 20 <br /> 21 if ( /clear/ ) {<br /> 22 ( $^O =~ /Win/ ) ? system( "cls" ) : system( "clear" );<br /> 23 print( "Type the text you wish to be in this file.n" );<br /> 24 print( "Type clear on a blank line to start over.n" );<br />Opens the file to write to<br /> 25 print( "Type quit on a blank line " );<br /> 26 print( "when you are finished.n" );<br />27 open( FILE, ">$file" ) or die( "Cannot open: $!" );<br /> 28 }<br />
  45. 45. 29 else {<br />Prints to the file<br />30 print( FILE );<br /> 31 }<br />Closes the file<br /> 32 }<br /> 33 <br />34close( FILE ) or die( "Cannot close: $!" );<br />What file would you like to create? hello.pl<br />Type the text you wish to be in this file.<br />Type clear on a blank line to start over.<br />Type quit on a blank line when you are finished.<br />#!/usr/bin/perl<br /># hello.pl<br /># Typical "hello world" program.<br /> <br />use warnings;<br />use strict;<br /> <br />print( "Hello World" );<br />quit<br />
  46. 46. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.6: fig18_06.pl<br /> 3 # Gets a web page and opens for editing.<br /> 4 <br /> 5 use warnings;<br />Will only open URLs that begin with http://<br /> 6 use strict;<br /> 7 use LWP::Simple;<br /> 8 <br />If the OS is windows then the program opens notepad, or else it will open vi<br /> 9 my( $URL, $filename ) = @ARGV;<br />Uses the exec function to open the desired program and file<br /> 10 <br />11if ( $URL !~ m#http://# ) {<br /> 12 $URL =~ s#^#http://#;<br /> 13 }<br /> 14 <br /> 15 if ( is_error( getstore( $URL, $filename ) ) ) {<br /> 16 die( "Could not get file: $!" );<br /> 17 }<br /> 18 <br />19my $program = ( $^O =~ /Win/ ? "notepad.exe" : "vi" );<br /> 20 <br />21exec( $program, $filename );<br />
  47. 47.
  48. 48.
  49. 49. The open function<br />Places a pipe symbol (|) in the string that specifies the command to be executed.<br />If at the front the input of a file is attached to a file handle<br />If at the end the output of a file is attached to a file handle<br />Write to the handle to modify the input<br />Read from the handle to modify the output<br />The open2 function<br />Allows the use of both STDIN and STDOUT<br />The open3 function<br />Allows the programmer to obtain the STDERR<br />Controlling the Input and Output of Processes<br />
  50. 50. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.7: fig18_07.pl<br /> 3 # Demonstrating using open to connect different processes.<br />Connects the STDOUT to the handle DIR<br /> 4 <br /> 5 use warnings;<br />Connects MORE to STDIN<br />Writes data to MORE<br /> 6 use strict;<br /> 7 <br />Closes the pipes<br />8open( DIR, "dir *.* |" ) or die( "Cannot open dir pipe: $!" );<br />9open( MORE, "| more" ) or die( "Cannot open more: $!" );<br /> 10 <br /> 11 while ( <DIR> ) {<br />12 print( MORE );<br /> 13 }<br /> 14 <br />15close( DIR ) or die( "Cannot close DIR: $!" );<br /> 16 close( MORE ) or die( "Cannot close MORE: $!" );<br />
  51. 51.
  52. 52. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.8: fig18_08.pl<br /> 3 # Demonstrates using backticks.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 my $date;<br /> 9 my $version;<br /> 10 <br />If the operating system is Windows then display the version and the date<br />11if ( $^O =~ /Win/ ) {<br /> 12 $version = ( `ver` );<br /> 13 $date = ( `date /t` );<br /> 14 }<br />15else {<br />If the OS is not Windows then use a different method to display its version and date<br /> 16 $version = ( `uname -r -s` );<br /> 17 $date = ( `date` );<br /> 18 }<br /> 19 <br /> 20 print( "The Operating system version is $version");<br /> 21 print( "n", $date );<br />The Operating system version is <br />Microsoft Windows 2000 [Version 5.00.2195]<br /> <br />Tue 11/28/2000 <br />The Operating system version is Linux 2.2.14-6.1.1<br /> <br />Tue Nov 28 06:26:43 EST 2000<br />
  53. 53. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.9: fig18_09.pl<br /> 3 # Demonstrating the open2 function.<br />Uses IPC::Open2 to allow for both input and output to be attached to a file handle<br /> 4 <br /> 5 use warnings;<br />Executes fig18_10.pl<br /> 6 use strict;<br />7use IPC::Open2;<br />Alters the output that fig18_10.pl would display<br /> 8 <br /> 9 $| = 1;<br /> 10 my $command = 'perl fig18_10.pl';<br /> 11 <br />12open2( *INPUT, *OUTPUT, $command ) or<br /> 13 die( "Cannot open: $!" );<br /> 14 <br /> 15 for ( 1 .. 20 ) {<br />16 print( OUTPUT $_, "n" );<br /> 17 my $read = <INPUT>;<br /> 18 print( $_, ' ', $read );<br /> 19 }<br /> 20 <br /> 21 close( OUTPUT ) or die( "Cannot close OUTPUT: $!" );<br /> 22 close( INPUT ) or die( "Cannot close INPUT: $!" );<br />
  54. 54. 1 MyItnkr.UtBnc<br />2 My6jwfxg7zFGE<br />3 My3G3DtIRpQEA<br />4 Myza94uYasdEE<br />5 MyCBteM4CIuTk<br />6 MyWRtG.HnvdL2<br />7 Myb8qLeBJ6TlM<br />8 MyE6k76L3NeoE<br />9 MyxpYOghD0TIE<br />10 MyI8Uhkz4HPjY<br />11 MyB3G7m.CDII6<br />12 MyugXM4mJTc1c<br />13 My9OUTHS6M.jg<br />14 My8UYE/LL8dgU<br />15 MyYAcz4F1r.to<br />16 MycPbNadoaNYo<br />17 MyXdTeyyclDNY<br />18 MyTm8RxjwtKO.<br />19 My2Q98KujB0i6<br />20 MyecGYzUxpdvk<br />
  55. 55. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.10: fig18_10.pl<br /> 3 # Helper to fig18_09.pl.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 $| = 1;<br /> 9 <br /> 10 while ( <STDIN> ) {<br />11 print( crypt( $_, "My cats' breath smells like cat food“ ));<br /> 12 print( "n" );<br /> 13 }<br />A simple program the displays this line for output after being encrypted<br />
  56. 56. The pipe function<br />Used to connect file handles of parent and children programs<br />Allows them to communicate to each other<br />Should set $| to flush after each output<br />Helps to prevent deadlock<br />Using open with filename “-”<br />Returns a handle that is a duplicate of STDIN or STDOUT<br />Communicating Between Processes<br />
  57. 57. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.11: fig18_11.pl<br /> 3 # Using pipe to communicate with child processes.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 use IO::Handle;<br /> 8 <br />Links the parent input with the child output, and visa versa<br />9pipe( CHILDINPUT, PARENTOUTPUT );<br /> 10 pipe( PARENTINPUT, CHILDOUTPUT );<br /> 11 <br /> 12 PARENTOUTPUT->autoflush( 1 );<br /> 13 CHILDOUTPUT->autoflush( 1 );<br /> 14 STDOUT->autoflush( 1 );<br /> 15 <br />16if ( my $pid = fork() ) {<br />Forks the program<br /> 17 <br />18for ( 1 .. 10 ) {<br />Loops ten times displaying its output and the child’s output<br /> 19 print( PARENTOUTPUT "$_n" );<br /> 20 print( "I said $_, " );<br /> 21 chomp( my $response = <PARENTINPUT> );<br /> 22 print( "and he said $response!n" );<br /> 23 }<br /> 24 <br />25 close( PARENTOUTPUT ) or<br />Closes the parent input and output handles<br /> 26 die( "Cannot close PARENTOUTPUT: $!" );<br /> 27 close( PARENTINPUT ) or<br /> 28 die( "Cannot close PARENTINPUT: $!" );<br /> 29 }<br />
  58. 58. The child code<br />30elsif ( defined( $pid ) ) {<br />31 close( PARENTOUTPUT ) or<br />Closes the parent input and output<br /> 32 die( "Cannot close PARENTOUTPUT: $!" );<br /> 33 close( PARENTINPUT ) or<br /> 34 die( "Cannot close PARENTINPUT: $!" );<br /> 35 <br />36while ( <CHILDINPUT> ) {<br />Prints out the parent output multiplied by 20<br /> 37 chomp();<br /> 38 print( CHILDOUTPUT $_ * 20, "n" );<br /> 39 }<br /> 40 <br />41 close( CHILDOUTPUT ) or<br />Closes the child input and output<br /> 42 die( "Cannot close CHILDOUTPUT: $!" );<br /> 43 close( CHILDINPUT ) or<br /> 44 die( "Cannot close CHILDINPUT: $!" );<br /> 45 exit();<br /> 46 }<br /> 47 else {<br /> 48 die( "Fork did not work" );<br /> 49 }<br /> 50 <br /> 51 print( "And that is the end of the conversation.n" );<br />I said 1, and he said 20!<br />I said 2, and he said 40!<br />I said 3, and he said 60!<br />I said 4, and he said 80!<br />I said 5, and he said 100!<br />I said 6, and he said 120!<br />I said 7, and he said 140!<br />I said 8, and he said 160!<br />I said 9, and he said 180!<br />I said 10, and he said 200!<br />And that is the end of the conversation.<br />
  59. 59. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.12: fig18_12.pl<br /> 3 # Using open to fork and filter output.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 <br /> 8 $| = 1;<br /> 9 <br />This will create a copy of the input in a file handle<br />10if ( my $pid = open( CHILD, "-|" ) ) {<br /> 11 my $i;<br /> 12 <br />Displays the line number<br />13while ( <CHILD> ) {<br /> 14 print( "Line ", ++$i, ": $_" );<br /> 15 }<br /> 16 <br /> 17 close( CHILD ) or die( "Cannot close: $!" );<br /> 18 }<br />Will output the lines synchronized with the line number output<br />19elsif ( defined( $pid ) ) {<br /> 20 print( "I am doing somen" );<br /> 21 print( "processing heren" );<br /> 22 print( "that producesn" );<br /> 23 print( "multiple linesn" );<br /> 24 print( "of output.n" );<br /> 25 exit();<br /> 26 }<br /> 27 else {<br /> 28 print( "Could not fork." );<br /> 29 }<br />Line 1: I am doing some<br />Line 2: processing here<br />Line 3: that produces<br />Line 4: multiple lines<br />Line 5: of output.<br />
  60. 60. Signals<br />Messages that are delivered to a program by the OS<br />Allow processes to communicate with one another<br />Signals are generated by several things<br />Errors<br />Events<br />User input<br />Internally from a program<br />Most signals can be overridden<br />SIGKILL and SIGSTOP cannot be overridden<br />Used as a last resort for programs that stop responding<br />Signals Handling<br />
  61. 61. Special Signals<br />__DIE__ (two underscores (_))<br />Used to override the die command<br />Not effective when first invoked<br />Prevents infinite recursion<br />__WARN__<br />Used to override the warn command<br />DEFAULT<br />Returns a signal to its original state<br />IGNORE<br />Ignores a signal<br />Signals Handling (II)<br />
  62. 62. Signal Handling<br />
  63. 63. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.14: fig18_14.pl<br /> 3 # Demonstrates using %SIG to define our own signal handlers.<br /> 4 <br /> 5 use warnings;<br />Every time SIGINT is used it calls the stop function<br /> 6 use strict;<br />Prints out Hooray! every second as long as $count does not equal zero<br /> 7 <br /> 8 my $count = 3;<br />Subtracts one from the counter and displays it<br /> 9 $| = 1;<br /> 10 <br />11$SIG{ 'INT' } = &stop;<br /> 12 <br />13while ( $count ) {<br /> 14 print( "Hooray!" );<br /> 15 print( "n" );<br /> 16 sleep( 1 );<br /> 17 }<br /> 18 <br /> 19 sub stop<br /> 20 {<br /> 21 $SIG{ 'INT' } = &stop;<br />22 $count--;<br /> 23 print( "$countn" );<br /> 24 <br /> 25 unless ( $count ) {<br /> 26 print( "Sorry, I did not want to stop...n" );<br /> 27 }<br /> 28 }<br />
  64. 64. Hooray!<br />Hooray!<br />2<br />Hooray!<br />Hooray!<br />Hooray!<br />1<br />Hooray!<br />0<br />Sorry, I did not want to stop...<br />
  65. 65. The kill function<br />Used to send signals to another process<br />First argument is the signal to be sent<br />Rest of the arguments are the processes to send the signal to<br />Terminating children<br />Leave behind zombies in process table<br />Allow parent to figure out if the child ended normally<br />Reaping is performed to eliminate zombies<br />Use the wait or waitpid functions to do additional operations<br />The IGNORE statement is used to remove the children from the table<br />Sending Signals<br />
  66. 66. 1 #!/usr/bin/perl<br /> 2 # Fig. 18.15: fig18_15.pl<br /> 3 # Sending signals to child processes using kill.<br /> 4 <br /> 5 use warnings;<br /> 6 use strict;<br /> 7 use POSIX qw( :signal_h :errno_h :sys_wait_h );<br /> 8 <br />9$SIG{ USR1 } = &user1;<br />Calls the desired functions<br /> 10 $SIG{ USR2 } = &user2;<br /> 11 $| = 1;<br /> 12 <br /> 13 sub user1 <br /> 14 {<br /> 15 $SIG{ USR1 } = &user1;<br />Displays when the signal is received<br />16 print( "I have received SIGUSR1.n" );<br /> 17 }<br /> 18 <br /> 19 sub user2 <br /> 20 {<br /> 21 $SIG{ USR2 } = &user2;<br /> 22 print( "I have received SIGUSR2.n" );<br /> 23 }<br /> 24 <br />
  67. 67. 25 if ( my $pid = fork() ) {<br />Sends a signal using the kill function<br />26 kill( 'USR1', $pid );<br /> 27 kill( 'USR2', $pid );<br /> 28 print( "I have just sent two signals.n" );<br /> 29 sleep( 2 );<br /> 30 print( "Now I am going to send a few more.n" );<br /> 31 kill( USR2 => $pid );<br /> 32 kill( USR1 => $pid );<br /> 33 print( "Parent done.n" );<br /> 34 }<br /> 35 elsif ( defined( $pid ) ) {<br /> 36 sleep( 5 );<br /> 37 print( "Kid done.n" );<br /> 38 exit();<br /> 39 }<br /> 40 else {<br /> 41 print( "Forking error...n" );<br /> 42 }<br /> 43 <br /> 44 wait();<br />fig18_15.pl<br />Program Output<br />I have just sent two signals.<br />I have received SIGUSR2.<br />I have received SIGUSR1.<br />Now I am going to send a few more.<br />Parent done.<br />I have received SIGUSR2.<br />I have received SIGUSR1.<br />Kid done.<br />
  68. 68. Install new port.<br />Mounting and Unmounting File Systems<br />Configure interface.<br />Soft/Hard Linking.<br />FreeBSD Administration<br />
  69. 69. Adding Packages<br />pkg_add -r lsof-4.56.4<br />Deleting Packages<br />pkg_delete xchat-1.7.1<br />Packages<br />
  70. 70. Makefile: The Makefile contains various statements that specify how the application should be compiled and where it should be installed on your system<br />distinfo file: This file contains information about the files that must be downloaded to build the port, and checksums, to ensure that those files have not been corrupted during the download.<br />files directory: This directory contains patches to make the program compile and install on your FreeBSD system. This directory may also contain other files used in building the port.<br />pkg-comment file: This is a one-line description of the program.<br />pkg-descr file: This is a more detailed, often multiple-line, description of the program.<br />pkg-plist file: This is a list of all the files that will be installed by the port. It also tells the ports system what files to remove upon deinstallation.<br />Port Installation<br />
  71. 71. <ul><li># whereislsoflsof</li></ul>/usr/ports/sysutils/lsof<br />Note: You must be the root user to install ports.<br /><ul><li># cd /usr/ports/sysutils/lsof</li></ul>Example<br />
  72. 72. make<br />>> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/. <br />>> Attempting to fetch from file:/cdrom/ports/distfiles/.<br />===> Extracting for lsof-4.57 ... [extraction output snipped] ... <br />>> Checksum OK for lsof_4.57D.freebsd.tar.gz. <br />===> Patching for lsof-4.57 <br />===> Applying FreeBSD patches for lsof-4.57 <br />===> Configuring for lsof-4.57 ... [configure output snipped] ... <br />===> Building for lsof-4.57 ... [compilation snipped] ... #<br />
  73. 73. # make install<br />===> Installing for lsof-4.57 ... [install routines snipped] ... <br />===> Generating temporary packing list <br />===> Compressing manual pages for lsof-4.57 <br />===> Registering installation for lsof-4.57<br />===> SECURITY NOTE: This port has installed the following binaries which execute with increased privileges. <br />
  74. 74. # cd /usr/ports/irc/lsof<br /># make deinstall<br />===> Deinstalling for lsof-4.57 <br />Removing Installed Ports<br />
  75. 75. /etc/fstab File<br />The /etc/fstab file contains a list of lines of the following format:<br /> device /mount-point fstype options dumpfreqpassn<br />mount-point: directory on which to mount the file system.<br />fstype: The file system type to pass to mount. The default FreeBSD file system is ufs.<br />Options: Either rw for read-write file systems, or ro for read-only file systems, followed by any other options that may be needed. A common option is noauto for file systems not normally mounted during the boot sequence. <br />dumpfreq: This is used by dump to determine which file systems require dumping. If the field is missing, a value of zero is assumed.<br />passno: This determines the order in which file systems should be checked. <br />Mounting and Unmounting File Systems<br />
  76. 76. In its most basic form, you use:<br /> # mount devicemountpoint<br />-a Mount all the file systems listed in /etc/fstab. Except those marked as “noauto”, excluded by the -t flag, or those that are already mounted.<br />-d Do everything except for the actual mount system call. This option is useful in conjunction with the -v flag to determine what mount is actually trying to do.<br />-f Force the mount of an unclean file system (dangerous), or forces the revocation of write access when downgrading a file system's mount status from read-write to read-only.<br />-r Mount the file system read-only. This is identical to using the ro argument to the -o option.<br />The mount command<br />
  77. 77. Through sysinstall -> configure -> Networking -> Interfaces<br />Use ifconfig command as follows:ifconfig interface inet IP<br /> Example:ifconfiglnc0 inet 202.54.1.22<br />Notes: <br /><ul><li>To setup up ip permanently open file /etc/rc.conf ; add/modify network entries:ee/etc/rc.conf
  78. 78. Setup values as follows:hostname="fbsdx.test.com"ifconfig_lnc0="inet 192.168.0.6 netmask 255.255.255.0"
  79. 79. Ethernet interface name can be Obtained using ifconfig -a command.
  80. 80. You should run /etc/netstartscript or /etc/rc.d/netif restart.</li></ul>Interface Configuration<br />
  81. 81. a hard link is a pointer to the file's i-node.<br /> a soft link, also called symbolic link, is a file that contains the name of another file. We can then access the contents of the other file through that name. That is, a symbolic link is like a pointer to the pointer to the file's contents.<br />Hard Links Vs. Soft Links<br />
  82. 82. <ul><li>Pointers to programs, files, or directories located elsewhere (just like Windows shortcuts)
  83. 83. If the original program, file, or directory is renamed, moved, or deleted, the soft link is broken. 
  84. 84. If you type ls -F you can see which files are soft links because they end with @
  85. 85. To create a soft link called myfilelink.txt that points to a file called myfile.txt, use this: </li></ul>ln-s myfile.txt myfilelink.txt<br />Soft links<br />
  86. 86. <ul><li>Pointers to programs and files, but NOT directories
  87. 87. If the original program or file is renamed, moved, or deleted, the hard link is NOT broken
  88. 88. Hard links cannot span disk drives, so you CANNOT have a hard link on /dev/hdb that refers to a program or file on /dev/had
  89. 89. Can’t connect files from different file systems.
  90. 90. To create a hard link called myhardlink.txt that points to a file called myfile.txt, use this: ln myfile.txt myhardlink.txt</li></ul>Hard links<br />
  91. 91. Hard Linking<br />
  92. 92. Soft Linking<br />
  93. 93. Questions<br />
  94. 94. Thank you<br />

×