github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/null/ssh.go (about) 1 package null 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 ) 10 11 // SSHAddress returns a function that can be given to the SSH communicator 12 // for determining the SSH address 13 func SSHAddress(host string, port int) func(multistep.StateBag) (string, error) { 14 return func(state multistep.StateBag) (string, error) { 15 return fmt.Sprintf("%s:%d", host, port), nil 16 } 17 } 18 19 // SSHConfig returns a function that can be used for the SSH communicator 20 // config for connecting to the specified host via SSH 21 // private_key_file has precedence over password! 22 func SSHConfig(username string, password string, privateKeyFile string) func(multistep.StateBag) (*gossh.ClientConfig, error) { 23 return func(state multistep.StateBag) (*gossh.ClientConfig, error) { 24 25 if privateKeyFile != "" { 26 // key based auth 27 28 bytes, err := ioutil.ReadFile(privateKeyFile) 29 if err != nil { 30 return nil, fmt.Errorf("Error setting up SSH config: %s", err) 31 } 32 privateKey := string(bytes) 33 34 signer, err := gossh.ParsePrivateKey([]byte(privateKey)) 35 if err != nil { 36 return nil, fmt.Errorf("Error setting up SSH config: %s", err) 37 } 38 39 return &gossh.ClientConfig{ 40 User: username, 41 Auth: []gossh.AuthMethod{ 42 gossh.PublicKeys(signer), 43 }, 44 }, nil 45 } else { 46 // password based auth 47 48 return &gossh.ClientConfig{ 49 User: username, 50 Auth: []gossh.AuthMethod{ 51 gossh.Password(password), 52 gossh.KeyboardInteractive( 53 ssh.PasswordKeyboardInteractive(password)), 54 }, 55 }, nil 56 } 57 } 58 }