Advertisement
m4ly

use Net::OpenSSH; SSH Backup

Jul 29th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.80 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. ###
  4. # Autor: Dawid Mocek
  5. # MZ Zabrze
  6. #
  7. #
  8. #
  9. use strict;
  10. use warnings;
  11.  
  12. # /usr/ports/net/p5-Net-OpenSSH
  13. use Net::OpenSSH;
  14.  
  15. # /usr/ports/net/p5-Net-Telnet
  16. use Net::Telnet
  17.  
  18. # Stand. w PERL5
  19. use POSIX qw/strftime/;
  20.  
  21. # /usr/ports/devel/p5-Config-IniFiles
  22. use Config::IniFiles;
  23.  
  24. use constant DATE => scalar strftime('%Y-%m-%d', localtime);
  25.  
  26. # Zastepuje . na - w stringu
  27. sub dot_replace {
  28.     my $string = $_[0];
  29.     $string =~ s/\./-/g;
  30.     return $string;
  31. }
  32.  
  33. # Zastepuje \s(spacja) i inne biale znaki w stringu
  34. sub space_replace {
  35.     my $string = $_[0];
  36.     $string =~ s/\s/_/g;
  37.     return $string;
  38. }
  39.  
  40. sub printn {
  41.     print $_[0] . "\n";
  42. }
  43.  
  44. sub dir_exists {
  45.     my $dir = $_[0];
  46.     if( -d $dir) {
  47.         return 0;
  48.         }
  49.     return -1;
  50. }
  51.  
  52. ###### W metodach capture(), capture2() itd  TRZEBA podać pelne sciezki do binarek #######
  53. sub ssh {
  54.     my $host = $_[0];
  55.     my $user = $_[1];
  56.     my $pass = $_[2];
  57.     #lc = lowercase
  58.     my $destdir = lc($_[3]);
  59.  
  60.     my $ssh = Net::OpenSSH->new($host, user => $user, passwd => $pass, timeout => 5, expand_vars => 1);
  61.     if($ssh) {
  62.         # Nazwa pliku wynikowego 7z na remote hoscie
  63.         my $filename = DATE . ".7z";
  64.  
  65.         # Pelna sciezka pliku wynikowego 7z na remote hoscie
  66.         my $filepath =  "/home/dawid/".$filename;
  67.  
  68.         # Komenda na remote hoscie
  69.         my $cmd = "/usr/local/bin/7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on ".$filepath." /home/dawid/*.jpg";
  70.  
  71.         # Wykonujmey komende. $out = StdOut zwrocony przez 7z na remote hoscie
  72.         my $out  = $ssh->capture($cmd);
  73.  
  74.         # Kod zwrocony przez proces 7z na remote hoscie
  75.         my $exit_code = $?;
  76.  
  77.         if($exit_code == 0 && ($out =~ m/Everything\sis\sOk/)) {
  78.             if(dir_exists($destdir) == 0) {
  79.  
  80.                 # Pobieramy backup z remote hosta - zwiekszyc timeout jesli plik jest jebitnie duzy
  81.                 my $scp_ret = $ssh->scp_get({timeout => 15}, $filepath, $destdir . '/'. $filename);
  82.  
  83.                 if($scp_ret) {
  84.                     # Kasujemy backup na remote hoscie
  85.                     $ssh->capture("/bin/rm " . $filepath);
  86.  
  87.                     printn("[SUCCESS][SSH][SCP_GET][" . $host . "] Remote file: '" .$filepath ."' saved in local dir: '" .$destdir ."'");
  88.                 }
  89.                 else {
  90.                     printn("[ERROR][SSH][SCP_GET][" . $host . "] Strange. Connection timeout or file: '".$filepath."' was suddenly removed or chmod problem. More: ". $ssh->error);
  91.                 }
  92.             }
  93.             else {
  94.                 printn("[ERROR][SSH][SCP_GET][" . $host . "] Destination dir: '". $destdir ."' does not exists !");
  95.             }
  96.         }
  97.         else {
  98.             printn("[ERROR][SSH][CAPTURE][" . $host . "] Cmd: '" .$cmd ."' returned code: " . $exit_code . " !");
  99.         }
  100.     }
  101.     else {
  102.         printn("[ERROR][SSH][CON][" . $host . "] ". $ssh->error);
  103.     }
  104.     # Usuwamy obiekt z pamieci(rozlaczamy sie przy okazji)
  105.     undef $ssh;
  106. }
  107.  
  108. sub telnet {
  109.     my $host = $_[0];
  110.     my $user = $_[1];
  111.     my $pass = $_[2];
  112. }
  113.  
  114. sub load_ini {
  115.     my $inifle = $_[0];
  116.     my %ini;
  117.     tie %ini, 'Config::IniFiles', (-file => $inifile, -fallback => "General");
  118.     return %ini;
  119. }
  120.  
  121.  
  122. ############# MAIN ######################
  123.  
  124. my $inifile = "/root/ips.ini";
  125.  
  126. my %ini = load_ini($inifile);
  127.  
  128. while(my($devicename, $data) = each(%ini)) {
  129.     my $proto =  $data->{"protocol"};
  130.     if($proto =~ m/^ssh$/) {
  131.         printn($proto ." to: " . $devicename);
  132.         ssh($data->{'host'}, $data->{'user'}, $data->{'password'}, $data->{'destdir'});
  133.     }
  134.     elsif($proto =~ m/^telnet$/) {
  135.         printn($proto . " to: " . $devicename);
  136.     }
  137.     else {
  138.         printn("unknow protocol: " . $proto . " in section: " .$devicename);
  139.     }
  140.  
  141. }
  142.  
  143. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement