Leser: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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; }
1 2 3 4
use Test::Backup my $test = Test::Backup->new(%tmp_config); (das erzeugen der config hab ich weggelassen das funktioniert) $test->connect(); $test->do_tftp_backup();
$test->connect();
my $pty=$test->connect();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
sub connect{ my ($self) = @_; unless($self->{_pid}) { 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(); $self->{_pid}=$pid; $self->{_pty}=$pty; } return $self->{_pty}; }
Guest werCode (perl): (dl )my $pty=$test->connect();
aber ich würde "connect" etwas anders schreiben:
Code (perl): (dl )1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21sub connect{ my ($self) = @_; unless($self->{_pid}) { 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(); $self->{_pid}=$pid; $self->{_pty}=$pty; } return $self->{_pty}; }
Du willst doch nicht immer wieder eine neue Verbindung aufbauen, oder?
Dann kannst auch in "do_tftp_backup" schreiben:
my $pty=$self->connect;