netconfig on juniper solution

Dec 12, 2012 at 7:32 AM
Edited Dec 12, 2012 at 7:37 AM

There are some problems using this on an juniper ex switch.
"Server capabilities is not the first message received"

This is because the replys are not received in one chunk. and also, the final _prompt contains a linefeed.

This is solved for me . See Red

... 

protected override void OnDataReceived(uint dataTypeCode, byte[] data) {
string chunk = Encoding.UTF8.GetString(data);
if (this.ServerCapabilities == null)
// This must be server capabilities, old protocol
{
sb.Append(chunk);
if (!chunk.Contains(_prompt)) {
return ;
//throw new NetConfServerException("Server capabilities is not the first message received");
}
try
{
chunk = sb.ToString(); sb.Clear(); 

.....

else // Old protocol
{
sb.Append(chunk);
if(!chunk.Contains(_prompt))
{
  return;
 //throw new NetConfServerException("Server XML message does not end with the prompt " + _prompt);
}
chunk = sb.ToString(); sb.Clear();
this._rpcReply.Append(chunk.Replace(_prompt, ""));
this._rpcReplyReceived.Set();

}

public XmlDocument SendReceiveRpc(XmlDocument rpc, bool automaticMessageIdHandling)
{
sb.Clear();
.. 

protected override void OnChannelOpen()
{
sb.Clear();
.. 

 

internal class NetConfSession: SubsystemSession

{ 
private StringBuilder sb = new StringBuilder();
Dec 12, 2012 at 7:44 AM
Edited Dec 12, 2012 at 10:33 AM

And if someone needs a sample getting vlan information from a juniper device. (ex-4200 switch)

static XmlDocument netconfCommand(string cmd)
{
XmlDocument c = new XmlDocument();
c.LoadXml(
"<rpc><" + cmd + "/></rpc>");
return c;
}

static void Main(string[] args)
{
XmlDocument answer;
 
NetConfClient nc = new NetConfClient("switch", "username", "mypassword");
  nc.AutomaticMessageIdHandling =
false;
  nc.Connect();
  answer = nc.SendReceiveRpc(netconfCommand(
"get-vlan-information"));
  answer.Save(
@"c:\temp\answer.xml");
}

Coordinator
Dec 20, 2012 at 4:38 AM

 

Hi,

 

Can you send me modified files or submit it as a patch?

This piece of code was contributed by other developer and I have no knowledge of how it should work.

 

Thanks,

Oleg

Dec 20, 2012 at 6:25 AM

Hi

There were some small modifications, and now it works every hour, each day, on 40 devices, reading information from Juniper switches, which use netconf.

Fully documented at http://www.juniper.net/techpubs/en_US/junos12.2/information-products/topic-collections/netconf-guide/index.html

Renci.SshClient\Renci.SshNet\Netconf\NetConfSession.cs had some small changes.

Because all chunks of data do not come in one piece.

It would be nice if the original code had this patches.

You library really works perfectly. No more hassling with plink.exe.

Regards,

Luc Vandenbroucke

System engineer SCK•CEN

www.sckcen.be

From: olegkap [email removed]
Sent: donderdag 20 december 2012 6:39
To: Vandenbroucke Luc
Subject: Re: netconfig on juniper solution [sshnet:406490]

From: olegkap

Hi,

Can you send me modified files or submit it as a patch?

This piece of code was contributed by other developer and I have no knowledge of how it should work.

Thanks,

Oleg



-

www.sckcen.be

-
-----------------------------------------------------------------------
SCK•CEN Disclaimer: http://www.sckcen.be/en/Legal-aspects/E-mail-disclaimer
Coordinator
Dec 20, 2012 at 2:05 PM

Hi,

 

I could not find a modified NetConfSession.cs file there.

 

Can you please send it to me so I could merge your changes?

Thanks,

Oleg

Dec 20, 2012 at 2:28 PM

Hello,

I did upload the file as patch 13499

Is this ok ?

Regards,

Luc

From: olegkap [email removed]
Sent: donderdag 20 december 2012 16:06
To: Vandenbroucke Luc
Subject: Re: netconfig on juniper solution [sshnet:406490]

From: olegkap

Hi,

I could not find a modified NetConfSession.cs file there.

Can you please send it to me so I could merge your changes?

Thanks,

Oleg



-

www.sckcen.be

-
-----------------------------------------------------------------------
SCK•CEN Disclaimer: http://www.sckcen.be/en/Legal-aspects/E-mail-disclaimer
Coordinator
Dec 20, 2012 at 3:02 PM

 

Yes,

Thanks you I will apply this patch in a second.

 

Thanks,

Oleg

Feb 7, 2014 at 12:10 AM
Sorry i know this is an old thread. Did this get merged into the main release? I am using the 3.5 and 4.0 Binary in a project and getting the same error on this.Connect()

Thanks in advance
Feb 7, 2014 at 4:55 AM
It is still working for me.
Netconf is configured on Juniper ( set system services netconf ssh )

and in my code

nc = new NetConfClient(ip, "admin", pw);
nc.AutomaticMessageIdHandling = false;
nc.OperationTimeout = new TimeSpan(0, 0, 60);
try {
                nc.Connect();
}
catch (System.Exception ex)
{
                ...
}
...
Feb 11, 2014 at 3:21 PM
OK. I think I got this figured out I was loading an older version of the Binary. I am no longer getting the error.

would anyone be able to post an example of an RPC message? I am sending the following:

<rpc><get-chassis-inventory><detail/></get-chassis-inventory></rpc>

all I get as return:

rpc-reply
Feb 11, 2014 at 3:32 PM

I do

static Renci.SshNet.NetConfClient nc;

nc = new NetConfClient(ip, user, pw);

nc.AutomaticMessageIdHandling = false;

nc.OperationTimeout = new TimeSpan(0, 0, 60);

nc.Connect();

System.Xml.XmlDocument c = new System.Xml.XmlDocument();

System.Xml.XmlDocument d = new System.Xml.XmlDocument();

c.PreserveWhitespace = false;

c.LoadXml<rpc><get-chassis-inventory/></rpc>");

d = nc.SendReceiveRpc(c);