Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
28 changes: 27 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"context"
"fmt"
"io"
"log"
"net"
"os"
"time"

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

// 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) {
auth, err := Key(ssh_config.Get(name, "IdentityFile"), "")
if err != nil {
log.Fatal(err)
}
return New(
ssh_config.Get(name, "User"),
ssh_config.Get(name, "HostName"),
auth,
)
}

// 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 @@ -69,6 +83,18 @@ func NewUnknown(user string, addr string, auth Auth) (*Client, error) {
})
}

func NewWithConfigFileUnknown(name string) (*Client, error) {
auth, err := Key(ssh_config.Get(name, "IdentityFile"), "")
if err != nil {
log.Fatal(err)
}
return NewUnknown(
ssh_config.Get(name, "User"),
ssh_config.Get(name, "HostName"),
auth,
)
}

// 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.4
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
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