package Test::Backup; use strict; use warnings; use diagnostics; use Data::Dumper; use PAR 'module.par'; use Net::OpenSSH; use Git::Repository; use Expect; sub new{ my ($class,%args) = @_; my $self = { _username => $args{username}, _tftproot => $args{tftproot}, _password => $args{password}, _autoenable => $args{autoenable}, _tftpserver => $args{tftpserver}, _filename => $args{filename}, _timeout => $args{timeout}, _hostname => $args{hostname}, _path => $args{path}, _type => $args{type}, _tftpuser => $args{tftpuser}, _context => $args{config}, }; return bless($self,$class); }; sub connect{ my ($self) = @_; my $host = $self->{_hostname}; my %opts = ( user => $self->{_username}, password => $self->{_password}, master_opts => [ -o => "StrictHostKeyChecking false", -o => "HashKnownHosts false" ], ); my $ssh = Net::OpenSSH->new( $host, %opts ); my ( $pty, $pid ) = $ssh->open2pty(); return $pty; } sub do_tftp_backup{ my ($self,%args) = @_; my $tftpserver = $self->{_tftpserver}; my $filename = $self->{_filename}; my $path = $self->{_path}; my $context = $self->{_context}; my $cmd = "copy $context tftp://$tftpserver/$path/$filename"; my $expect = Expect->init($pty); $expect->send($cmd); $expect->expect( $self->{_timeout}, [ qr/Address or name of remote host/sm, sub { my $self = shift; $self->send("\r"); $self->set_accum( $self->after() ); exp_continue; } ], [ qr/Destination filename/sm, sub { my $self = shift; $self->send("\r"); $self->set_accum( $self->after() ); exp_continue; } ], [ qr/bytes copied/sm, sub { my $self=shift; $self->set_accum( $self->after() ); $self->soft_close(); } ], [ qr/Error/sm, sub { my $self = shift; $self->soft_close(); } ], ); return; }