@@ -314,20 +314,43 @@ func TestInterfacesWithNetsh(t *testing.T) {
314
314
}
315
315
}
316
316
317
- func netshInterfaceIPv4ShowAddress (name string ) ([]string , error ) {
318
- out , err := runCmd ("netsh" , "interface" , "ipv4" , "show" , "address" , "name=\" " + name + "\" " )
319
- if err != nil {
320
- return nil , err
321
- }
317
+ func netshInterfaceIPv4ShowAddress (name string , netshOutput []byte ) []string {
322
318
// adress information is listed like:
319
+ //
320
+ //Configuration for interface "Local Area Connection"
321
+ // DHCP enabled: Yes
323
322
// IP Address: 10.0.0.2
324
323
// Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0)
325
324
// IP Address: 10.0.0.3
326
325
// Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0)
326
+ // Default Gateway: 10.0.0.254
327
+ // Gateway Metric: 0
328
+ // InterfaceMetric: 10
329
+ //
330
+ //Configuration for interface "Loopback Pseudo-Interface 1"
331
+ // DHCP enabled: No
332
+ // IP Address: 127.0.0.1
333
+ // Subnet Prefix: 127.0.0.0/8 (mask 255.0.0.0)
334
+ // InterfaceMetric: 50
335
+ //
327
336
addrs := make ([]string , 0 )
328
337
var addr , subnetprefix string
329
- lines := bytes .Split (out , []byte {'\r' , '\n' })
338
+ var processingOurInterface bool
339
+ lines := bytes .Split (netshOutput , []byte {'\r' , '\n' })
330
340
for _ , line := range lines {
341
+ if ! processingOurInterface {
342
+ if ! bytes .HasPrefix (line , []byte ("Configuration for interface" )) {
343
+ continue
344
+ }
345
+ if ! bytes .Contains (line , []byte (`"` + name + `"` )) {
346
+ continue
347
+ }
348
+ processingOurInterface = true
349
+ continue
350
+ }
351
+ if len (line ) == 0 {
352
+ break
353
+ }
331
354
if bytes .Contains (line , []byte ("Subnet Prefix:" )) {
332
355
f := bytes .Split (line , []byte {':' })
333
356
if len (f ) == 2 {
@@ -351,18 +374,50 @@ func netshInterfaceIPv4ShowAddress(name string) ([]string, error) {
351
374
}
352
375
}
353
376
}
354
- return addrs , nil
377
+ return addrs
355
378
}
356
379
357
- func netshInterfaceIPv6ShowAddress (name string ) ([]string , error ) {
380
+ func netshInterfaceIPv6ShowAddress (name string , netshOutput []byte ) []string {
381
+ // adress information is listed like:
382
+ //
383
+ //Address ::1 Parameters
384
+ //---------------------------------------------------------
385
+ //Interface Luid : Loopback Pseudo-Interface 1
386
+ //Scope Id : 0.0
387
+ //Valid Lifetime : infinite
388
+ //Preferred Lifetime : infinite
389
+ //DAD State : Preferred
390
+ //Address Type : Other
391
+ //Skip as Source : false
392
+ //
393
+ //Address XXXX::XXXX:XXXX:XXXX:XXXX%11 Parameters
394
+ //---------------------------------------------------------
395
+ //Interface Luid : Local Area Connection
396
+ //Scope Id : 0.11
397
+ //Valid Lifetime : infinite
398
+ //Preferred Lifetime : infinite
399
+ //DAD State : Preferred
400
+ //Address Type : Other
401
+ //Skip as Source : false
402
+ //
403
+
358
404
// TODO: need to test ipv6 netmask too, but netsh does not outputs it
359
- out , err := runCmd ("netsh" , "interface" , "ipv6" , "show" , "address" , "interface=\" " + name + "\" " )
360
- if err != nil {
361
- return nil , err
362
- }
405
+ var addr string
363
406
addrs := make ([]string , 0 )
364
- lines := bytes .Split (out , []byte {'\r' , '\n' })
407
+ lines := bytes .Split (netshOutput , []byte {'\r' , '\n' })
365
408
for _ , line := range lines {
409
+ if addr != "" {
410
+ if len (line ) == 0 {
411
+ addr = ""
412
+ continue
413
+ }
414
+ if string (line ) != "Interface Luid : " + name {
415
+ continue
416
+ }
417
+ addrs = append (addrs , addr )
418
+ addr = ""
419
+ continue
420
+ }
366
421
if ! bytes .HasPrefix (line , []byte ("Address" )) {
367
422
continue
368
423
}
@@ -383,9 +438,9 @@ func netshInterfaceIPv6ShowAddress(name string) ([]string, error) {
383
438
f [0 ] = []byte (ParseIP (string (f [0 ])).String ())
384
439
}
385
440
386
- addrs = append ( addrs , string (bytes .ToLower (bytes .TrimSpace (f [0 ]) )))
441
+ addr = string (bytes .ToLower (bytes .TrimSpace (f [0 ])))
387
442
}
388
- return addrs , nil
443
+ return addrs
389
444
}
390
445
391
446
func TestInterfaceAddrsWithNetsh (t * testing.T ) {
@@ -395,6 +450,16 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) {
395
450
if ! isEnglishOS (t ) {
396
451
t .Skip ("English version of OS required for this test" )
397
452
}
453
+
454
+ outIPV4 , err := runCmd ("netsh" , "interface" , "ipv4" , "show" , "address" )
455
+ if err != nil {
456
+ t .Fatal (err )
457
+ }
458
+ outIPV6 , err := runCmd ("netsh" , "interface" , "ipv6" , "show" , "address" , "level=verbose" )
459
+ if err != nil {
460
+ t .Fatal (err )
461
+ }
462
+
398
463
ift , err := Interfaces ()
399
464
if err != nil {
400
465
t .Fatal (err )
@@ -431,14 +496,8 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) {
431
496
}
432
497
sort .Strings (have )
433
498
434
- want , err := netshInterfaceIPv4ShowAddress (ifi .Name )
435
- if err != nil {
436
- t .Fatal (err )
437
- }
438
- wantIPv6 , err := netshInterfaceIPv6ShowAddress (ifi .Name )
439
- if err != nil {
440
- t .Fatal (err )
441
- }
499
+ want := netshInterfaceIPv4ShowAddress (ifi .Name , outIPV4 )
500
+ wantIPv6 := netshInterfaceIPv6ShowAddress (ifi .Name , outIPV6 )
442
501
want = append (want , wantIPv6 ... )
443
502
sort .Strings (want )
444
503
0 commit comments