标签 perl 下的文章

作者: reistlin
来源: http://www.reistlin.com/blog/220
更新时间: 2011.04
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

perl -MMail::Sender -e 'Mail::Sender->printAuthProtocols("smtp.reistlin.com")';

#!/usr/bin/perl

use strict;
use Data::Dumper;
use Mail::Sender;

# debug switch
my $debug = 0;

my $sender = new Mail::Sender();

if ($sender->MailMsg({
	smtp => 'smtp.reistlin.com',
	from => '发件人@reistlin.com',
	to => '收件人@reistlin.com',
	subject => "subject",
	msg => "message",
	auth => 'LOGIN',
	authid => 'username',
	authpwd => 'password',
}) < 0) {
	die "$Mail::Sender::Error\n";
}

print "Mail Sent OK\n"

作者: reistlin
来源: http://www.reistlin.com/blog/174
更新时间: 2011.02
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl

# name: cpbak2 v1.0
# author: reistlin
# website: www.reistlin.com
# date: 2011.02.24

use strict;
use Data::Dumper;
use Time::Local;
use Getopt::Std;

# debug switch
my $debug = 0;


####################
# Configuration
####################

# defined current user home
my $home = $ENV{'HOME'};

# defined backup directory
my $path = "$home/backup";

# defined time tag
my $date = `date +%Y%m%d`;


####################
# Initial Directory
####################

if ( ! -e $path ) {
	system "mkdir -p $path";
}


####################
# Initial Getopt
####################

use vars qw($opt_c $opt_d);
getopts('cd');

# parameter "-c" backup to current directory
if ( $opt_c ) {
	$path = ".";
}

# parameter "-d" detail date mode "YYYY_MMDD_HHSS"
if ( $opt_d ) {
	$date = `date +%Y_%m%d_%H%m`;
}


####################
# Main Program
####################

# input target
my $target = shift;

# remove "\n"
chomp $date;

if ( $target ) {
	my $check = index($target, ".");

	if ( $check == -1 ) {
		print `cp -a $target $path/$target.$date`;
		exit;
	} else {
		$target =~ m/^(.+)\.(.+)$/;
		print `cp -a $target $path/$1.$date.$2`;
		exit;
	}
} else {
	exit;
}

作者: reistlin
来源: http://www.reistlin.com/blog/173
更新时间: 2011.02
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl

# name: cpbak v1.0
# author: reistlin
# website: www.reistlin.com
# date: 2011.02.22

use strict;
use Data::Dumper;
use Time::Local;

# debug switch
my $debug = 0;

my $name = shift;
my $date = `date +%Y%m%d`;

chomp $date;

if ( $name ) {
	my $check = index($name, ".");

	if ( $check == -1 ) {
        	print `cp -a $name $name.$date`;
        	exit;
	} else {
        	$name =~ m/^(.+)\.(.+)$/;
        	print `cp -a $name $1.$date.$2`;
        	exit;
	}
} else {
	exit;
}

作者: reistlin
来源: http://www.reistlin.com/blog/172
更新时间: 2011.02
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl

# name: install_lib v1.0
# author: reistlin
# website: www.reistlin.com
# date: 2011.02.22

use strict;
use Data::Dumper;
use Time::Local;

# debug switch
my $debug = 0;


####################
# Configuration
####################

# download website
my $download = "http://www.reistlin.com/download/linux";
# home
my $home = $ENV{'HOME'};
# prefix
my $prefix = "$home/lib";
# pkgs
my $pkgs = "$home/pkgs";
# build
my $build = "$pkgs/build";
# core
my $core = "4";


####################
# Library Packages
####################

my %lib = (
	curl => "curl-7.20.1.tar.gz",
	freetype => "freetype-2.4.4.tar.gz",
	gd => "gd-2.0.35.tar.gz",
	gettext => "gettext-0.18.1.1.tar.gz",
	jpeg => "jpeg-8c.tar.gz",
	libevent => "libevent-1.4.14b-stable.tar.gz",
	libiconv => "libiconv-1.13.1.tar.gz",
	libmcrypt => "libmcrypt-2.5.7.tar.gz",
	libpng => "libpng-1.4.4.tar.gz",
	libxml2 => "libxml2-2.7.8.tar.gz",
	mhash => "mhash-0.9.9.9.tar.bz2",
	pcre => "pcre-8.10.tar.gz",
	zlib => "zlib-1.2.5.tar.gz"
);


####################
# Initialization
####################

if ( ! -e $prefix ) {
	system "mkdir -p $prefix";
}
if ( ! -e $pkgs ) {
        system "mkdir -p $pkgs";
}
if ( ! -e $build ) {
        system "mkdir -p $build";
}


####################
# Function
####################

sub install {
        my %hash = @_;
        while ( my ($key, $value) = each %hash ) {
                $value =~ m/^([a-zA-Z]+)(.+)(\.tar\.gz|\.tar\.bz2)$/;

		print "\n";
		print "*** INSTALL [$value] ***\n";
		print "\n";

		if ( ! -e "$pkgs/$value" ) {
			system "wget -P $pkgs $download/$value";
		} else {
			print "[INFO] [$value] already exists in the local\n";
		}

		if ( $3 eq "\.tar\.gz" ) {
			system "tar zxvf $pkgs/$value -C $build";
		} elsif ( $3 eq "\.tar\.bz2" ) {
			system "tar jxvf $pkgs/$value -C $build";
		} else {
			print "[ERROR] unknown compression file [$value]\n";
			next;
		}

		# change directory
		chdir "$build/$1$2/";

		system "./configure --prefix=$prefix/$1$2";
		system "make -j $core";
		system "make install";
		system "ln -s $prefix/$1$2 $prefix/$1";

		print "\n";
		print "*** END ***\n";
		print "\n";
        }
}

####################
# Main Program
####################

&install(%lib);

作者: reistlin
来源: http://www.reistlin.com/blog/128
更新时间: 2011.01
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl

# name: last_logs v1.3
# author: reistlin
# website: www.reistlin.com
# date: 2011.02.28

use strict;
use Data::Dumper;
use Time::Local;

# debug switch
my $debug = 0;


####################
# Configuration
####################

# check logfile
my $check_file = "/home/reistlin/error.log";

# access.log = 1 / error.log = 2 / php_error.log = 3
my $check_type = 2;


####################
# Initialization
####################

my @check_array; my $check_day; my $check_hour; my $regular_day; my $regular_hour;

# set LANG=en_US.UTF-8
$ENV{'LANG'} = 'LANG=en_US.UTF-8';

if ( $check_type == 1 ) {
	# access.log
	$check_day = `date "+%d/%b/%Y"`;
	$check_hour = `date -d "1 hours ago" "+%H"`;
	chomp $check_day;
	chomp $check_hour;
	$regular_day = '\s\[(\d{2}\/[a-zA-Z]{3}\/\d{4})';
	$regular_hour = '\:(\d{2})\:\d{2}\:\d{2}\s';
} elsif ( $check_type == 2 ) {
	# error.log
	$check_day = `date "+%Y/%m/%d"`;
	$check_hour = `date -d "1 hours ago" "+%H"`;
	chomp $check_day;
	chomp $check_hour;
	$regular_day = '^(\d{4}\/\d{2}\/\d{2})';
	$regular_hour = '\s(\d{2})\:\d{2}\:\d{2}\s';
} elsif ( $check_type == 3 ) {
	# php_error.log
	$check_day = `date "+%d-%b-%Y"`;
	$check_hour = `date -d "1 hours ago" "+%H"`;
	chomp $check_day;
	chomp $check_hour;
	$regular_day = '^\[(\d{2}\-[a-zA-Z]{3}\-\d{4})';
	$regular_hour = '\s(\d{2})\:\d{2}\:\d{2}\]\s';
} else {
	print "[ERROR] Check logfile type error!\n";
	print "[ERROR] Please reset the logfile type!\n";
	print "[INFO] access.log = 1\n";
	print "[INFO] error.log = 2\n";
	print "[INFO] php_error.log = 3\n";
	exit;
}


####################
# Check logfile
####################

if ( ! -e $check_file ) {
	print "[Error] $check_file $!\n";
	exit;
}


####################
# Open logfile
####################

open(FILE, $check_file);

while (<FILE>) {
	$_ =~ m/$regular_day/;
	# check day
	if ( $1 eq $check_day ) {
		push(@check_array, $_);
	}
}

close(FILE);


####################
# Search logs
####################

if ( @check_array ) {
	foreach my $line (@check_array) {
		$line =~ m/$regular_hour/;
		# check hour
		if ( $1 eq $check_hour ) {
			print $line;
		}
	}
}

作者: reistlin
来源: http://www.reistlin.com/blog/53
更新时间: 2011.01
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl -w 

# Name: renamer v0.2
# Author: reistlin
# Website: www.reistlin.com
# Hotfix: bigyong
# Website: www.bigyong.com
# Date: 2011.01.10

use strict;
use Data::Dumper;

# Debug Switch
my $debug = 0;

# Defined "uppercase" or "lowercase"
# lowercase = 0 (default)
# uppercase = 1
my $file_case = "0";

# Defined File Type
my $file_type = "png";

# Defined Directory
my $file_dir = "/home/reistlin";

opendir(DIR, $file_dir) or die "[Error] [$file_dir] $!\n";
my @list = readdir(DIR);
closedir(DIR);

foreach my $tmp (@list) {
                # exclude "." or ".."
                next if ( $tmp =~ m/^\.+$/ );
                # full path
                my $fullpath = $file_dir . "/" . $tmp;
                # rename mode 
                if ( ! -d $fullpath ) {
                        if ( $fullpath =~ m/\.$file_type$/i ) {
                                if ( ( $tmp ne lc($tmp) ) && ( $file_case == 0 ) ) {
                                        rename $tmp, lc($tmp);
                                        print "[OK] Rename Success [lowercase]: $tmp\n";
                                }
                                if ( ( $tmp ne uc($tmp) ) && ( $file_case == 1 ) ) {
                                        rename $tmp, uc($tmp);
                                        print "[OK] Rename Success [uppercase]: $tmp\n";
                                }
                        }
                }
}

作者: reistlin
来源: http://www.reistlin.com/blog/33
更新时间: 2010.08
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl -w 

# Name: create md5 v1.0
# Author: reistlin
# Website: www.reistlin.com
# Hotfix: bigyong
# Website: www.bigyong.com
# Date: 2010.08.02

use strict;
use Data::Dumper;
use Digest::MD5 qw(md5 md5_hex md5_base64);


# Debug Switch
my $debug = 0;

# Start Time
my $time_1 = time();
# Clear
system "clear";


# Defined Original Hash File
my $original_file = "check_md5.log";
my %original_hash;

# Defined CheckList Directory
my $checklist_dir = "/home/reistlin";

# Defined Exclude Directory
my $exclude_tag = 0;
my @exclude_dir = ("/home/reistlin/exclude1", "/home/reistlin/exclude2");
my $exclude_key = join("|", @exclude_dir);


# Create MD5 Mode
sub createmd5 {
        my $path = shift;
        # clean path end "/"
        $path =~ s/\/+$//;

        opendir(DIR, $path) or die "[Error] Can not check directory [$path] \n";
        my @list = readdir(DIR);
        closedir(DIR);

        foreach my $tmp (@list) {
                # exclude "." or ".."
                next if ( $tmp =~ m/^\.+$/ );
                # full path
                my $path_sub = $path . "/" . $tmp;
                # exclude directory
		next if ( ( $path_sub =~ /$exclude_key/ ) && ( $exclude_tag == 1 ) );
                # subdirectory recursive
                if ( -d $path_sub ) {
                        &createmd5($path_sub);
                } else {
			$original_hash{$path_sub} = &md5sum($path_sub);
                }
        }
}


# MD5 Check Mode
sub md5sum {
        my $file = shift;
        open(FILE, $file) or die "[Error] Can not check file [$file] \n";
        binmode(FILE);

        my $md5 = Digest::MD5->new();
        $md5->addfile(*FILE);

        close(FILE);

        $md5->hexdigest;
}


# File Stat Mode
sub filestat {
        my $file = shift;
        my @file_stat = stat($file);
        my $mtime = localtime $file_stat[9];
        my $ctime = localtime $file_stat[10];

        print "\n";
        print "[$file] Modify: $mtime \n";
        print "[$file] Change: $ctime \n";
        print "[$file] UID: $file_stat[4] \n";
        print "[$file] GID: $file_stat[5] \n";
        print "[$file] Size: $file_stat[7] bytes \n";
        print "\n";
}


# Main Program
&createmd5($checklist_dir);


# Create MD5 logfile
open(LINE, "+>" . $original_file) or die "[Error] Can not create MD5 logfile [$original_file] \n";

while ( my ($file, $md5) = each %original_hash ) {
	print LINE "$md5 $file\n";
}

close(LINE);


# Print Result
if ( -e $original_file ) {
	print "Congratulations! MD5 logfile Create Succeed! \n";
	&filestat($original_file);
}


# End Time
my $time_2 = time();
my $time = $time_2 - $time_1;


# Script Runtime
print "Runtime \[$time\] Seconds ... \n";
print "End! Good Luck! :-) \n";
print "\n";

作者: reistlin
来源: http://www.reistlin.com/blog/32
更新时间: 2010.08
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

#!/usr/bin/perl -w 

# Name: check md5 v1.0
# Author: reistlin
# Website: www.reistlin.com
# Hotfix: bigyong
# Website: www.bigyong.com
# Date: 2010.08.02

use strict;
use Data::Dumper;
use Digest::MD5 qw(md5 md5_hex md5_base64);


# Debug Switch
my $debug = 0;

# Start Time
my $time_1 = time();
# Clear
system "clear";


# Defined Original Hash File
my $original_file = "./check_md5.log";
my %original_hash;

# Defined CheckList Directory
my $checklist_dir = "/home/reistlin";

# Defined Exclude Directory
my $exclude_tag = 0;
my @exclude_dir = ("/home/reistlin/exclude1", "/home/reistlin/exclude2");
my $exclude_key = join("|", @exclude_dir);

# Defined Result
my @md5_ok;
my @md5_no;
my @md5_bad;


# Check Hash File
if ( ! -e $original_file ) { 
	print "[Error] Can not open MD5 logfile [$original_file] \n"; 
	print "[Error] Please run the \[create_md5.pl\] to create original MD5 logfile \n"; 
	exit;
} else {
	# check file stat
	&filestat($original_file);

	# load hash file
	open(FILE, $original_file);

	while (<FILE>) {
		my $line = $_;
		if ( $line =~ m/(.+)\s+(.+?)\s*$/ ) {
			$original_hash{$2} = $1;
		}
	}
	close(FILE);
}


# List file Check MD5
sub checkmd5 {
	my $path = shift;
	# clean path end "/"
	$path =~ s/\/+$//;

	opendir(DIR, $path) or die "[Error] Can not check directory [$path] \n";
	my @list = readdir(DIR);
	closedir(DIR);

	foreach my $tmp (@list) {
		# exclude "." or ".."
		next if ( $tmp =~ m/^\.+$/ );
		# full path
		my $path_sub = $path . "/" . $tmp;
		# exclude directory
		next if ( ( $path_sub =~ /$exclude_key/ ) && ( $exclude_tag == 1 ) );
		# subdirectory recursive
		if ( -d $path_sub ) {
			&checkmd5($path_sub);
		} else {
			# check key
			if ( exists $original_hash{$path_sub} ) {
				# check md5 
				if ( $original_hash{$path_sub} eq &md5sum($path_sub) ) {
					push @md5_ok, $path_sub;
				} else {
					push @md5_bad, $path_sub;
				}
			} else {
				push @md5_no, $path_sub;
			}
		}
	}
}


# MD5 Check Mode
sub md5sum {
	my $file = shift;
	open(FILE, $file) or die "[Error] Can not check file [$file] \n";
	binmode(FILE);
	
	my $md5 = Digest::MD5->new();
	$md5->addfile(*FILE);
	
	close(FILE);
	
	$md5->hexdigest;
}


# File Stat Mode
sub filestat {
	my $file = shift;
	my @file_stat = stat($file);
	my $mtime = localtime $file_stat[9];
	my $ctime = localtime $file_stat[10];
	
	print "\n";
	print "[$file] Modify: $mtime \n"; 
	print "[$file] Change: $ctime \n";
	print "[$file] UID: $file_stat[4] \n";
	print "[$file] GID: $file_stat[5] \n";
	print "[$file] Size: $file_stat[7] bytes \n";
	print "\n";
}


# Print Array Mode
sub print_array {
	my @array = @_;
	foreach my $tmp (@array) {
		print "$tmp \n";
	}
}


# Main Program
&checkmd5($checklist_dir);


# Print Result Mode
my $scalar_ok = scalar(@md5_ok);
my $scalar_no = scalar(@md5_no);
my $scalar_bad = scalar(@md5_bad);

print "[OK MD5] $scalar_ok Files:\n";
&print_array(@md5_ok);
print "\n";

print "[NO MD5] $scalar_no Files:\n";
&print_array(@md5_no);
print "\n";

print "[BAD MD5] $scalar_bad Files:\n";
&print_array(@md5_bad);
print "\n";


# End Time
my $time_2 = time();
my $time = $time_2 - $time_1;

# Script Runtime
print "Runtime \[$time\] Seconds ... \n";
print "End! Good Luck! :-) \n";
print "\n";

作者: reistlin
来源: http://www.reistlin.com/blog/28
更新时间: 2010.07
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

1. Print Array - 打印数组

&print_array("数组")

#!/usr/bin/perl -w 

use strict;
use Data::Dumper;

# Debug Switch
my $debug = 0;

sub print_array {
        my @array = @_;
        my $index;
        for ( $index = 0; $index < @array; $index++ ) {
                print "$array[$index] \n";
        }
}

# 测试
my @test = (1,2,3,4,5,6,7,8,9,0);
&print_array(@test);

#!/usr/bin/perl -w 

use strict;
use Data::Dumper;

# Debug Switch
my $debug = 0;

sub print_array {
        my @array = @_;
        foreach my $tmp (@array) {
                print "$tmp \n";
        }
}

# 测试
my @test = (1,2,3,4,5,6,7,8,9,0);
&print_array(@test);

2. Print Hash - 打印哈希

&print_hash("哈希")

#!/usr/bin/perl -w 

use strict;
use Data::Dumper;

# Debug Switch
my $debug = 0;

sub print_hash {
        my %hash = @_;
        while ( my ($key, $value) = each %hash ) {
                print "$key => $value\n";
        }
}

# 测试
my %test = (a=>1, b=>2, c=>3);
&print_hash(%test);

3. Last Digits - 取字符串后"n"位

&last_digits("字符串", "位数")

#!/usr/bin/perl -w 

use strict;
use Data::Dumper;

# Debug Switch
my $debug = 0;

sub last_digits () {
        my $string = shift;
        my $digits = shift;
        my $length = length($string);
        if ($length > $digits) {
                print substr($string, $length - $digits, $digits);
        }
}

# 测试
&last_digits("987654321", "3");

作者: reistlin
来源: http://www.reistlin.com/blog/26
更新时间: 2010.06
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.pngcode.png

[下载] 1950年1月1日 - 2010年12月31日 [passwd.rar]

#!/usr/bin/perl -w 

# Name: birthday password dict v1.0
# Author: reistlin
# Website: www.reistlin.com
# Hotfix: bigyong
# Website: www.bigyong.com
# Date: 2010.06.30

use strict;
use Data::Dumper;

# Debug Switch
my $debug = 0;
# start time
my $time_1 = time();
# clear
system "clear";

my $year_local;
my $year_start;
my @year_array;

# defined year revised (60 year ago) 
my $year_revised = 60;
# defined result file 
my $result_file = "passwd.lis";
# defined result count
my $result_count = 0;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year_local = $year + 1900;
$year_start = $year_local - $year_revised;

# short year
my $year_short = substr $year_start, 2, 2;
for (; $year_short <= $year; $year_short++) {
	my $length = length ($year_short);
        if ($length > 2) {
		push (@year_array, (substr $year_short, $length - 2, 2));
	} else {
		push (@year_array, $year_short);
	}
}

# loop object
my @default_year = ("$year_start" .. "$year_local", @year_array);
my @default_month = ("1" .. "12", "01" .. "12");
my @default_day = ("1" .. "31", "01" .. "31");

# open result file
open (IN, "+>" . $result_file);

# save to result file
for (my $index_year=0; $index_year < @default_year; $index_year++) {
	for (my $index_month=0; $index_month < @default_month; $index_month++) {
		for (my $index_day=0; $index_day < @default_day; $index_day++) {
			print IN "$default_year[$index_year]$default_month[$index_month]$default_day[$index_day]\n";	
			$result_count++;
		}
	}
}

# check result file
if (-e $result_file) {
	print "[INFO] Success \[$result_file\] Saved ... \n";
}
 
# result file details
my @result_stat = stat ($result_file);
print "[INFO] Passwd Size: $result_stat[7] bytes \n";
print "[INFO] Passwd Count: $result_count \n";

# end time
my $time_2 = time();
my $time = $time_2 - $time_1;
# script runtime
print "[INFO] Runtime \[$time\] Second ... \n";
print "[INFO] End! Good Luck! :-) \n";