github.com/sneal/packer@v0.5.2/builder/virtualbox/common/ssh.go (about) 1 package common 2 3 import ( 4 gossh "code.google.com/p/go.crypto/ssh" 5 "fmt" 6 "github.com/mitchellh/multistep" 7 "github.com/mitchellh/packer/communicator/ssh" 8 "io/ioutil" 9 "os" 10 ) 11 12 func SSHAddress(state multistep.StateBag) (string, error) { 13 sshHostPort := state.Get("sshHostPort").(uint) 14 return fmt.Sprintf("127.0.0.1:%d", sshHostPort), nil 15 } 16 17 func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) { 18 return func(state multistep.StateBag) (*gossh.ClientConfig, error) { 19 auth := []gossh.ClientAuth{ 20 gossh.ClientAuthPassword(ssh.Password(config.SSHPassword)), 21 gossh.ClientAuthKeyboardInteractive( 22 ssh.PasswordKeyboardInteractive(config.SSHPassword)), 23 } 24 25 if config.SSHKeyPath != "" { 26 keyring, err := sshKeyToKeyring(config.SSHKeyPath) 27 if err != nil { 28 return nil, err 29 } 30 31 auth = append(auth, gossh.ClientAuthKeyring(keyring)) 32 } 33 34 return &gossh.ClientConfig{ 35 User: config.SSHUser, 36 Auth: auth, 37 }, nil 38 } 39 } 40 41 func sshKeyToKeyring(path string) (gossh.ClientKeyring, error) { 42 f, err := os.Open(path) 43 if err != nil { 44 return nil, err 45 } 46 defer f.Close() 47 48 keyBytes, err := ioutil.ReadAll(f) 49 if err != nil { 50 return nil, err 51 } 52 53 keyring := new(ssh.SimpleKeychain) 54 if err := keyring.AddPEMKey(string(keyBytes)); err != nil { 55 return nil, err 56 } 57 58 return keyring, nil 59 }