//-------------------------------------------Function--------------------------------------//
        //
        // getGroup
        //
        private List <string> getGroup()
        {
            commandRows.Add("/user/group/print");
            List <string> groupName = new List <string>();

            if (commandRows.Any())
            {
                List <string> rows = new List <string>();
                foreach (string row in commandRows)
                {
                    rows.AddRange(row.Split('|').Where(r => !string.IsNullOrEmpty(r)));
                }
                var result = connection.CallCommandSync(rows.ToArray());
                foreach (var resultItem in result)
                {
                    string g = "";
                    foreach (var word in resultItem.Words)
                    {
                        if (word.Key.Equals("name"))
                        {
                            g = word.Value;
                        }
                    }
                    groupName.Add(g);
                }
                commandRows.Clear();
            }
            return(groupName);
        }
        //
        // Execute Command
        //
        private List <string> ExecuteCommand(List <string> command)
        {
            List <string> walledGarden = null;

            if (commandRows.Any())
            {
                List <string> rows = new List <string>();
                foreach (string row in commandRows)
                {
                    rows.AddRange(row.Split('|').Where(r => !string.IsNullOrEmpty(r)));
                }
                var result = connection.CallCommandSync(rows.ToArray());
                commandRows.Clear();
            }
            return(walledGarden);
        }
Exemple #3
0
 //
 // Execute Command
 //
 private void ExecuteCommand(string commandStr)
 {
     if (!string.IsNullOrWhiteSpace(commandStr))
     {
         commandRows.Add(commandStr);
     }
     if (commandRows.Any())
     {
         List <string> rows = new List <string>();
         foreach (string row in commandRows)
         {
             rows.AddRange(row.Split('|').Where(r => !string.IsNullOrEmpty(r)));
         }
         var result = connection.CallCommandSync(rows.ToArray());
         commandRows.Clear();
     }
 }
Exemple #4
0
        private List <string> getUsername()
        {
            List <string> nameList = new List <string>();
            var           result   = connection.CallCommandSync("/user/print");

            foreach (var resultItem in result)
            {
                foreach (var item in resultItem.Words)
                {
                    if (item.Key.Equals("name"))
                    {
                        nameList.Add(item.Value);
                    }
                }
            }
            return(nameList);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
            {
                connection.OnReadRow  += Connection_OnReadRow;
                connection.OnWriteRow += Connection_OnWriteRow;

                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                var configuration = builder.Build();

                connection.Open(configuration["host"], configuration["user"], configuration["pass"]);

                List <string> commandRows = new List <string>();
                string        command;
                Console.WriteLine("Write command and press [ENTER] on empty line");
                Console.WriteLine("Empty command + [ENTER] stops console.");
                do
                {
                    command = Console.ReadLine();

                    if (!string.IsNullOrWhiteSpace(command))
                    {
                        commandRows.Add(command);
                    }
                    else
                    {
                        if (commandRows.Any())
                        {
                            List <string> rows = new List <string>();
                            foreach (string row in commandRows)
                            {
                                rows.AddRange(row.Split('|').Where(r => !string.IsNullOrEmpty(r)));
                            }
                            var result = connection.CallCommandSync(rows.ToArray());
                            foreach (var resultItem in result)
                            {
                                foreach (var word in resultItem.Words)
                                {
                                    Console.WriteLine($"{word.Key}={word.Value}");
                                }
                            }

                            commandRows.Clear();
                        }
                        else
                        {
                            break; //empty row and empty command -> end
                        }
                    }
                }while (true);
            }

            Console.WriteLine("Press [ENTER] to close.");
            Console.ReadLine();
        }
Exemple #6
0
        //
        // Get User
        //
        private List <User> getUsers(List <string> command)
        {
            List <User> users = new List <User>();

            if (command.Any())
            {
                List <string> rows = new List <string>();
                foreach (string row in commandRows)
                {
                    rows.AddRange(row.Split('|').Where(r => !string.IsNullOrEmpty(r)));
                }
                var result = connection.CallCommandSync(rows.ToArray());
                foreach (var resultItem in result)
                {
                    User u = new User();
                    foreach (var word in resultItem.Words)
                    {
                        if (word.Key.Equals("name"))
                        {
                            u.name = word.Value;
                        }
                        else if (word.Key.Equals("group"))
                        {
                            u.group = word.Value;
                        }
                        else if (word.Key.Equals("last-logged-in"))
                        {
                            u.lastLoggedIn = word.Value;
                        }
                        else if (word.Key.Equals("disabled"))
                        {
                            u.disabled = word.Value;
                        }
                        u.address = "";
                    }
                    users.Add(u);
                }
                commandRows.Clear();
            }
            return(users);
        }
Exemple #7
0
        static void Main(string[] args)
        {
            using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
            {
                connection.OnReadRow  += Connection_OnReadRow;
                connection.OnWriteRow += Connection_OnWriteRow;
                connection.Open(ConfigurationManager.AppSettings["host"], ConfigurationManager.AppSettings["user"], ConfigurationManager.AppSettings["pass"]);

                List <string> commandRows = new List <string>();
                string        command;
                Console.WriteLine("MIKROTIK CONTROLLER");
                Console.WriteLine("Write command and press [ENTER] on empty line");
                Console.WriteLine("Empty command + [ENTER] stops console.");
                do
                {
                    command = Console.ReadLine();

                    if (!string.IsNullOrWhiteSpace(command))
                    {
                        commandRows.Add(command);
                    }
                    else
                    {
                        if (commandRows.Any())
                        {
                            List <string> rows = new List <string>();
                            foreach (string row in commandRows)
                            {
                                rows.AddRange(row.Split('|').Where(r => !string.IsNullOrEmpty(r)));
                            }
                            var result = connection.CallCommandSync(rows.ToArray());
                            foreach (var resultItem in result)
                            {
                                foreach (var word in resultItem.Words)
                                {
                                    Console.WriteLine(word);
                                }
                            }

                            commandRows.Clear();
                        }
                        else
                        {
                            break; //empty row and empty command -> end
                        }
                    }
                }while (true);
            }

            Console.WriteLine("Press [ENTER] to close.");
            Console.ReadLine();
        }