Linux.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package Net::OpenSSH::More::Linux;
  2. use strict;
  3. use warnings;
  4. use parent 'Net::OpenSSH::More::Linux';
  5. use File::Slurper ();
  6. =head1 ASSUMPTIONS
  7. This module assumes that both the local and remote machine are some variant of GNU/Linux.
  8. =head2 METHODS
  9. =head3 B<get_primary_adapter>
  10. Method to retrieve the primary device interface from /proc/net/route
  11. Modified from /scripts/mainipcheck and t/lib/Test/GetPrimaryEth.pm
  12. Optionally accepts a truthy arg to indicate whether you want this for the
  13. local host instead of the remote host.
  14. =cut
  15. sub get_primary_adapter {
  16. my ( $self, $use_local ) = @_;
  17. my %interfaces;
  18. my $proc_route_path = do {
  19. if ($use_local) {
  20. File::Slurper::read_text('/proc/net/route');
  21. }
  22. else {
  23. $self->cmd( "cat /proc/net/route", 1 );
  24. }
  25. };
  26. foreach my $line ( split( /\n/, $proc_route_path ) ) {
  27. if ( $line =~ m/^(.+?)\s*0{8}\s.*?(\d+)\s+0{8}\s*(?:\d+\s*){3}$/ ) {
  28. my ( $interface, $metric ) = ( $1, $2 );
  29. push @{ $interfaces{$metric} }, $interface;
  30. }
  31. }
  32. my $lowest_metric = ( sort keys %interfaces )[0];
  33. my $interface = $interfaces{$lowest_metric}[0];
  34. return $interface || 'eth0';
  35. }
  36. =head2 get_remote_ips
  37. Returns HASH of the IPv4 & IPv6 SLAAC addresses of an optionally provided interface.
  38. If no interfaces is provided, use the default interface.
  39. CAVEATS: This uses the 'ip' tool, so if your system is too old for this, perhaps consider
  40. writing your own getter for local IPs.
  41. =cut
  42. sub get_remote_ips {
  43. my ( $self, $interface ) = @_;
  44. return (
  45. 'v4' => [ $get_addrs_for_iface->( $self, $interface, 'inet' ) ],
  46. 'v6' => [ $get_addrs_for_iface->( $self, $interface, 'inet6' ) ],
  47. );
  48. }
  49. =head2 get_local_ips
  50. Returns HASH of the IPv4 & IPv6 SLAAC addresses of an optionally provided interface.
  51. If no interfaces is provided, use the default interface.
  52. This one fetches it from the local machine and not the remote host, as sometimes
  53. that can be useful (say in the context of a test where you need this info).
  54. Same caveats that exist for get_remote_ips apply here.
  55. =cut
  56. sub get_local_ips {
  57. my ( $self, $interface ) = @_;
  58. return (
  59. 'v4' => [ $get_addrs_for_iface->( $self, $interface, 'inet', 1 ) ],
  60. 'v6' => [ $get_addrs_for_iface->( $self, $interface, 'inet6', 1 ) ],
  61. );
  62. }
  63. ###################
  64. # PRIVATE METHODS #
  65. ###################
  66. my $get_addrs_for_iface = sub {
  67. my ( $self, $interface, $proto, $use_local ) = @_;
  68. $interface ||= $self->get_primary_adapter($use_local);
  69. $self->diag("Attempting to get $proto address for interface $interface");
  70. my $regex = $proto eq 'inet' ? '[\d\.]+' : '[\da-f:]+'; # Close enough
  71. my $cmd = "ip -f $proto addr show $interface scope global dynamic";
  72. my $ip = $use_local ? `$cmd` : $self->cmd($cmd);
  73. my @matches = $ip =~ m/$proto\s+($regex)/g;
  74. return @matches;
  75. };
  76. #######################
  77. # END PRIVATE METHODS #
  78. #######################
  79. 1;