Connector.pm 883 B

1234567891011121314151617181920212223242526272829303132333435
  1. use strict;
  2. use warnings;
  3. use Net::OpenSSH ();
  4. use Net::SFTP::Foreign ();
  5. use experimental 'signatures';
  6. # Cache the connections/objects internally
  7. my ( $ssh, $sftp );
  8. sub new ( $class, $host='127.0.0.1', $verbosity ) {
  9. my $obj = bless {
  10. 'ppid' => $$, # May not need this ultimately
  11. 'host' => $host,
  12. 'verbosity' => $verbosity
  13. }, $class;
  14. return $obj;
  15. }
  16. # Plenty o code duplication here, can probably golf down?
  17. sub ssh ($self) {
  18. return $ssh if $ssh;
  19. print "# Connecting to $self->{'host'} via Net::OpenSSH" if $self->{'verbosity'} >= 1;
  20. $ssh = Net::OpenSSH->new($self->{'host'});
  21. return $ssh;
  22. }
  23. sub sftp ($self) {
  24. return $sftp if $sftp;
  25. print "# Connecting to $self->{'host'} via Net::SFTP::Foreign" if $self->{'verbosity'} >= 1;
  26. $sftp = Net::SFTP::Foreign->new($self->{'host'});
  27. return $sftp;
  28. }
  29. 1;