Skip to content

Commit d40dbe3

Browse files
thebinarynemith
andauthored
add NewSSHSessionForExternalClient for managing ssh client externally (#125)
* add NewSSHSessionForExternalClient for transport_ssh This enables user to initiate and manage (including closing) of ssh client externally. This enables transport session to reuse external ssh client for multiple sessions. * Update netconf/transport_ssh.go refactor: rename externClient to managedSession on TransportSSH Co-authored-by: Brandon Bennett <[email protected]> * fixup! Update netconf/transport_ssh.go Signed-off-by: thebinary <[email protected]> * rename NewSSHSessionForExternalClient to NewSSHClientSession Signed-off-by: thebinary <[email protected]> * refactor: remove redundant clientToTransport fn Signed-off-by: thebinary <[email protected]> --------- Signed-off-by: thebinary <[email protected]> Co-authored-by: Brandon Bennett <[email protected]>
1 parent 92d0d9c commit d40dbe3

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

netconf/transport_ssh.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type TransportSSH struct {
3434
TransportBasicIO
3535
sshClient *ssh.Client
3636
sshSession *ssh.Session
37+
38+
// SSH Client connection is managed externally
39+
managedSession bool
3740
}
3841

3942
// Close closes an existing SSH session and socket if they exist.
@@ -48,13 +51,15 @@ func (t *TransportSSH) Close() error {
4851
if err := t.sshSession.Close(); err != nil {
4952
// If we receive an error when trying to close the session, then
5053
// lets try to close the socket, otherwise it will be left open
51-
t.sshClient.Close()
54+
if !t.managedSession {
55+
t.sshClient.Close()
56+
}
5257
return err
5358
}
5459
}
5560

5661
// Close the socket
57-
if t.sshClient != nil {
62+
if !t.managedSession && t.sshClient != nil {
5863
return t.sshClient.Close()
5964
}
6065
return fmt.Errorf("No connection to close")
@@ -117,6 +122,22 @@ func NewSSHSession(conn net.Conn, config *ssh.ClientConfig) (*Session, error) {
117122
return NewSession(t), nil
118123
}
119124

125+
// NewSSHClientSession creates a new NETCONF session using an existing ssh.Client
126+
// initiated and managed externally.
127+
func NewSSHClientSession(client *ssh.Client) (*Session, error) {
128+
t := &TransportSSH{
129+
sshClient: client,
130+
managedSession: true,
131+
}
132+
133+
err := t.setupSession()
134+
if err != nil {
135+
return nil, err
136+
}
137+
138+
return NewSession(t), nil
139+
}
140+
120141
// DialSSH creates a new NETCONF session using a SSH Transport.
121142
// See TransportSSH.Dial for arguments.
122143
func DialSSH(target string, config *ssh.ClientConfig) (*Session, error) {

0 commit comments

Comments
 (0)