/// <summary> /// Creates and opens connection to the specified mikrotik host on specified port and perform the logon operation. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <param name="host">The host (name or ip).</param> /// <param name="port">TCPIP port.</param> /// <param name="user">The user.</param> /// <param name="password">The password.</param> /// <returns>Opened instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Close"/> public static ITikConnection OpenConnection(TikConnectionType connectionType, string host, int port, string user, string password) { ITikConnection result = CreateConnection(connectionType); result.Open(host, port, user, password); return result; }
/// <summary> /// Creates mikrotik Connection of given type. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <returns>Instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Open(string, string, string)"/> public static ITikConnection CreateConnection(TikConnectionType connectionType) { ITikConnection result; if (connectionType == TikConnectionType.Api) { result = new ApiConnection(); } else throw new NotImplementedException(string.Format("Connection type '{0}' not supported.", connectionType)); return result; }
private static void OpenConnectionAndExecuteSimpleCommand(TikConnectionType connectionType, string host, string user, string pass) { using (var connection = CreateOpenedConnection(connectionType, host, user, pass)) { ITikCommand readCmd = connection.CreateCommand("/system/identity/print"); var originalIdentity = readCmd.ExecuteScalar(); Assert.IsNotNull(originalIdentity); connection.Close(); } }
/// <summary> /// Creates mikrotik Connection of given type. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <returns>Instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Open(string, string, string)"/> public static ITikConnection CreateConnection(TikConnectionType connectionType) { switch (connectionType) { case TikConnectionType.Api: return new ApiConnection(false); case TikConnectionType.ApiSsl: return new ApiConnection(true); default: throw new NotImplementedException(string.Format("Connection type '{0}' not supported.", connectionType)); } }
/// <summary> /// Creates mikrotik Connection of given type. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <returns>Instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Open(string, string, string)"/> public static ITikConnection CreateConnection(TikConnectionType connectionType) { switch (connectionType) { case TikConnectionType.Api: return(new ApiConnection(false)); case TikConnectionType.ApiSsl: return(new ApiConnection(true)); default: throw new NotImplementedException(string.Format("Connection type '{0}' not supported.", connectionType)); } }
/// <summary> /// Creates mikrotik Connection of given type. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <returns>Instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Open(string, string, string)"/> public static ITikConnection CreateConnection(TikConnectionType connectionType) { ITikConnection result; if (connectionType == TikConnectionType.Api) { result = new ApiConnection(); } else { throw new NotImplementedException(string.Format("Connection type '{0}' not supported.", connectionType)); } return(result); }
/// <summary> /// Creates mikrotik Connection of given type. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <returns>Instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Open(string, string, string)"/> public static ITikConnection CreateConnection(TikConnectionType connectionType) { switch (connectionType) { case TikConnectionType.Api: return new ApiConnection(false, ApiConnection.LoginProcessVersion.Version1); case TikConnectionType.Api_v2: return new ApiConnection(false, ApiConnection.LoginProcessVersion.Version2); case TikConnectionType.ApiSsl: return new ApiConnection(true, ApiConnection.LoginProcessVersion.Version1); case TikConnectionType.ApiSsl_v2: return new ApiConnection(true, ApiConnection.LoginProcessVersion.Version2); default: throw new NotImplementedException(string.Format("Connection type '{0}' not supported.", connectionType)); } }
/// <summary> /// Constructs for the given lower layer. /// </summary> /// <param name="address">The address of this device.</param> /// <param name="connectionType">The type of the connection in use (required in case we have to re-open).</param> /// <param name="tikConnection">The lower layer connection for talking to this device.</param> /// <param name="options">The options to use.</param> /// <param name="sysIdent">The system ident (to create SystemData from).</param> /// /// <param name="sysResource">The system resource info (to create SystemData from).</param> /// <param name="sysRouterboard">The system routerboard info (to create SystemData from).</param> public MikrotikApiDeviceHandler(IpAddress address, TikConnectionType connectionType, ITikConnection tikConnection, IQuerierOptions options, SystemIdentity sysIdent, SystemResource sysResource, SystemRouterboard sysRouterboard) { if ((options.AllowedApis & this.SupportedApi) == 0) { throw new ArgumentOutOfRangeException(nameof(options), $"This device handler doesn't support any of the APIs allowed by the IQuerierOptions (allowed: {options.AllowedApis}, supported {this.SupportedApi})."); } this.sysRouterboard = sysRouterboard ?? throw new ArgumentNullException(nameof(sysRouterboard), "sysRouterboard is null when creating a MikrotikApiDeviceHandler"); this.sysResource = sysResource ?? throw new ArgumentNullException(nameof(sysResource), "sysResource is null when creating a MikrotikApiDeviceHandler"); this.sysIdent = sysIdent ?? throw new ArgumentNullException(nameof(sysIdent), "sysIdent is null when creating a MikrotikApiDeviceHandler"); this.Address = address ?? throw new ArgumentNullException(nameof(address), "address is null when creating a MikrotikApiDeviceHandler"); this.TikConnection = tikConnection ?? throw new ArgumentNullException(nameof(tikConnection), "tikConnection is null when creating a MikrotikApiDeviceHandler"); this.DetectedConnectionType = connectionType; this.Options = options ?? throw new ArgumentNullException(nameof(options), "options are null when creating a MikrotikApiDeviceHandler"); }
/// <summary> /// Creates mikrotik Connection of given type. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <returns>Instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Open(string, string, string)"/> public static ITikConnection CreateConnection(TikConnectionType connectionType) { switch (connectionType) { case TikConnectionType.Api: #pragma warning disable CS0618 // Type or member is obsolete case TikConnectionType.Api_v2: #pragma warning restore CS0618 // Type or member is obsolete return(new ApiConnection(false)); case TikConnectionType.ApiSsl: #pragma warning disable CS0618 // Type or member is obsolete case TikConnectionType.ApiSsl_v2: #pragma warning restore CS0618 // Type or member is obsolete return(new ApiConnection(true)); default: throw new NotImplementedException(string.Format("Connection type '{0}' not supported.", connectionType)); } }
/// <inheritdoc /> public override bool IsApplicableVendorSpecific(IpAddress address, IQuerierOptions options) { try { this.tikConnection = ConnectionFactory.CreateConnection(this.apiInUse); // following MUST be called before Open() this.tikConnection.SendTimeout = Convert.ToInt32(options.Timeout.TotalMilliseconds); this.tikConnection.ReceiveTimeout = Convert.ToInt32(options.Timeout.TotalMilliseconds); this.tikConnection.Open(address.ToString(), options.LoginUser ?? string.Empty, options.LoginPassword ?? string.Empty); this.QuerySystemData(); return(true); } catch (TikCommandException tikConnectionException) { this.CollectException("MTik APIv2", tikConnectionException); log.Info($"Device {address}: Mikrotik APIv2 connection failed: {tikConnectionException.Message}. Trying APIv1"); if (this.tikConnection != null) { this.tikConnection.Dispose(); this.tikConnection = null; } } catch (TikConnectionException tikConnectionException) { this.CollectException("MTik APIv2", tikConnectionException); log.Info($"Device {address}: Mikrotik APIv2 connection failed: {tikConnectionException.Message}. Trying APIv1"); if (this.tikConnection != null) { this.tikConnection.Dispose(); this.tikConnection = null; } } catch (IOException ioEx) { this.CollectException("MTik APIv2 (not falling back to v1 due to IOException being api-version-independent)", ioEx); log.Info($"Device {address}: I/O Exception in Mikrotik API connection: {ioEx.Message}. Considering device as not applicable"); if (this.tikConnection != null) { this.tikConnection.Dispose(); this.tikConnection = null; } return(false); } catch (SocketException socketEx) { this.CollectException("MTik APIv2 (not falling back to v1 due to SocketException being api-version-independent)", socketEx); log.Info($"Device {address}: Socket Exception in Mikrotik API connection: {socketEx.Message}. Considering device as not applicable"); if (this.tikConnection != null) { this.tikConnection.Dispose(); this.tikConnection = null; } return(false); } this.apiInUse = TikConnectionType.Api; try { this.tikConnection = ConnectionFactory.CreateConnection(this.apiInUse); // following MUST be called before Open() this.tikConnection.SendTimeout = Convert.ToInt32(options.Timeout.TotalMilliseconds); this.tikConnection.ReceiveTimeout = Convert.ToInt32(options.Timeout.TotalMilliseconds); this.tikConnection.Open(address.ToString(), options.LoginUser ?? string.Empty, options.LoginPassword ?? string.Empty); this.QuerySystemData(); return(true); } catch (TikCommandException tikConnectionException) { this.CollectException("MTik APIv1", tikConnectionException); log.Info($"Device {address}: Mikrotik APIv1 connection failed: {tikConnectionException.Message}. Assuming Mikrotik API NOT AVAILABLE"); if (this.tikConnection != null) { this.tikConnection.Dispose(); this.tikConnection = null; } return(false); } catch (TikConnectionException tikConnectionException) { this.CollectException("MTik APIv1", tikConnectionException); log.Info($"Device {address}: Mikrotik APIv1 connection failed: {tikConnectionException.Message}. Assuming Mikrotik API NOT AVAILABLE"); if (this.tikConnection != null) { this.tikConnection.Dispose(); this.tikConnection = null; } return(false); } }
/// <summary> /// Creates and opens connection to the specified mikrotik host on default port and perform the logon operation. /// Async version. /// </summary> /// <param name="connectionType">Type of technology used to connect to mikrotik router.</param> /// <param name="host">The host (name or ip).</param> /// <param name="user">The user.</param> /// <param name="password">The password.</param> /// <returns>Opened instance of mikrotik Connection.</returns> /// <seealso cref="ITikConnection.Close"/> public static async System.Threading.Tasks.Task <ITikConnection> OpenConnectionAsync(TikConnectionType connectionType, string host, string user, string password) { ITikConnection result = CreateConnection(connectionType); await result.OpenAsync(host, user, password); return(result); }
internal static ITikConnection CreateConnectionType(TikConnectionType api) { ITikConnection conn = ConnectionFactory.CreateConnection(api); return(conn); }
internal static ITikConnection CreateInstanceOfMikrotik(TikConnectionType Api, string RealIP, int Port, string MikUserName, string MikPassword) { connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, RealIP, Port, MikUserName, MikPassword); return(connection); }