Communicating with a ssh server over a subsystem

865 views
Skip to first unread message

Charl Matthee

unread,
Aug 11, 2013, 1:52:11 PM8/11/13
to golan...@googlegroups.com
Hi,

I am trying to find a way to communicate with a remote system that exposes a netconf subsystem over ssh.

Here is an extract of the code I am using to accomplish this:

package main

import (
"net"
)

type clientPassword string

func (p clientPassword) Password(user string) (string, error) {
return string(p), nil
}

func main() {
  username := "foo"
  password := "bar"

config := &ssh.ClientConfig{
User: username,
Auth: []ssh.ClientAuth{
ssh.ClientAuthPassword(clientPassword(password)),
},
}

  client, err := ssh.Dial("tcp", "127.0.0.1:22", config)
  if err != nil {
      panic("Failed to dial: " + err.Error())
  }
  defer client.Close()

session, err := client.NewSession()
if err != nil {
panic(err)
}
defer session.Close()

// Request a netconf subsystem
if err := session.RequestSubsystem("netconf"); err != nil {
panic(err)
}

// How do I now interact with the netconf subsystem?
}

I am unsure how I now send the netconf subsystem XML/RPC commands (for e.g. `<?xml version="1.0" encoding="UTF-8"?><hello><capabilities><capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability></capabilities></hello>]]>]]>`) as one cannot use the Start()/Run()/etc. commands to send the data.

I have tried using the session.StdoutPipe() and session.StdinPipe() functions but I cannot get this working either.

Does anyone has an example of how to use the session.StdoutPipe() and session.StdinPipe() functions if that is the way this is supposed to work? Alternatively, has anyone got any experience working with netconf over ssh in golang?


Ciao

Charl

Jonathan Pittman

unread,
Aug 11, 2013, 4:00:41 PM8/11/13
to Charl Matthee, golang-nuts
I would start here.

http://tools.ietf.org/html/rfc6242

I would also get std{in,out,err} set up before doing the subsystem request.  Then after the subsystem request, try simply writing to the stdin pipe (which is an io.WriteCloser).  And then read from stdout and stderr pipes (both are io.Reader).


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Charl Matthee

unread,
Aug 11, 2013, 4:34:11 PM8/11/13
to golan...@googlegroups.com, Charl Matthee
Hi,


On Sunday, 11 August 2013 22:00:41 UTC+2, Jonathan Pittman wrote:
 
I would also get std{in,out,err} set up before doing the subsystem request.  Then after the subsystem request, try simply writing to the stdin pipe (which is an io.WriteCloser).  And then read from stdout and stderr pipes (both are io.Reader).

Moved the  std{in,out,err} set up before doing the subsystem request and everything's working now.

Thanks.


Ciao

Charl

Charl Matthee

unread,
Aug 12, 2013, 1:02:22 AM8/12/13
to golan...@googlegroups.com, Charl Matthee
Hi,


On Sunday, 11 August 2013 22:34:11 UTC+2, Charl Matthee wrote:
 
Moved the  std{in,out,err} set up before doing the subsystem request and everything's working now.

For those interested in how this all works here is the code used after the ssh client has been created:

        // Create a session
session, err := client.NewSession()
if err != nil {
panic(err)
}
defer session.Close()

si, err := session.StdinPipe()
if err != nil {
panic(err)
}
defer si.Close()

so, err := session.StdoutPipe()
if err != nil {
panic(err)
}

// Request a netconf subsystem
if err := session.RequestSubsystem("netconf"); err != nil {
panic(err)
}

input := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?><hello><capabilities><capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability></capabilities></hello>]]>]]>`)
n, err := si.Write(input.Bytes())
if err != nil && err != io.EOF {
panic(err)
}
fmt.Printf("Wrote %d bytes: %s\n", n, input.String())

out, err := ioutil.ReadAll(so)
if err != nil {
panic(err)
}
fmt.Printf("Read: %s\n", string(out))


Ciao

Charl
Reply all
Reply to author
Forward
0 new messages