@@ -78,7 +78,7 @@ public static boolean nmcliIsInstalled() {
78
78
}
79
79
}
80
80
81
- private static List <NMDeviceInfo > allInterfaces = new ArrayList <>() ;
81
+ private static List <NMDeviceInfo > allInterfaces = null ;
82
82
private static long lastReadTimestamp = 0 ;
83
83
84
84
public static List <NMDeviceInfo > getAllInterfaces () {
@@ -88,35 +88,36 @@ public static List<NMDeviceInfo> getAllInterfaces() {
88
88
89
89
var ret = new ArrayList <NMDeviceInfo >();
90
90
91
- if (!Platform .isLinux ()) {
92
- // Can only determine interface name on Linux, give up
93
- return ret ;
94
- }
95
-
96
- try {
97
- var shell = new ShellExec (true , false );
98
- shell .executeBashCommand (
99
- "nmcli -t -f GENERAL.CONNECTION,GENERAL.DEVICE,GENERAL.TYPE device show" );
100
- String out = shell .getOutput ();
101
- if (out == null ) {
102
- return new ArrayList <>();
91
+ if (Platform .isLinux ()) {
92
+ String out = null ;
93
+ try {
94
+ var shell = new ShellExec (true , false );
95
+ shell .executeBashCommand (
96
+ "nmcli -t -f GENERAL.CONNECTION,GENERAL.DEVICE,GENERAL.TYPE device show" , true , false );
97
+ out = shell .getOutput ();
98
+ } catch (IOException e ) {
99
+ logger .error ("IO Exception occured when calling nmcli to get network interfaces!" , e );
103
100
}
104
- Pattern pattern =
105
- Pattern .compile ("GENERAL.CONNECTION:(.*)\n GENERAL.DEVICE:(.*)\n GENERAL.TYPE:(.*)" );
106
- Matcher matcher = pattern .matcher (out );
107
- while (matcher .find ()) {
108
- if (!matcher .group (2 ).equals ("lo" )) {
109
- // only include non-loopback devices
110
- ret .add (new NMDeviceInfo (matcher .group (1 ), matcher .group (2 ), matcher .group (3 )));
101
+ if (out != null ) {
102
+ Pattern pattern =
103
+ Pattern .compile ("GENERAL.CONNECTION:(.*)\n GENERAL.DEVICE:(.*)\n GENERAL.TYPE:(.*)" );
104
+ Matcher matcher = pattern .matcher (out );
105
+ while (matcher .find ()) {
106
+ if (!matcher .group (2 ).equals ("lo" )) {
107
+ // only include non-loopback devices
108
+ ret .add (new NMDeviceInfo (matcher .group (1 ), matcher .group (2 ), matcher .group (3 )));
109
+ }
111
110
}
112
111
}
113
- } catch (IOException e ) {
114
- logger .error ("Could not get active network interfaces!" , e );
115
112
}
116
-
117
- logger .debug ("Found network interfaces: " + ret );
118
-
119
- allInterfaces = ret ;
113
+ if (!ret .equals (allInterfaces )) {
114
+ if (ret .isEmpty ()) {
115
+ logger .error ("Unable to identify network interfaces!" );
116
+ } else {
117
+ logger .debug ("Found network interfaces: " + ret );
118
+ }
119
+ allInterfaces = ret ;
120
+ }
120
121
return ret ;
121
122
}
122
123
0 commit comments