public void UpdateMetrics() { var capsManRegistrationTables = connection.LoadAll <CapsManRegistrationTable>(); var dhcpServerLeases = connection.LoadAll <DhcpServerLease>(); var interfaces = connection.LoadAll <Interface>(); foreach (var client in capsManRegistrationTables) { var dhcpLease = dhcpServerLeases.SingleOrDefault(l => client.MACAddress == l.MacAddress); var match = Regex.Match(client.Bytes, "([0-9]+),([0-9]+)"); txBytes.WithLabels(client.MACAddress, dhcpLease?.Address ?? "", dhcpLease?.HostName ?? "") .Set(long.Parse(match.Groups[1].Value)); rxBytes.WithLabels(client.MACAddress, dhcpLease?.Address ?? "", dhcpLease?.HostName ?? "") .Set(long.Parse(match.Groups[2].Value)); var signalClient = signal.WithLabels(client.MACAddress, dhcpLease?.Address ?? "", dhcpLease?.HostName ?? ""); signalClient.Set(client.Signal); } foreach (var client in interfaces) { var dhcpLease = dhcpServerLeases.SingleOrDefault(l => client.MacAddress == l.MacAddress); txBytes.WithLabels(client.MacAddress, dhcpLease?.Address ?? "", dhcpLease?.HostName ?? "") .Set(client.TxByte); rxBytes.WithLabels(client.MacAddress, dhcpLease?.Address ?? "", dhcpLease?.HostName ?? "") .Set(client.RxByte); } }
private static void QueueTreeMerge(ITikConnection connection) { var original = connection.LoadAll <QueueTree>().Where(q => q.Name == "Q1" || q.Name == "Q2" || q.Name.StartsWith("Q3")); string unique = Guid.NewGuid().ToString(); List <QueueTree> expected = new List <QueueTree>() { new QueueTree() { Name = "Q1", Parent = "global", PacketMark = "PM1" }, new QueueTree() { Name = "Q2", Parent = "global", PacketMark = "PM2", Comment = unique }, //always update new QueueTree() { Name = "Q3 " + unique, Parent = "global", PacketMark = "PM3" }, // always insert + delete from previous run }; //Merge with Name as key - can not save via SaveListDifferences because all items in 'expected' are new (.id=null) => insert will be done, not CUD connection.CreateMerge(expected, original) .WithKey(queue => queue.Name) .Field(q => q.Parent) .Field(q => q.PacketMark) .Field(q => q.Comment) .Save(); }
private static void DnsCachePrint(ITikConnection connection) { var cache = connection.LoadAll <IpDns.DnsCache>(); foreach (var c in cache) { Console.WriteLine(c.EntityToString()); } }
/// <summary> /// Deletes all entities of given type on mikrotik router. /// </summary> /// <typeparam name="TEntity">Deleted entity type.</typeparam> /// <param name="connection">Tik connection used to delete entity.</param> /// <returns>Number of deleted entities. </returns> public static int DeleteAll <TEntity>(this ITikConnection connection) where TEntity : new() { var list = connection.LoadAll <TEntity>(); int result = list.Count(); connection.SaveListDifferences(new List <TEntity>() /*empty list as expected => delete all*/, list); return(result); }
private void btnConnect_Click(object sender, EventArgs e) { capsManSecurities = new List <CapsManSecurity>(); try { btnDisplayChangePassword.Enabled = false; connection = ConnectionFactory.OpenConnection(TikConnectionType.ApiSsl, txtHost.Text, txtUser.Text, txtPassword.Text); if ((bool)chkKeep.Checked) { Properties.Settings.Default.Host = txtHost.Text; Properties.Settings.Default.User = txtUser.Text; Properties.Settings.Default.Password = Protect(txtPassword.Text); } else { Properties.Settings.Default.Host = ""; Properties.Settings.Default.User = ""; Properties.Settings.Default.Password = ""; } Properties.Settings.Default.Save(); } catch (Exception ex) { connection = null; lbSec.DataSource = capsManSecurities; lbSec.DisplayMember = "Comment"; lbSec.ValueMember = "Id"; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); return; } var myCapsManSecurities = connection.LoadAll <CapsManSecurity>(); // only use security configurations with a comment foreach (var s in myCapsManSecurities) { if (s.Comment.Length > 0) { capsManSecurities.Add(s); btnDisplayChangePassword.Enabled = true; } } lbSec.DataSource = capsManSecurities; lbSec.DisplayMember = "Comment"; lbSec.ValueMember = "Id"; lbSec.Focus(); }
private static void AddFirewalFilter(ITikConnection connection) { var firewallFilter = new FirewallFilter() { Chain = FirewallFilter.ChainType.Forward, Action = FirewallFilter.ActionType.Accept, }; connection.Save(firewallFilter); var loaded = connection.LoadAll<FirewallFilter>().First(); loaded.Comment = "TEST"; connection.Save(loaded); }
private static void AddFirewalFilter(ITikConnection connection) { var firewallFilter = new FirewallFilter() { Chain = FirewallFilter.ChainType.Forward, Action = FirewallFilter.ActionType.Accept, }; connection.Save(firewallFilter); var loaded = connection.LoadAll <FirewallFilter>().First(); loaded.Comment = "TEST"; connection.Save(loaded); }
private static void FirewallMangleMerge(ITikConnection connection) { //manage just subset before rules marked with comment =START= and =END= //Create subset boundaries if not present const string startComment = "=START="; const string endComment = "=END="; var startMangle = connection.LoadSingleOrDefault<FirewallMangle>(connection.CreateParameter("comment", startComment)); if (startMangle == null) { startMangle = new FirewallMangle() { Chain = "forward", Action = FirewallMangle.ActionType.Passthrough, Comment = startComment, Disabled = true, }; connection.Save(startMangle); }; var endMangle = connection.LoadSingleOrDefault<FirewallMangle>(connection.CreateParameter("comment", endComment)); if (endMangle == null) { endMangle = new FirewallMangle() { Chain = "forward", Action = FirewallMangle.ActionType.Passthrough, Comment = endComment, Disabled = true, }; connection.Save(endMangle); }; //Merge subset between boundaries string unique = Guid.NewGuid().ToString(); List<FirewallMangle> original = connection.LoadAll<FirewallMangle>().SkipWhile(m=>m.Comment != startComment).TakeWhile(m=>m.Comment != endComment) .Concat(new List<FirewallMangle> { endMangle}) .ToList(); //just subset between =START= and =END= (not very elegant but functional and short ;-) ) List<FirewallMangle> expected = new List<FirewallMangle>(); expected.Add(startMangle); expected.Add(new FirewallMangle() { Chain = "forward", SrcAddress = "192.168.1.1", Action = FirewallMangle.ActionType.MarkPacket, NewPacketMark = "mark-001", Passthrough = false, }); expected.Add(new FirewallMangle() { Chain = "forward", SrcAddress = "192.168.1.2", Action = FirewallMangle.ActionType.MarkPacket, NewPacketMark = "mark-002" + "-" + unique, Passthrough = false, }); expected.Add(new FirewallMangle() { Chain = "forward", SrcAddress = "192.168.1.3", Action = FirewallMangle.ActionType.MarkPacket, NewPacketMark = "mark-003", Passthrough = false, Comment = unique, }); expected.Add(endMangle); connection.CreateMerge(expected, original) .WithKey(mangle => mangle.SrcAddress + ":" + mangle.Comment) //Use src-address as key .Field(q => q.Chain) .Field(q => q.SrcAddress) //Do not forget include also key fields !!! .Field(q => q.Action) .Field(q => q.NewPacketMark) .Field(q => q.Passthrough) .Field(q => q.Comment) .Save(); }
private static void DnsCachePrint(ITikConnection connection) { var cache = connection.LoadAll<IpDns.DnsCache>(); foreach(var c in cache) { Console.WriteLine(c.EntityToString()); } }
private static void DhcpClientRelease(ITikConnection connection) { connection.LoadAll<IpDhcpClient>().First().Release(connection); }
/// <summary> /// Load all users async /// </summary> /// <returns>The List of users</returns> public Task <IEnumerable <UserManagerUser> > LoadAllUsersAsync() => Task.Run(() => _connection.LoadAll <UserManagerUser>());
private static void DhcpClientRelease(ITikConnection connection) { connection.LoadAll <IpDhcpClient>().First().Release(connection); }
public Task <IEnumerable <HotspotUser> > LoadAllUsersAsync() => Task.Run(() => _connection.LoadAll <HotspotUser>());
private static void FirewallMangleMerge(ITikConnection connection) { //manage just subset before rules marked with comment =START= and =END= //Create subset boundaries if not present const string startComment = "=START="; const string endComment = "=END="; var startMangle = connection.LoadSingleOrDefault <FirewallMangle>(connection.CreateParameter("comment", startComment)); if (startMangle == null) { startMangle = new FirewallMangle() { Chain = "forward", Action = FirewallMangle.ActionType.Passthrough, Comment = startComment, Disabled = true, }; connection.Save(startMangle); } ; var endMangle = connection.LoadSingleOrDefault <FirewallMangle>(connection.CreateParameter("comment", endComment)); if (endMangle == null) { endMangle = new FirewallMangle() { Chain = "forward", Action = FirewallMangle.ActionType.Passthrough, Comment = endComment, Disabled = true, }; connection.Save(endMangle); } ; //Merge subset between boundaries string unique = Guid.NewGuid().ToString(); List <FirewallMangle> original = connection.LoadAll <FirewallMangle>().SkipWhile(m => m.Comment != startComment).TakeWhile(m => m.Comment != endComment) .Concat(new List <FirewallMangle> { endMangle }) .ToList(); //just subset between =START= and =END= (not very elegant but functional and short ;-) ) List <FirewallMangle> expected = new List <FirewallMangle>(); expected.Add(startMangle); expected.Add(new FirewallMangle() { Chain = "forward", SrcAddress = "192.168.1.1", Action = FirewallMangle.ActionType.MarkPacket, NewPacketMark = "mark-001", Passthrough = false, }); expected.Add(new FirewallMangle() { Chain = "forward", SrcAddress = "192.168.1.2", Action = FirewallMangle.ActionType.MarkPacket, NewPacketMark = "mark-002" + "-" + unique, Passthrough = false, }); expected.Add(new FirewallMangle() { Chain = "forward", SrcAddress = "192.168.1.3", Action = FirewallMangle.ActionType.MarkPacket, NewPacketMark = "mark-003", Passthrough = false, Comment = unique, }); expected.Add(endMangle); connection.CreateMerge(expected, original) .WithKey(mangle => mangle.SrcAddress + ":" + mangle.Comment) //Use src-address as key .Field(q => q.Chain) .Field(q => q.SrcAddress) //Do not forget include also key fields !!! .Field(q => q.Action) .Field(q => q.NewPacketMark) .Field(q => q.Passthrough) .Field(q => q.Comment) .Save(); }
public IEnumerable <Interface> GetInterfaces() { var interfaces = connection.LoadAll <Interface>(); return(interfaces); }
private static void QueueTreeMerge(ITikConnection connection) { var original = connection.LoadAll<QueueTree>().Where(q=> q.Name == "Q1" || q.Name == "Q2" || q.Name.StartsWith("Q3")); string unique = Guid.NewGuid().ToString(); List<QueueTree> expected = new List<QueueTree>() { new QueueTree() { Name = "Q1", Parent = "global", PacketMark = "PM1" }, new QueueTree() { Name = "Q2", Parent = "global", PacketMark = "PM2", Comment = unique }, //always update new QueueTree() { Name = "Q3 " + unique, Parent = "global", PacketMark = "PM3" }, // always insert + delete from previous run }; //Merge with Name as key - can not save via SaveListDifferences because all items in 'expected' are new (.id=null) => insert will be done, not CUD connection.CreateMerge(expected, original) .WithKey(queue => queue.Name) .Field(q => q.Parent) .Field(q => q.PacketMark) .Field(q => q.Comment) .Save(); }
public void VeriAl(string svIP, string svKulAdi, string svSifre, int serverId) { try { using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api)) { connection.Open(svIP, svKulAdi, svSifre); var hs = connection.LoadAll <HotspotUser>(); foreach (var user in hs) { var kuladi = user.Name; var sifre = user.Password; //connection.Delete<HotspotUser>(user); // 7 ağustos try { SqlCeConnection baglanti = new SqlCeConnection(@"Data Source=Hotspot.sdf;Persist Security Info=False;"); SqlCeCommand cmd = new SqlCeCommand(); if (baglanti.State == ConnectionState.Closed) { baglanti.Open(); } cmd.Connection = baglanti; cmd.CommandText = "select * from HotspotTBL H , ServerTBL S where H.serverId='" + serverId + "' and H.kullaniciAdi='" + kuladi + "'"; cmd.ExecuteNonQuery(); SqlCeDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { } else { dr.Close();//datareader i kapattık saatEkle = TimeSpan.FromDays(365); //KULLANICI EKLEME //gün ve saat ekleme string date = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now.Add(saatEkle));//zamanı gün olarak arttırdık if (baglanti.State == ConnectionState.Closed) { baglanti.Open(); } string kayit = "insert into HotspotTBL(serverId,kullaniciAdi,sifre,sure) values (@serverId,@kullaniciAdi,@sifre,@sure)"; SqlCeCommand komut = new SqlCeCommand(kayit, baglanti); komut.Parameters.AddWithValue("@serverId", serverId); komut.Parameters.AddWithValue("@kullaniciAdi", kuladi); komut.Parameters.AddWithValue("@sifre", sifre); komut.Parameters.AddWithValue("@sure", date); komut.ExecuteNonQuery(); baglanti.Close(); } } catch (Exception hata) { MessageBox.Show("İşlem Sırasında Hata Oluştu." + hata.Message); } // } } } catch (Exception) { MessageBox.Show("Server bağlantısı başarısız"); } }