@@ -461,15 +461,12 @@ func (d *Parallels9Driver) MAC(vmName string) (string, error) {
461
461
return mac , nil
462
462
}
463
463
464
- // IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
465
- //
466
464
// Parses the file /Library/Preferences/Parallels/parallels_dhcp_leases
467
465
// file contain a list of DHCP leases given by Parallels Desktop
468
466
// Example line:
469
467
// 10.211.55.181="1418921112,1800,001c42f593fb,ff42f593fb000100011c25b9ff001c42f593fb"
470
468
// IP Address ="Lease expiry, Lease time, MAC, MAC or DUID"
471
- func (d * Parallels9Driver ) IPAddress (mac string , vmName string ) (string , error ) {
472
-
469
+ func (d * Parallels9Driver ) ipWithLeases (mac string , vmName string ) (string , error ) {
473
470
if len (mac ) != 12 {
474
471
return "" , fmt .Errorf ("Not a valid MAC address: %s. It should be exactly 12 digits." , mac )
475
472
}
@@ -493,34 +490,52 @@ func (d *Parallels9Driver) IPAddress(mac string, vmName string) (string, error)
493
490
}
494
491
}
495
492
496
- if len ( mostRecentIP ) == 0 {
497
- log . Printf ( "IP lease not found for MAC address %s in: %s \n " , mac , d . dhcpLeaseFile )
493
+ return mostRecentIP , nil
494
+ }
498
495
499
- var stdout bytes.Buffer
500
- cmd := exec .Command (d .PrlctlPath , "list" , vmName , "--full" , "--no-header" , "-o" , "ip_configured" )
501
- cmd .Stdout = & stdout
502
- if err := cmd .Run (); err != nil {
503
- log .Printf ("Command run failed for Virtual Machine: %s\n " , vmName )
504
- return "" , err
505
- }
496
+ func (d * Parallels9Driver ) ipWithPrlctl (vmName string ) (string , error ) {
497
+ ip := ""
498
+ var stdout , stderr bytes.Buffer
499
+ cmd := exec .Command (d .PrlctlPath , "list" , vmName , "--full" , "--no-header" , "-o" , "ip_configured" )
500
+ cmd .Stdout = & stdout
501
+ cmd .Stderr = & stderr
502
+ if err := cmd .Run (); err != nil {
503
+ log .Printf ("Command run failed for Virtual Machine: %s\n " , vmName )
504
+ return "" , err
505
+ }
506
506
507
- stdoutString := strings .TrimSpace (stdout .String ())
508
- re := regexp .MustCompile (`([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)` )
509
- macMatch := re .FindAllStringSubmatch (stdoutString , 1 )
507
+ stderrString := strings .TrimSpace (stderr .String ())
508
+ if len (stderrString ) > 0 {
509
+ log .Printf ("stderr: %s" , stderrString )
510
+ }
510
511
511
- if len ( macMatch ) != 1 {
512
- return "" , fmt . Errorf ( "Unable to retrieve ip address of VM %s through tools \n " , vmName )
513
- }
512
+ stdoutString := strings . TrimSpace ( stdout . String ())
513
+ re := regexp . MustCompile ( `([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)` )
514
+ macMatch := re . FindAllStringSubmatch ( stdoutString , 1 )
514
515
515
- mostRecentIP = macMatch [0 ][1 ]
516
+ if len (macMatch ) != 1 {
517
+ return "" , fmt .Errorf ("Unable to retrieve ip address of VM %s through tools\n " , vmName )
516
518
}
517
519
518
- if len (mostRecentIP ) == 0 {
519
- return "" , fmt .Errorf ("IP address not found for this VM: %s\n " , vmName )
520
+ ip = macMatch [0 ][1 ]
521
+ return ip , nil
522
+ }
523
+
524
+ // IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
525
+ // If the MAC address is not found in the DHCP lease file, it will try to find ip using prlctl
526
+ func (d * Parallels9Driver ) IPAddress (mac string , vmName string ) (string , error ) {
527
+ ip , err := d .ipWithLeases (mac , vmName )
528
+ if (err != nil ) || (len (ip ) == 0 ) {
529
+ log .Printf ("IP lease not found for MAC address %s in: %s\n " , mac , d .dhcpLeaseFile )
530
+
531
+ ip , err = d .ipWithPrlctl (vmName )
532
+ if (err != nil ) || (len (ip ) == 0 ) {
533
+ return "" , fmt .Errorf ("IP address not found for this VM using prlctl: %s\n " , vmName )
534
+ }
520
535
}
521
536
522
- log .Printf ("Found IP lease: %s for MAC address %s\n " , mostRecentIP , mac )
523
- return mostRecentIP , nil
537
+ log .Printf ("Found IP lease: %s for MAC address %s\n " , ip , mac )
538
+ return ip , nil
524
539
}
525
540
526
541
// ToolsISOPath returns a full path to the Parallels Tools ISO for the specified guest
0 commit comments