Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Config #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package goph
import (
"context"
"fmt"
"strconv"
"strings"
"io"
"net"
"os"
"time"

"github.com/kevinburke/ssh_config"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
Expand All @@ -35,9 +38,51 @@ type Config struct {
// DefaultTimeout is the timeout of ssh client connection.
var DefaultTimeout = 20 * time.Second

func GetAuth(host string) (Auth, error) {
auth, err := Key(getSshConfig(host, "IdentityFile"), "")
if err != nil {
return nil, fmt.Errorf("get key: %w", err)
}
return auth, nil
}

func getSshConfig(alias string, key string) string {
// to fix https://github.com/kevinburke/ssh_config/issues/61 issue
return strings.Trim(ssh_config.Get(alias, key), "\"")
}

func NewWithConfigFileHostKeyCallback(name string, callback ssh.HostKeyCallback) (*Client, error){
auth, err := GetAuth(name)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(getSshConfig(name, "Port"))
if err != nil {
return nil, err
}

return NewConn(&Config{
User: getSshConfig(name, "User"),
Addr: getSshConfig(name, "HostName"),
Port: uint(port),
Auth: auth,
Timeout: DefaultTimeout,
Callback: callback,
})

}

// NewWithConfigFile starts a new ssh connection with given config file, the host public key must be in known hosts.
func NewWithConfigFile(name string) (*Client, error) {
callback, err := DefaultKnownHosts()
if err != nil {
return nil, err
}
return NewWithConfigFileHostKeyCallback(name, callback)
}

// New starts a new ssh connection, the host public key must be in known hosts.
func New(user string, addr string, auth Auth) (c *Client, err error) {

callback, err := DefaultKnownHosts()

if err != nil {
Expand Down Expand Up @@ -70,6 +115,10 @@ func NewUnknown(user string, addr string, auth Auth) (*Client, error) {
})
}

func NewWithConfigFileUnknown(name string) (*Client, error) {
return NewWithConfigFileHostKeyCallback(name, ssh.InsecureIgnoreHostKey())
}

// NewConn returns new client and error if any.
func NewConn(config *Config) (c *Client, err error) {

Expand Down
3 changes: 2 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package goph
import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"strings"
)

// Cmd it's like os/exec.Cmd but for ssh session.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/melbahja/goph
go 1.13

require (
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.5
golang.org/x/crypto v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kevinburke/ssh_config v1.1.0 h1:pH/t1WS9NzT8go394IqZeJTMHVm6Cr6ZJ6AQ+mdNo/o=
github.com/kevinburke/ssh_config v1.1.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down