/// <summary> /// Calls command and starts backgroud reading thread. After that returns control to calling thread. /// All read rows are returned as callbacks (<paramref name="onLoadItemCallback"/>, <paramref name="onExceptionCallback"/>) from loading thread. /// REMARKS: if you want to propagate loaded values to GUI, you should use some kind of synchronization or Invoke, because /// callbacks are called from non-ui thread. /// The running load can be terminated by <see cref="ITikCommand.Cancel"/> or <see cref="ITikCommand.CancelAndJoin()"/> call. /// Command is returned as result of the method. /// </summary> /// <typeparam name="TEntity">Loaded entities type.</typeparam> /// <param name="command">Tik command executed to load.</param> /// <param name="onLoadItemCallback">Callback called for each loaded !re row</param> /// <param name="onExceptionCallback">Callback called when error occurs (!trap row is returned)</param> /// <param name="onDoneCallback">Callback called at the end of command run (!done row is returned). Usefull for cleanup operations at the end of command lifecycle. You can also use synchronous call <see cref="ITikCommand.CancelAndJoin()"/> from calling thread and do cleanup after it.</param> public static void LoadAsync <TEntity>(this ITikCommand command, Action <TEntity> onLoadItemCallback, Action <Exception> onExceptionCallback = null, Action onDoneCallback = null) where TEntity : new() { Guard.ArgumentNotNull(command, "command"); Guard.ArgumentNotNull(onLoadItemCallback, "onLoadItemCallback"); command.ExecuteAsync( reSentence => onLoadItemCallback(CreateObject <TEntity>(reSentence)), trapSentence => { if (onExceptionCallback != null) { onExceptionCallback(new TikCommandTrapException(command, trapSentence)); } }, () => { if (onDoneCallback != null) { onDoneCallback(); } }); }
public static bool suspendpppoeuser(string pppoeuser) { using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api)) // Use TikConnectionType.Api for mikrotikversion prior v6.45 { connection.Open("10.19.10.1", "OrionAdmin", "Frank1e2015"); var pppoeactive = connection.LoadList <PppActive>(); foreach (PppActive pppa in pppoeactive) { string ipaddress = ""; if (pppa.Name == pppoeuser) { ipaddress = pppa.Address; } if (ipaddress != "") { ITikCommand cmd = connection.CreateCommand("/ip/firewall/address-list/add", connection.CreateParameter("list", "unmssuspend"), connection.CreateParameter("address", ipaddress), connection.CreateParameter("comment", "suspend")); cmd.ExecuteAsync(response => { // Console.WriteLine("Row: " + response.GetResponseField("tx")); }); cmd.Cancel(); } } connection.Close(); } return(true); }
private static void Torch(ITikConnection connection) { ITikCommand torchCmd = connection.CreateCommand("/tool/torch", connection.CreateParameter("interface", "ether1"), connection.CreateParameter("port", "any"), connection.CreateParameter("src-address", "0.0.0.0/0"), connection.CreateParameter("dst-address", "0.0.0.0/0") ); torchCmd.ExecuteAsync(response => { Console.WriteLine("Row: " + response.GetResponseField("tx")); }); Console.WriteLine("Press ENTER"); Console.ReadLine(); torchCmd.CancelAndJoin(); }
public static bool UpdateSecret() { using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api)) // Use TikConnectionType.Api for mikrotikversion prior v6.45 { connection.Open("10.19.60.1", "OrionAdmin", "Frank1e2015"); radiusEntities db = new radiusEntities(); List <RadiusEntry> radiusentries = db.RadiusEntries.ToList(); foreach (RadiusEntry re in radiusentries) { ITikCommand cmd = connection.CreateCommand("/ppp/secret/add", connection.CreateParameter("name", re.username), connection.CreateParameter("password", re.value), connection.CreateParameter("profile", re.groupname)); cmd.ExecuteAsync(response => { // Console.WriteLine("Row: " + response.GetResponseField("tx")); }); cmd.Cancel(); } return(true); } }
public static bool Mikrotik() { using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api)) // Use TikConnectionType.Api for mikrotikversion prior v6.45 { connection.Open("10.19.20.1", "OrionAdmin", "Frank1e2015"); ITikCommand cmd = connection.CreateCommand("/system/identity/print"); var identity = cmd.ExecuteScalar(); Console.WriteLine("Identity: {0}", identity); var logs = connection.LoadList <Log>(); foreach (Log log in logs) { Console.WriteLine("{0}[{1}]: {2}", log.Time, log.Topics, log.Message); } var firewallFilter = new FirewallFilter() { Chain = FirewallFilter.ChainType.Forward, Action = FirewallFilter.ActionType.Accept, }; connection.Save(firewallFilter); ITikCommand torchCmd = connection.CreateCommand("/tool/torch", connection.CreateParameter("interface", "ether1"), connection.CreateParameter("port", "any"), connection.CreateParameter("src-address", "0.0.0.0/0"), connection.CreateParameter("dst-address", "0.0.0.0/0")); torchCmd.ExecuteAsync(response => { Console.WriteLine("Row: " + response.GetResponseField("tx")); }); Console.WriteLine("Press ENTER"); Console.ReadLine(); torchCmd.Cancel(); return(true); } }