Linux.pm 3.0 KB

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