Merge pull request #4 from gaxuhongyu/master

Fix ssh error: must specify HostKeyCallback
This commit is contained in:
Tobias Schwab
2017-08-09 16:15:23 +02:00
committed by GitHub
2 changed files with 16 additions and 8 deletions

View File

@@ -22,6 +22,7 @@ Golang ssh library
client := gossh.New("some.host", "user")
// my default agent authentication is used. use
// client.SetPassword("<secret>")
// client.SetPrivateKey("<PrivateKey path>") if not set this path will read this prv key:$HOME/.ssh/id_rsa
// for password authentication
client.DebugWriter = MakeLogger("DEBUG")
client.InfoWriter = MakeLogger("INFO ")

View File

@@ -35,7 +35,7 @@ type Client struct {
DebugWriter Writer
ErrorWriter Writer
InfoWriter Writer
PrivateKey ssh.Signer
PrivateKey string
}
func (c *Client) Password(user string) (password string, e error) {
@@ -72,6 +72,10 @@ func (c *Client) SetPassword(password string) {
c.password = password
}
func (c *Client) SetPrivateKey(privateKey string) {
c.PrivateKey = privateKey
}
func (c *Client) Connection() (*ssh.Client, error) {
if c.Conn != nil {
return c.Conn, nil
@@ -95,7 +99,8 @@ func (c *Client) Connect() (err error) {
c.Port = 22
}
config := &ssh.ClientConfig{
User: c.User,
User: c.User,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
keys := []ssh.Signer{}
@@ -109,12 +114,14 @@ func (c *Client) Connect() (err error) {
}
}
if c.PrivateKey != nil {
keys = append(keys, c.PrivateKey)
}
if pk, err := readPrivateKey(os.ExpandEnv("$HOME/.ssh/id_rsa")); err == nil {
keys = append(keys, pk)
if len(c.PrivateKey) != 0 {
if pk, err := readPrivateKey(c.PrivateKey); err == nil {
keys = append(keys, pk)
}
} else {
if pk, err := readPrivateKey(os.ExpandEnv("$HOME/.ssh/id_rsa")); err == nil {
keys = append(keys, pk)
}
}
if len(keys) > 0 {