github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/ssh.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	commonssh "github.com/mitchellh/packer/common/ssh"
     6  	packerssh "github.com/mitchellh/packer/communicator/ssh"
     7  	"golang.org/x/crypto/ssh"
     8  )
     9  
    10  // CommHost returns the VM's IP address which should be used to access it by SSH.
    11  func CommHost(state multistep.StateBag) (string, error) {
    12  	vmName := state.Get("vmName").(string)
    13  	driver := state.Get("driver").(Driver)
    14  
    15  	mac, err := driver.MAC(vmName)
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  
    20  	ip, err := driver.IPAddress(mac)
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  
    25  	return ip, nil
    26  }
    27  
    28  // SSHConfigFunc returns SSH credentials to access the VM by SSH.
    29  func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*ssh.ClientConfig, error) {
    30  	return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
    31  		auth := []ssh.AuthMethod{
    32  			ssh.Password(config.Comm.SSHPassword),
    33  			ssh.KeyboardInteractive(
    34  				packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
    35  		}
    36  
    37  		if config.Comm.SSHPrivateKey != "" {
    38  			signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
    39  			if err != nil {
    40  				return nil, err
    41  			}
    42  
    43  			auth = append(auth, ssh.PublicKeys(signer))
    44  		}
    45  
    46  		return &ssh.ClientConfig{
    47  			User: config.Comm.SSHUsername,
    48  			Auth: auth,
    49  		}, nil
    50  	}
    51  }