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 NewSSHSessionForExternalClient for managing ssh client externally #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions netconf/transport_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type TransportSSH struct {
TransportBasicIO
sshClient *ssh.Client
sshSession *ssh.Session

// SSH Client connection is managed externally
externClient bool
}

// Close closes an existing SSH session and socket if they exist.
Expand All @@ -48,13 +51,15 @@ func (t *TransportSSH) Close() error {
if err := t.sshSession.Close(); err != nil {
// If we receive an error when trying to close the session, then
// lets try to close the socket, otherwise it will be left open
t.sshClient.Close()
if !t.externClient {
t.sshClient.Close()
}
return err
}
}

// Close the socket
if t.sshClient != nil {
if !t.externClient && t.sshClient != nil {
return t.sshClient.Close()
}
return fmt.Errorf("No connection to close")
Expand Down Expand Up @@ -117,6 +122,17 @@ func NewSSHSession(conn net.Conn, config *ssh.ClientConfig) (*Session, error) {
return NewSession(t), nil
}

// NewSSHSessionForExternalClient creates a new NETCONF session using an existing ssh.Client
// initiated and managed externally.
func NewSSHSessionForExternalClient(client *ssh.Client) (*Session, error) {
t, err := clientToTransport(client)
if err != nil {
return nil, err
}

return NewSession(t), nil
}

// DialSSH creates a new NETCONF session using a SSH Transport.
// See TransportSSH.Dial for arguments.
func DialSSH(target string, config *ssh.ClientConfig) (*Session, error) {
Expand Down Expand Up @@ -244,6 +260,20 @@ func connToTransport(conn net.Conn, config *ssh.ClientConfig) (*TransportSSH, er
return t, nil
}

func clientToTransport(client *ssh.Client) (*TransportSSH, error) {
t := &TransportSSH{
sshClient: client,
externClient: true,
}

err := t.setupSession()
if err != nil {
return nil, err
}

return t, nil
}

type deadlineConn struct {
net.Conn
timeout time.Duration
Expand Down