Chinaunix首页 | 论坛 | 博客
  • 博客访问: 67695
  • 博文数量: 13
  • 博客积分: 247
  • 博客等级: 二等列兵
  • 技术积分: 138
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-02 18:17
文章分类

全部博文(13)

文章存档

2015年(1)

2014年(1)

2013年(1)

2012年(2)

2011年(8)

我的朋友

分类: 系统运维

2011-08-16 10:12:11

Due to needs in code synchronization between test machine and product machine,an ftp uploading program using Net::FTP module was developed.The Program Reads in a config file,parses host and file configuration to process ftp file uploading.
 
  1. #! /usr/bin/perl -w

  2. ##############################################################################################
  3. ##########FileName:ftp_put.pl
  4. ##########Usage:perl ftp_put.pl configFileName logFileName
  5. ##########Copyright (c) 2011.8-2011.9 Zhu Song.
  6. ##########All rights reserved.
  7. ##########This program is free software.
  8. ##########you can redistribute it and/or modify it under the same terms as Perl itself.
  9. ##############################################################################################

  10. use strict;
  11. use IO::File;
  12. use Net::FTP;
  13. use Data::Dumper;

  14. my $LOGFILE;
  15. my $CONFIGFILE;
  16. my $log_fh;
  17. my $config_fh;
  18. my %profile;
  19. my $ftp_ref;

  20. sub usage()
  21. {
  22.     print STDOUT "Usage:perl ftp_put.pl configFileName logFileName\n";
  23.     exit 1;
  24. }

  25. sub parseInput()
  26. {
  27.     $CONFIGFILE = $ARGV[0];
  28.     $LOGFILE = $ARGV[1];
  29. }

  30. sub initializeLogFile()
  31. {
  32.     $LOGFILE or die "not initialized LOGFILE yet\n";
  33.     my ($sec,$min,$hour,$day,$mon,$year,$isdist) = localtime;
  34.     $LOGFILE = $LOGFILE."\.".($year + 1900).($mon + 1).($day + 1);
  35. }

  36. sub openLogFile()
  37. {
  38.     $log_fh = IO::File->new($LOGFILE,"w") or die "can't open $LOGFILE to write\n";
  39.     $log_fh->autoflush(1);
  40. }

  41. sub _log()
  42. {
  43.     my $text = shift;
  44.     $log_fh->write($text) or die "error happenned when writing logfile\n";
  45. }

  46. sub writeLog()
  47. {
  48.     my $logText = shift;
  49.     my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdist) = localtime;
  50.     my $time = sprintf("[%04d-%02d-%02d %02d:%02d:%2d]",$year + 1900,$mon + 1,$day + 1,$hour,$min,$sec);
  51.     &_log($time.$logText);
  52. }


  53. sub log_warn
  54. {
  55.     &writeLog ( "[". __LINE__ ."] " ." @_");
  56. }

  57. sub log_die
  58. {
  59.     &writeLog ( "[". __LINE__ ."] " ." @_");
  60. }

  61. sub checkHost
  62. {
  63.     my $host_ref = shift;
  64.   if( defined($host_ref->{'IP'}) && defined($host_ref->{'ACCOUNT'}) && defined($host_ref->{'PASSWORD'}) )
  65.   {
  66.       1;
  67.   }
  68.   else
  69.   {
  70.       0;
  71.   }
  72. }


  73. sub loadConfig()
  74. {
  75.      $config_fh = IO::File->new($CONFIGFILE,"r") or die "can't open configFile $CONFIGFILE to read:$!\n";
  76.      my $host_flag = 0;
  77.      my $file_flag = 0;
  78.      my $task_count = 0;
  79.      while(my $cfLine = <$config_fh>)
  80.      {
  81.           chomp($cfLine);
  82.           #######如果输入行是空行或者输入行不以[或者 开头,读入下一行
  83.           next if ($cfLine =~ m/^ *$/) || ( $cfLine !~ m/^\s+.*\S+/ && $cfLine !~ m/^[ \[]/);
  84.           if( $cfLine =~ m/^\[HOST\]/ )
  85.           {
  86.             if ($host_flag == 1)
  87.             {
  88.                 checkHost $profile{HOST} or die "host config initialization failed.\n";
  89.               $host_flag = 0;
  90.             }
  91.             else
  92.             {
  93.                 $host_flag = 1;
  94.             }
  95.           }
  96.           elsif($cfLine =~ m/^ / && $host_flag == 1)
  97.           {
  98.           my ($host_key,$host_value) = split(/=/,$cfLine);
  99.           $host_key =~ s/ *\[(\S+)\].*/$1/;
  100.           $host_value =~ s/ *(\S+) */$1/;
  101.           $profile{HOST}{$host_key} = $host_value;
  102.           &writeLog("$host_key:$host_value\n");
  103.           }
  104.           elsif($cfLine =~ m/^ / && $file_flag == 1)
  105.           {
  106.                my ($file_key,$file_value) = split(/=/,$cfLine);
  107.                $file_key =~ s/ *\[(\S+)\].*/$1/;
  108.                $file_value =~ s/\s+//g;
  109.                die "error while reading file config:\n" unless($file_key eq "PATH" or $file_key eq "FILELIST");
  110.                if( $file_key eq "PATH" )
  111.                {
  112.                     $task_count++;
  113.                     my ($source_dir,$remote_dir,$remote_bak) = split(/,/,$file_value);
  114.                     $profile{TASK}[$task_count - 1]{'SOURCEDIR'} = $source_dir;
  115.                     $profile{TASK}[$task_count - 1]{'REMOTEDIR'} = $remote_dir;
  116.                $profile{TASK}[$task_count - 1]{'REMOTEBAK'} = $remote_bak;
  117.                }
  118.                elsif ($file_key eq "FILELIST" and defined(@{$profile{TASK}}[$task_count - 1]))
  119.                {
  120.                @{${${profile{TASK}}[$task_count - 1 ]}{'FILELIST'}} = split(/,/,$file_value);
  121.                }
  122.                else
  123.                {
  124.                     print STDERR "file profile config error\n";
  125.                }
  126.                &writeLog("$file_key:$file_value\n");
  127.           }
  128.           elsif($cfLine =~ m/^\[FILE\]/)
  129.           {
  130.                if ($file_flag == 1)
  131.             {
  132.                 $file_flag = 0;
  133.             }
  134.             else
  135.             {
  136.                 $file_flag = 1;
  137.             }
  138.           }
  139.      }
  140. }

  141. sub connectToFTP
  142. {
  143.      $ftp_ref = Net::FTP->new($profile{HOST}->{'IP'}, Debug => 0) or die("Cannot connect to $profile{HOST}->{IP}: $@");
  144.       $ftp_ref->login($profile{HOST}->{'ACCOUNT'},$profile{HOST}->{'PASSWORD'}) or die("Cannot login ".$ftp_ref->message);
  145.       if(!$ftp_ref->binary)
  146.       {
  147.         &writeLog ( "[". __LINE__ ."] " .$ftp_ref->message);
  148.       }
  149.       &writeLog("successfully login to host $profile{HOST}->{'IP'}\taccount:$profile{HOST}->{'ACCOUNT'}\n");
  150. }

  151. sub remote_backup()
  152. {
  153.     my($remote_directory,$remote_backup,$file_ref) = @_;
  154.     $ftp_ref->cwd($remote_directory) or die("can't change remote working directory to $remote_directory\n");
  155.     foreach my $file(@{$file_ref})
  156.     {
  157.         if(!$ftp_ref->rename($file,$remote_backup."/".$file))
  158.         {
  159.             &writeLog("[". __LINE__ ."] ",$ftp_ref->message);
  160.             next;
  161.         }
  162.         else
  163.         {
  164.             &writeLog("successfully rename $file to $remote_backup/$file remotely\n");
  165.         }
  166.     }
  167.     &writeLog("remote backup done\n");
  168. }

  169. sub put_files()
  170. {
  171.     my($source_dir,$remote_directory,$remote_backup,$file_ref) = @_;
  172.     chdir($source_dir) or die("can't change local working directory to $source_dir\n");
  173.     $ftp_ref->cwd($remote_directory) or die("can't change remote working directory to $remote_directory\n");
  174.     foreach my $file(@{$file_ref})
  175.     {
  176.         if(!$ftp_ref->put($file,$remote_directory."/".$file))
  177.         {
  178.             &writeLog("[". __LINE__ ."] ",$ftp_ref->message);
  179.             next;
  180.         }
  181.         else
  182.         {
  183.             &writeLog("successfully put localfile $file to remote host $remote_directory/$file\n");
  184.         }
  185.     }
  186.     
  187. }

  188. sub disconnectFromFTP()
  189. {
  190.     $ftp_ref->quit;
  191.     &writeLog("ftp_put has done,disconnected from remote host:$profile{HOST}->{'IP'}\n");
  192. }

  193. sub proc_ftp
  194. {
  195.     foreach my $task_ref(@{$profile{TASK}})
  196.     {
  197.         my $source_dir = $task_ref->{'SOURCEDIR'};
  198.         my $remote_dir = $task_ref->{'REMOTEDIR'};
  199.         my $remote_bak = $task_ref->{'REMOTEBAK'};
  200.         my $fileList_ref = $task_ref->{'FILELIST'};
  201.         &remote_backup($remote_dir,$remote_bak,$fileList_ref);
  202.         &put_files($source_dir,$remote_dir,$remote_bak,$fileList_ref);
  203.     }
  204. }
  205. sub sendfile()
  206. {
  207.     &connectToFTP;
  208.     &proc_ftp;
  209.     &disconnectFromFTP;
  210. }

  211. usage if @ARGV != 2;
  212. parseInput;
  213. initializeLogFile;
  214. openLogFile;
  215. loadConfig;
  216. $SIG{__WARN__} = \&log_warn;
  217. $SIG{__DIE__} = \&log_die;
  218. print Dumper(\%profile);
  219. &sendfile;

 config文件配置如下:

 

  1. [HOST]
  2.    [IP] = 135.100.101.88
  3.    [ACCOUNT]= billapp1
  4.    [PASSWORD] = billapp1
  5. [HOST]

  6. [FILE]
  7.    [PATH] = /billapp1/appcj/songwolf/test,/billapp1/appcj/aioss/datafile/datafile/bak,/billapp1/appcj/aioss/datafile/datafile/baktest
  8.    [FILELIST] = test.c,test1.c,test2.c
  9.    [PATH] = /billapp1/appcj/songwolf/test/awk,/billapp1/appcj/aioss/datafile/datafile/bak,/billapp1/appcj/aioss/datafile/datafile/baktest
  10.    [FILELIST] = jieduan.sh,student.sh
  11. [FILE]

总结:

1.autovivification is very useful when dealing with programs that need to parse config file

2.this uploading program need to be improved to have system independence and more convinient usage

of config file.so,to be continued.....

阅读(1566) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

regansong2011-08-16 10:33:54

小松加油.