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

40 lines
965 B
Go
Raw Normal View History

2013-12-22 12:08:09 -05:00
package common
import (
"fmt"
gossh "golang.org/x/crypto/ssh"
2013-12-22 12:08:09 -05:00
"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"
)
func SSHAddress(state multistep.StateBag) (string, error) {
sshHostPort := state.Get("sshHostPort").(uint)
return fmt.Sprintf("127.0.0.1:%d", sshHostPort), nil
}
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
gossh.Password(config.SSHPassword),
gossh.KeyboardInteractive(
2013-12-22 12:08:09 -05:00
ssh.PasswordKeyboardInteractive(config.SSHPassword)),
}
if config.SSHKeyPath != "" {
signer, err := commonssh.FileSigner(config.SSHKeyPath)
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.SSHUser,
Auth: auth,
}, nil
}
}