NetConfClient
Contains operation for working with NetConf server.
using Renci.SshNet.Common;
using Renci.SshNet.NetConf;
using System;
using System.Runtime.CompilerServices;
using System.Xml;
namespace Renci.SshNet
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class NetConfClient : BaseClient
{
private int _operationTimeout;
[System.Runtime.CompilerServices.Nullable(2)]
private INetConfSession _netConfSession;
public TimeSpan OperationTimeout {
get {
return TimeSpan.FromMilliseconds((double)_operationTimeout);
}
set {
_operationTimeout = value.AsTimeout("OperationTimeout");
}
}
[System.Runtime.CompilerServices.Nullable(2)]
internal INetConfSession NetConfSession {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _netConfSession;
}
}
public XmlDocument ServerCapabilities {
get {
if (_netConfSession == null)
throw new SshConnectionException("Client not connected.");
return _netConfSession.ServerCapabilities;
}
}
public XmlDocument ClientCapabilities {
get {
if (_netConfSession == null)
throw new SshConnectionException("Client not connected.");
return _netConfSession.ClientCapabilities;
}
}
public bool AutomaticMessageIdHandling { get; set; }
public NetConfClient(ConnectionInfo connectionInfo)
: this(connectionInfo, false)
{
}
public NetConfClient(string host, int port, string username, string password)
: this(new PasswordConnectionInfo(host, port, username, password), true)
{
}
public NetConfClient(string host, string username, string password)
: this(host, 22, username, password)
{
}
public NetConfClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)
: this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), true)
{
}
public NetConfClient(string host, string username, params IPrivateKeySource[] keyFiles)
: this(host, 22, username, keyFiles)
{
}
private NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
: this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
{
}
internal NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
_operationTimeout = -1;
AutomaticMessageIdHandling = true;
}
public XmlDocument SendReceiveRpc(XmlDocument rpc)
{
if (_netConfSession == null)
throw new SshConnectionException("Client not connected.");
return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
}
public XmlDocument SendReceiveRpc(string xml)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
return SendReceiveRpc(xmlDocument);
}
public XmlDocument SendCloseRpc()
{
if (_netConfSession == null)
throw new SshConnectionException("Client not connected.");
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><rpc message-id=\"6666\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><close-session/></rpc>");
return _netConfSession.SendReceiveRpc(xmlDocument, AutomaticMessageIdHandling);
}
protected override void OnConnected()
{
base.OnConnected();
_netConfSession = CreateAndConnectNetConfSession();
}
protected override void OnDisconnecting()
{
base.OnDisconnecting();
_netConfSession?.Disconnect();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && _netConfSession != null) {
_netConfSession.Dispose();
_netConfSession = null;
}
}
private INetConfSession CreateAndConnectNetConfSession()
{
INetConfSession netConfSession = base.ServiceFactory.CreateNetConfSession(base.Session, _operationTimeout);
try {
netConfSession.Connect();
return netConfSession;
} catch {
netConfSession.Dispose();
throw;
}
}
}
}