I want to disable and enable the rule in Ip>Firewall>NAT, knowing the comment rule.
I use a API in C#, and tik4net
Here is part of my code:
private void button1_Click(object sender, EventArgs e)
{
using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
string HOST = “ipAddress”;
string USER = “login”;
string PASS = “password”;
connection.Open(HOST, USER, PASS);
var rules = connection.LoadSingle();
I get an error: tik4net.TikCommandAmbiguousResultException: “only one response item expected, returned 2 items”
If I understand correctly, I am using the wrong extension. What do I need to use the extension, and how do I disable the rule?
Why would you like to change the NAT rule?
When would you change the rule?
What button do you like to use? The mode button on the router?
Could his be done using a script on the router?
Also what button? Physical on the router or in a program on a computer?
this is a soft button on the computer, which with one click will disable the rule on the router in NAT
I need to do this on a computer, and on a mobile. So that a person can turn off access in a few clicks anywhere where there is Internet without going into Winbox.
I still managed to turn the rule on and off. But this works with the GetId () property; TikSpecialProperties.Id, that is, using the identifier that is assigned to the NAT rule. How to make a rule disconnect using a comment on this rule? Do TikSpecialProperties have this property?
Using id is not convenient, because if you re-create the rules, it changes. Or the id in the rule can somehow be changed on the side Mikrotik?
var natRule = connection.CreateCommandAndParameters(“/ip/firewall/nat/print”, “comment”, nameRule).ExecuteList();
// var result = natRule.ExecuteList();
var id = natRule.Single().GetId();
var disableRule = connection.CreateCommandAndParameters(“/ip/firewall/nat/disable”, TikSpecialProperties.Id, id);
disableRule.ExecuteNonQuery();
}
}
private void button2_Click(object sender, EventArgs e)
{
using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
string HOST = “ipAddress”;
string USER = “login”;
string PASS = “passwd”;
connection.Open(HOST, USER, PASS);
var natRule = connection.CreateCommandAndParameters(“/ip/firewall/nat/print”, “comment”, “test”).ExecuteList();
var id = natRule.Single().GetId();
var enableRule = connection.CreateCommandAndParameters(“/ip/firewall/nat/enable”, TikSpecialProperties.Id, “*8”);
enableRule.ExecuteNonQuery();
}
}
I was able to delete the established connection. But there is a problem when there are more than one connection, I get an error at the stage: var id = connect.Single().GetId();
The error sounds like this: System.InvalidOperationException: “The sequence contains more than one element”.
Please tell me how can I delete all existing established connections?
I found how to disable all established connections, but what other syntax do I need to disable only those in dst-nat ipAddress:port
var list = connection.CreateCommandAndParameters(“/ip/firewall/connection/print”, TikCommandParameterFormat.NameValue,“.proplist”, “.id”).ExecuteList();
foreach (var item in list)
{
connection.CreateCommandAndParameters(“/ip/firewall/connection/remove”, “.id”, item.Words[“.id”]).ExecuteNonQuery();
}
your doing this all wrong. use port knocking to have a script check an address-list if something is present make the changes you want. that way it is all self contained into the router and the button can just be a dump widget on an android screen or something similiar. no smart api needed.