2014-03-24 06:20:32 -04:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
2014-04-26 14:28:02 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2014-03-24 06:20:32 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SSHAddress returns a function that can be given to the SSH communicator
|
|
|
|
// for determining the SSH address
|
|
|
|
func SSHAddress(host string, port int) func(multistep.StateBag) (string, error) {
|
|
|
|
return func(state multistep.StateBag) (string, error) {
|
|
|
|
return fmt.Sprintf("%s:%d", host, port), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHConfig returns a function that can be used for the SSH communicator
|
|
|
|
// config for connecting to the specified host via SSH
|
|
|
|
// private_key_file has precedence over password!
|
|
|
|
func SSHConfig(username string, password string, privateKeyFile string) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
|
|
|
|
if privateKeyFile != "" {
|
|
|
|
// key based auth
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(privateKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
privateKey := string(bytes)
|
|
|
|
|
2014-04-26 14:28:02 -04:00
|
|
|
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
|
|
|
|
if err != nil {
|
2014-03-24 06:20:32 -04:00
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: username,
|
2014-04-26 14:28:02 -04:00
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.PublicKeys(signer),
|
2014-03-24 06:20:32 -04:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
// password based auth
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: username,
|
2014-04-26 14:28:02 -04:00
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.Password(password),
|
|
|
|
gossh.KeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(password)),
|
2014-03-24 06:20:32 -04:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|