System Programming and Administration

23
Perl Programming Perl Programming Course Course System Programming System Programming and Administration and Administration Krassimir Berov I-can.eu

description

This is the fourteenth (and last for now) set of slides from a Perl programming course that I held some years ago. I want to share it with everyone looking for intransitive Perl-knowledge. A table of content for all presentations can be found at i-can.eu. The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

Transcript of System Programming and Administration

Page 1: System Programming and Administration

Perl Programming Perl Programming CourseCourse

System Programming System Programming and Administrationand Administration

Krassimir Berov

I-can.eu

Page 2: System Programming and Administration

ContentsContents

1.1. Recommended ModulesRecommended Modules

2.2. Running perlRunning perl

3.3. Command-line switchesCommand-line switches

4.4. Filesystem analysisFilesystem analysis

5.5. Mail processingMail processing

6.6. Security notesSecurity notes

Page 3: System Programming and Administration

Recommended ModulesRecommended Modules

• In no particular orderIn no particular order• Archive::Extract - A generic archive extracting Archive::Extract - A generic archive extracting

mechanismmechanism

• Carp - warn of errors (from perspective of caller)Carp - warn of errors (from perspective of caller)

• Config - access Perl configuration informationConfig - access Perl configuration information

• Config::Extensions - hash lookup of which core Config::Extensions - hash lookup of which core extensions were built.extensions were built.

• CPAN - query, download and build perl modules from CPAN - query, download and build perl modules from CPAN sitesCPAN sites

• Cwd - get pathname of current working directoryCwd - get pathname of current working directory

• Data::Dumper - stringified perl data structures, Data::Dumper - stringified perl data structures, suitable for both printing and evalsuitable for both printing and eval

Page 4: System Programming and Administration

Recommended ModulesRecommended Modules

• In no particular orderIn no particular order• Dumpvalue - provides screen dump of Perl data.Dumpvalue - provides screen dump of Perl data.

• Encode - character encodingsEncode - character encodings

• ExtUtils::Install - install files from here to thereExtUtils::Install - install files from here to there

• ExtUtils::Installed - Inventory management of ExtUtils::Installed - Inventory management of installed modulesinstalled modules

• ExtUtils::Liblist - determine libraries to use and how ExtUtils::Liblist - determine libraries to use and how to use themto use them

• ExtUtils::MakeMaker - Create a module MakefileExtUtils::MakeMaker - Create a module Makefile

• File::Basename - Parse file paths into directory, File::Basename - Parse file paths into directory, filename and suffix.filename and suffix.

• File::Compare - Compare files or filehandlesFile::Compare - Compare files or filehandles

Page 5: System Programming and Administration

Recommended ModulesRecommended Modules

• In no particular orderIn no particular order• File::Copy - Copy files or filehandlesFile::Copy - Copy files or filehandles

• File::Find - Traverse a directory tree.File::Find - Traverse a directory tree.

• File::Path - create or remove directory treesFile::Path - create or remove directory trees

• File::Spec - portably perform operations on file File::Spec - portably perform operations on file namesnames

• File::stat - by-name interface to Perl's built-in stat() File::stat - by-name interface to Perl's built-in stat() functionsfunctions

• File::Temp - return name and handle of a temporary File::Temp - return name and handle of a temporary file safelyfile safely

• FindBin - Locate directory of original perl scriptFindBin - Locate directory of original perl script

Page 6: System Programming and Administration

Recommended ModulesRecommended Modules

• In no particular orderIn no particular order• Getopt::Long - Extended processing of command Getopt::Long - Extended processing of command

line optionsline options• Getopt::Std - Process single-character switches with Getopt::Std - Process single-character switches with

switch clusteringswitch clustering• Hash::Util - A selection of general-utility hash Hash::Util - A selection of general-utility hash

subroutinessubroutines• IO::* - supply object methods for * handlesIO::* - supply object methods for * handles• IPC::* - processes...IPC::* - processes...• List::Util - A selection of general-utility list List::Util - A selection of general-utility list

subroutinessubroutines• Locale::* - ISO codes, localization etc.Locale::* - ISO codes, localization etc.• MIME::Base64 - Encoding and decoding of base64 MIME::Base64 - Encoding and decoding of base64

stringsstrings

Page 7: System Programming and Administration

Recommended ModulesRecommended Modules

• In no particular orderIn no particular order• Net::* - network programmingNet::* - network programming

• POSIX - Perl interface to IEEE Std 1003.1POSIX - Perl interface to IEEE Std 1003.1

• Scalar::Util - A selection of general-utility scalar Scalar::Util - A selection of general-utility scalar subroutinessubroutines

• Storable - persistence for Perl data structuresStorable - persistence for Perl data structures

• Test::More - yet another framework for writing test Test::More - yet another framework for writing test scriptsscripts

• ......

Page 8: System Programming and Administration

Running perlRunning perl

• Run a perl program byRun a perl program by

• making it directly executablemaking it directly executable

• passing the name of the source file as an argument passing the name of the source file as an argument on the command lineon the command line

• Upon startup, Perl looks for your program in one of Upon startup, Perl looks for your program in one of the following places:the following places:

• Specified line by line via -e or -E switches on the Specified line by line via -e or -E switches on the command line.command line.

• Contained in the file specified by the first filename on Contained in the file specified by the first filename on the command line.the command line.

• Passed in implicitly via standard input. Passed in implicitly via standard input.

Page 9: System Programming and Administration

Running perlRunning perl

• Run a perl program byRun a perl program by

• making it directly executablemaking it directly executable

• passing the name of the source file as an argument passing the name of the source file as an argument on the command lineon the command line

• Upon startup, Perl looks for your program in one of Upon startup, Perl looks for your program in one of the following places:the following places:

• Specified line by line via -e or -E switches on the Specified line by line via -e or -E switches on the command line.command line.

• Contained in the file specified by the first filename on Contained in the file specified by the first filename on the command line.the command line.

• Passed in implicitly via standard input. Passed in implicitly via standard input.

Page 10: System Programming and Administration

Command-line switchesCommand-line switches

• execute switch (-e) execute switch (-e)

>perl -e 'print "Hello World\n" '>perl -e 'print "Hello World\n" '>perl -e "print qq{Hello World\n}" # Windows>perl -e "print qq{Hello World\n}" # Windows>perl -e 'for(1..20){print qq|hi\n|;}'>perl -e 'for(1..20){print qq|hi\n|;}'

Page 11: System Programming and Administration

Command-line switchesCommand-line switches

• Running code snippetsRunning code snippets

• This will tell you of syntax errors immediately, This will tell you of syntax errors immediately, but script execution will not start until you send but script execution will not start until you send Perl an end-of-file character, Perl an end-of-file character,

• On Unix systems - CTRL-D at the start of a lineOn Unix systems - CTRL-D at the start of a line

• under Windows -CTRL-Z at the start of a line.under Windows -CTRL-Z at the start of a line.

>perl >perl for(1..20){for(1..20){print 'hi'.$/print 'hi'.$/}}

Page 12: System Programming and Administration

Command-line switchesCommand-line switches

• Printing switch (-p)Printing switch (-p)

• tells Perl to act as a stream editortells Perl to act as a stream editor

• will read input from STDIN, or from files will read input from STDIN, or from files mentioned on the command line, and place each mentioned on the command line, and place each line of input into $_. line of input into $_.

• your program is executed, and the contents of $_ your program is executed, and the contents of $_ are printed. are printed.

• most commonly used with s///,most commonly used with s///,

>perl -pe 's/perl/Python/gi' filesed.txt >filesed1.txt>perl -pe 's/perl/Python/gi' filesed.txt >filesed1.txt

Page 13: System Programming and Administration

Command-line switchesCommand-line switches

• Module switch (-M)Module switch (-M)

• use modules in the one-lineruse modules in the one-liner

> perl -MData::Dumper -e 'print Dumper(\%ENV)'> perl -MData::Dumper -e 'print Dumper(\%ENV)'

Page 14: System Programming and Administration

Command-line switchesCommand-line switches

• In-place switch (-i)In-place switch (-i)

• allows you to edit the file in place, overwriting allows you to edit the file in place, overwriting the original versionthe original version

• a bug in your program can result in data-lossa bug in your program can result in data-loss

• provide an argument to the switch: -i.old to provide an argument to the switch: -i.old to create a backup copy of the original file file.old create a backup copy of the original file file.old and then overwrite the originaland then overwrite the original

> perl -i.old -pe 's/perl/python/gi' original.txt> perl -i.old -pe 's/perl/python/gi' original.txt

Page 15: System Programming and Administration

Command-line switchesCommand-line switches

• Check switch (-c)Check switch (-c)• check the program for syntactic errors and to check the program for syntactic errors and to

exit without executing the fileexit without executing the file

• Warnings switch (-w)Warnings switch (-w)• runs your program with warnings turned onruns your program with warnings turned on

• Include switch (-I)Include switch (-I)• additional directories to be searched for additional directories to be searched for

modulesmodules

• modifies Perl’s special @INC variable.modifies Perl’s special @INC variable.

> perl -c script.pl> perl -c script.pl

Page 16: System Programming and Administration

Filesystem analysisFilesystem analysis

• Directory separatorsDirectory separators

• use File::Spec to perform really OS use File::Spec to perform really OS independent operations on directoriesindependent operations on directories

• use IO::Dir for directories manipulation use IO::Dir for directories manipulation

• use Cwd to get the current working dir.use Cwd to get the current working dir.

use File::Spec::Functions qw(rel2abs);use File::Spec::Functions qw(rel2abs);#or#oruse File::Specuse File::Spec#later...#later...$tmpdir = File::Spec->tmpdir();$tmpdir = File::Spec->tmpdir();$is_case_tolerant = File::Spec->case_tolerant();$is_case_tolerant = File::Spec->case_tolerant();

Page 17: System Programming and Administration

Filesystem analysisFilesystem analysis

• Working with filesWorking with files• use File::Copy to copy and move filesuse File::Copy to copy and move files

• use unlink() to delete filesuse unlink() to delete files

• use File::Temp to safely create temporary use File::Temp to safely create temporary filesfiles

• use file-test operators to find information use file-test operators to find information about filesabout files

• see: perldoc -f -xsee: perldoc -f -xuse File::Copy;use File::Copy;copy("file1","file2") or die "Copy failed: $!";copy("file1","file2") or die "Copy failed: $!";copy("Copy.pm",\*STDOUT);copy("Copy.pm",\*STDOUT);move("/dev1/fileA","/dev2/fileB");move("/dev1/fileA","/dev2/fileB");

Page 18: System Programming and Administration

Filesystem analysisFilesystem analysis

• Working with filesWorking with files• use chmod() to change file permissionsuse chmod() to change file permissions

• use umask() to change default file use umask() to change default file permissionspermissions

• use chown() to change file ownershipuse chown() to change file ownership

• use File::Find to traverse directory trees use File::Find to traverse directory trees and recursively manipulate files in themand recursively manipulate files in them

use Fcntl;use Fcntl;umask 0022;umask 0022;sysopen(FILE, "runme", O_WRONLY|O_CREAT|O_EXCL, 0777);sysopen(FILE, "runme", O_WRONLY|O_CREAT|O_EXCL, 0777);#creates file with permissions 0755#creates file with permissions 0755

Page 19: System Programming and Administration

Mail processingMail processing

• use Net::SMTP to send simple mailuse Net::SMTP to send simple mail

• use MIME::Lite to send mail with attachmentsuse MIME::Lite to send mail with attachments

• use Net::POP3 to retrieve mailuse Net::POP3 to retrieve mail

• use Mail::Audit to filter your messagesuse Mail::Audit to filter your messages

use Net::POP3;use Net::POP3;# Constructors# Constructors$pop = Net::POP3->new('pop3host', Timeout => 60);$pop = Net::POP3->new('pop3host', Timeout => 60);if ($pop->login($username, $password) > 0) { if ($pop->login($username, $password) > 0) { my $msgnums = $pop->list; my $msgnums = $pop->list; # hashref of msgnum => size# hashref of msgnum => size foreach my $msgnum (keys %$msgnums) {foreach my $msgnum (keys %$msgnums) { #do something#do something }} }}$pop->quit;$pop->quit;

Page 20: System Programming and Administration

Security notesSecurity notes

• Taint checkingTaint checking

• rule: You may not use data derived from rule: You may not use data derived from outside your program to affect something outside your program to affect something else outside your program at least, not by else outside your program at least, not by accident.accident.

• all data that comes from external sources all data that comes from external sources is is taintedtainted

#!/usr/bin/perl -wT # Taint mode is enabled#!/usr/bin/perl -wT # Taint mode is enabled

Page 21: System Programming and Administration

Security notesSecurity notes

• Tainted data will be considered Tainted data will be considered unsuitable for certain operations:unsuitable for certain operations:• Executing system commandsExecuting system commands

• Modifying filesModifying files

• Modifying directoriesModifying directories

• Modifying processesModifying processes

• Invoking any shellInvoking any shell

• Performing a match in a regular expression Performing a match in a regular expression using the (?{ ... }) constructusing the (?{ ... }) construct

• Executing code using string evalExecuting code using string eval#!/usr/bin/perl -wT # Taint mode is enabled#!/usr/bin/perl -wT # Taint mode is enabled

Page 22: System Programming and Administration

System Programming and System Programming and AdministrationAdministration

• RessourcesRessources

• perldoc perlrunperldoc perlrun

• perldoc perlsecperldoc perlsec

• http://perltraining.com.au/notes/sysadmin.pdfhttp://perltraining.com.au/notes/sysadmin.pdf

• http://perltraining.com.au/notes/perlsec.pdfhttp://perltraining.com.au/notes/perlsec.pdf

• etc...etc...

Page 23: System Programming and Administration

System Programming and System Programming and AdministrationAdministration

Questions?Questions?