Community discussions

MikroTik App
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

How to display information without "!done\!re"?

Mon Jan 09, 2017 6:10 pm

Hello
I wrote a simple api code that give some information about my router
dns name\ip address\content of file

when I run it with visual express using F5 I get the commnad screen and everything is working

my quesuon is -
how do I make this code a bat file so I can run this from every computer?
and if in the bat file I would see the full answer
!done ssid=test
!re....
!done....

or just the answer I'm getting ?
ssid=test

Thanks ,
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to display information without "!done\!re"?

Mon Jan 09, 2017 6:36 pm

What language are you writing your app with?

If it has a portable runtime (e.g. PHP), you could just bundle the runtime itself, and make the bat file call the runtime with your file.

Alternatively, use C# - the .NET framework is preinstalled on Windows, so there's no need to install anything. Your compiled executable will suffice.


Either way, the API protocol is too complex to be used by a bat file alone.
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

Re: How to display information without "!done\!re"?

Tue Jan 10, 2017 9:45 am

I'm using C#

why do you say it's complicated?
can't I just run the code(as bat file) to show me the data I want?

again - my code is very simple (for now)
just connect to the router (you need to give IP,User,Pass) and print all IP address

?


Thanks ,
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: How to display information without "!done\!re"?

Tue Jan 10, 2017 1:37 pm

Since you're using Visual (Studio) Express, an executable is automatically generated when running the program (F5). Look in your project folder and then somewhere bin\Debug.

C# specific questions are not really aimed at this forum.. However, as explained in another post, there is a NuGet package tik4net which is much easier to use and eliminates 90% of your code.

Also, if you have code specific questions, you should post the relevant code :)
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

Re: How to display information without "!done\!re"?

Tue Jan 10, 2017 4:45 pm

OK -
so I have another question
what is my mistake while I'm trying to change the SSID ?
            mikrotik.Send("/interface/wireless/set");
            mikrotik.Send("?name=wlan1");
            mikrotik.Send("=ssid=IHaveChangeThis!");
while if I do the same to cahgne the identity it's working :
 mikrotik.Send("/system/identity/set");
 mikrotik.Send("=name=Change ID-Test");
?

Thanks ,
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: How to display information without "!done\!re"?

Tue Jan 10, 2017 5:07 pm

You cannot use query words in set statement.

First, retrieve the id:

Command: /interface/wireless/print
Response: !re.=.id=*D

Then, set the ssid:

Command: /interface/wireless/set
Parameter: =.id=*D and =ssid=test
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

Re: How to display information without "!done\!re"?

Tue Jan 10, 2017 5:28 pm

I think I missing something...

I did what you told my - but i get nothing =
mikrotik.Send("/interface/wireless/print");
            foreach (string h in mikrotik.Read())
            {
                Console.WriteLine(h);
            }
the console just stuck - what I did wrong?
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: How to display information without "!done\!re"?

Tue Jan 10, 2017 8:34 pm

You need to close the command (end sentence).
mikrotik.Send("/interface/wireless/print");
Should be:
mikrotik.Send("/interface/wireless/print", true);
Did I mention you better take a look at the tik4net NuGet package? :)
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

Re: How to display information without "!done\!re"?

Wed Jan 11, 2017 10:02 am

1. I still didn't take a look at tik4net NuGet package - I'm a newbe at all this and I wanted to start from the easiest way.
would you say this package much more "user friendly" command way?

2. so to see I understand it correct
when I enter "true" after a command it's like pressing "Enter"? or the "true" is like end of command? and I need to put it in the last command of send.mikrotik of the same command?

3. I have print and got ID=*5 - is there any way to print the info without knowing the ID ?
let say I replace wlan card -wouldn't it change the ID?
something like [find where nambers=0]?

Thanks ,


Thanks ,
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: How to display information without "!done\!re"?

Thu Jan 12, 2017 12:31 pm

1. If you don't take the time to take a look at this package or google for it, then I shouldn't take the time to reply.

2.

"End sentence" is a boolean parameter in the send function:
        public void Send(string co, bool endsentence)
        {
            byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
            byte[] velikost = EncodeLength(bajty.Length);
            connection.Write(velikost, 0, velikost.Length);
            connection.Write(bajty, 0, bajty.Length);
            connection.WriteByte(0);
        }
Effectively this sends a "null" character to the MT (connection.WriteByte(0)).
(Well, it seems that it will always send a null character regardless of the endsentence value, but I don't think anyone will explicitly use endsentence = false in this function overload)

3.
I don't understand the question.
The command "print" is exactly the command you should make to get info without knowing the ID.
You can then use the acquired ID to make modifications in subsequent commands, if necessary.
 
User avatar
danikf
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Mon Mar 14, 2011 8:57 am

Re: How to display information without "!done\!re"?

Tue Jan 17, 2017 9:56 pm

Hi,
in this situation (modifying wireless interface) you can use name in position of id. So the code will be easy.
            mikrotik.Send("/interface/wireless/set");
            mikrotik.Send("=ssid=IHaveChangeThis!");
            mikrotik.Send(".id=wlan1");            
But (as author of tik4net library) I have to recomend it :-)
using tik4net;
// ...
// create and open connection first
using(var connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS))
{
  // update the wlan intrerface with ADO.NET like API
  var updateCommand = connection.CreateCommandAndParameters("/interface/wireless/set",
        "ssid", "test_ssid",
         ".id", "wlan1");
  updateCommand.ExecuteNonQuery();
}
Take a look at samples:
https://github.com/danikf/tik4net/wiki/ ... r-all-APIs
https://github.com/danikf/tik4net/wiki/ ... et-library

Enjoy,
D
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

Re: How to display information without "!done\!re"?

Wed Mar 22, 2017 9:41 pm

I'm sorry for the late response....
I had everything working - but then my computer stop working
and all my files + explaining + my own code gone :-(
(I didn't have any backup - I'm an IDIOT!)

I just need a reminder about setting new value
I'm still using the basic API in C# (I just needed it to work and after it I will try the tik4net library)
I got stuck here :
 private void SendNewSetting_Click(object sender, EventArgs e)
        {
            // find out the .id of the SIM interface
            mikrotik.Send("/interface/ppp-client/print");
            mikrotik.Send("?name=" + SIM);
            mikrotik.Send("=.proplist=.id", true);
            string temp = mikrotik.Read()[0];
            temp = temp.Substring(8);
            textBox2.ForeColor = System.Drawing.Color.Black;
            textBox2.Text = (temp);

            mikrotik.Send("/interface/ppp-client/set");
            mikrotik.Send("=.id=" + temp);
            mikrotik.Send("=user=" + NewUser);
            mikrotik.Send("=password=" + NewPassword);
            mikrotik.Send("=apn=" +NewApn , true );
            foreach (string h in mikrotik.Read())
            {
             //   textBox2.Text = ("1" + h);
            }
    }
when I print I can see the id = *7

but he doesn't change me the value
why?
I don't remember what I have done last time...

and when I do this in Console , it's working
Console.WriteLine("Enter SIM number - ");
            string SIM = Console.ReadLine();
            Console.WriteLine("Enter new APN - ");
            string NewApn = Console.ReadLine();
            mikrotik.Send("/interface/ppp-client/print");
            mikrotik.Send("?name=" + SIM);
            mikrotik.Send("=.proplist=.id", true);
            string temp = mikrotik.Read()[0];
            temp = temp.Substring(8);
            

            mikrotik.Send("/interface/ppp-client/set");
            mikrotik.Send("=.id=" + temp);
            mikrotik.Send("=apn=" + NewApn, true);
            foreach (string h in mikrotik.Read())
            {
              //  Console.WriteLine(h);
            }
Thanks ,

Who is online

Users browsing this forum: GoogleOther [Bot] and 76 guests