Linux.pm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package Net::OpenSSH::More::Linux;
  2. #ABSTRACT: Useful subcommands for linux machines
  3. use strict;
  4. use warnings;
  5. use parent 'Net::OpenSSH::More';
  6. use File::Slurper ();
  7. =head1 NAME
  8. Net::OpenSSH::More::Linux
  9. =head1 DESCRIPTION
  10. This module contains useful methods to complement the parent's when in use on
  11. all linux environments.
  12. =head1 ASSUMPTIONS
  13. This module assumes that both the local and remote machine are some variant of GNU/Linux.
  14. Don't use this if that's not the case.
  15. =cut
  16. ###################
  17. # PRIVATE METHODS #
  18. ###################
  19. my $get_addrs_for_iface = sub {
  20. my ( $self, $interface, $proto, $use_local ) = @_;
  21. $interface ||= $self->get_primary_adapter($use_local);
  22. $self->diag("Attempting to get $proto address for interface $interface");
  23. my $regex = $proto eq 'inet' ? '[\d\.]+' : '[\da-f:]+'; # Close enough
  24. my $cmd = "ip -f $proto addr show $interface scope global dynamic";
  25. my $ip = $use_local ? `$cmd` : $self->cmd($cmd);
  26. my @matches = $ip =~ m/$proto\s+($regex)/g;
  27. return @matches;
  28. };
  29. #######################
  30. # END PRIVATE METHODS #
  31. #######################
  32. =head2 METHODS
  33. =head3 B<get_primary_adapter>
  34. So, on linux, there's no "primary" adapter, just the "correct" adapter
  35. for whatever given route. As such, what's the best way to determine
  36. this?
  37. This is a method to guess the "best" device interface from /proc/net/route.
  38. How does it determine this? By the "metric" stat -- the lower the better,
  39. as the lower the cost, the higher the preference.
  40. If you have set the metric improperly, you'll get bad results, but that's
  41. nothing to do with the code here.
  42. Optionally accepts a truthy arg to indicate whether you want this for the
  43. local host instead of the remote host.
  44. =cut
  45. sub get_primary_adapter {
  46. my ( $self, $use_local ) = @_;
  47. my %interfaces;
  48. my $proc_route_path = $use_local ? File::Slurper::read_text('/proc/net/route') : $self->sftp->get_content('/proc/net/route');
  49. foreach my $line ( split( /\n/, $proc_route_path ) ) {
  50. # Iface Destination Gateway Flags RefCt Use Metric Mask MTU Wndow IRTT
  51. my ( $interface, $metric ) = $line =~ m/^(.+?)\s+[0-9A-F]{8}\s+[0-9A-F]{8}\s+\d+\s+\d+\s+\d+\s+(\d+)\s+[0-9A-F]{8}\s+\d+\s+\d+\s+\d+\s*$/;
  52. push @{ $interfaces{$metric} }, $interface if ( length $interface && defined $metric );
  53. }
  54. my $lowest_metric = ( sort keys %interfaces )[0];
  55. my $interface = $interfaces{$lowest_metric}->[0] if defined $lowest_metric && $interfaces{$lowest_metric};
  56. return $interface || 'eth0';
  57. }
  58. =head2 get_remote_ips
  59. Returns HASH of the IPv4 & IPv6 SLAAC addresses of an optionally provided interface.
  60. If no interfaces is provided, use the default interface.
  61. CAVEATS: This uses the 'ip' tool, so if your system is too old for this, perhaps consider
  62. writing your own getter for local IPs.
  63. =cut
  64. sub get_remote_ips {
  65. my ( $self, $interface ) = @_;
  66. return (
  67. 'v4' => [ $get_addrs_for_iface->( $self, $interface, 'inet' ) ],
  68. 'v6' => [ $get_addrs_for_iface->( $self, $interface, 'inet6' ) ],
  69. );
  70. }
  71. =head2 get_local_ips
  72. Returns HASH of the IPv4 & IPv6 SLAAC addresses of an optionally provided interface.
  73. If no interfaces is provided, use the default interface.
  74. This one fetches it from the local machine and not the remote host, as sometimes
  75. that can be useful (say in the context of a test where you need this info).
  76. Same caveats that exist for get_remote_ips apply here.
  77. =cut
  78. sub get_local_ips {
  79. my ( $self, $interface ) = @_;
  80. return (
  81. 'v4' => [ $get_addrs_for_iface->( $self, $interface, 'inet', 1 ) ],
  82. 'v6' => [ $get_addrs_for_iface->( $self, $interface, 'inet6', 1 ) ],
  83. );
  84. }
  85. =head2 copy
  86. Effectively the same thing as `cp $SOURCE $DEST` on the remote server.
  87. =cut
  88. sub copy {
  89. my ( $self, $SOURCE, $DEST ) = @_;
  90. return $self->cmd( qw{cp -a}, $SOURCE, $DEST );
  91. }
  92. 1;