packer-cn/builder/virtualbox/common/ssh.go

44 lines
1.1 KiB
Go
Raw Normal View History

2013-12-22 12:08:09 -05:00
package common
import (
"github.com/mitchellh/multistep"
commonssh "github.com/mitchellh/packer/common/ssh"
2013-12-22 12:08:09 -05:00
"github.com/mitchellh/packer/communicator/ssh"
2015-06-09 00:34:20 -04:00
gossh "golang.org/x/crypto/ssh"
2013-12-22 12:08:09 -05:00
)
func CommHost(host string) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
return host, nil
}
}
func SSHPort(state multistep.StateBag) (int, error) {
sshHostPort := state.Get("sshHostPort").(int)
2017-03-28 21:47:10 -04:00
return sshHostPort, nil
2013-12-22 12:08:09 -05:00
}
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
2013-12-22 12:08:09 -05:00
}
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
2013-12-22 12:08:09 -05:00
if err != nil {
return nil, err
}
2014-04-26 14:12:43 -04:00
auth = append(auth, gossh.PublicKeys(signer))
2013-12-22 12:08:09 -05:00
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
2013-12-22 12:08:09 -05:00
Auth: auth,
}, nil
}
}