github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/parallels/common/ssh.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.google.com/p/go.crypto/ssh"
     7  	"github.com/mitchellh/multistep"
     8  	commonssh "github.com/mitchellh/packer/common/ssh"
     9  	packerssh "github.com/mitchellh/packer/communicator/ssh"
    10  )
    11  
    12  func SSHAddress(state multistep.StateBag) (string, error) {
    13  	vmName := state.Get("vmName").(string)
    14  	driver := state.Get("driver").(Driver)
    15  
    16  	mac, err := driver.Mac(vmName)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  
    21  	ip, err := driver.IpAddress(mac)
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  
    26  	return fmt.Sprintf("%s:22", ip), nil
    27  }
    28  
    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.SSHPassword),
    33  			ssh.KeyboardInteractive(
    34  				packerssh.PasswordKeyboardInteractive(config.SSHPassword)),
    35  		}
    36  
    37  		if config.SSHKeyPath != "" {
    38  			signer, err := commonssh.FileSigner(config.SSHKeyPath)
    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.SSHUser,
    48  			Auth: auth,
    49  		}, nil
    50  	}
    51  }