Пример #1
0
 def do_connect(self, inp):
     '''
     Connect to Mikrotik
     '''
     # To Do: Support SSH connections
     # To Do: Support api_ssl connections
     device_path = Path(Path.cwd() / '.mso_db' / 'devices' / inp)
     with open(device_path / 'local_config.json', 'r') as json_config:
         local_config = json.load(json_config)
         ip_address = local_config['ip_address']
     try:
         s = mikrotik.open_socket(dst=ip_address, port=8728)
         mtk = Mikrotik(s)
         mtk.login(username='******', pwd='')
     except:
         print('Failed to connect')
Пример #2
0
    def do_login(self, line):
        """
        Connect to device
        Usage: address user password
        """
        args = line.split()
        if len(args) < 1:
            print("Usage: login address [user] [password]")
            return
        if len(args) < 2:
            user = input("Username: "******"Password: "******"Password: ")
        else:
            user = args[1]
            passwd = args[2]

        self.m =  Mikrotik(args[0])
        self.m.login(user, passwd)
        self.prompt = '%s> ' % args[0]
Пример #3
0
__author__ = 'Dev'

from mikrotik import Mikrotik
from add_user import User

user = User()
user.add_user()
print user.enter_data
print user.sel_device
mikrotik = Mikrotik()
mikrotik.add_user_mikrotik(user.sel_device, user.enter_data)
Пример #4
0
class MikrotikCommandLoop(cmd.Cmd):

    m = None
    prompt = '> '

    def precmd(self, line):
        if self.m and line.startswith("/"):
            line = "run %s" % line
        return cmd.Cmd.precmd(self, line)

    def do_greet(self, line):
        print("Welcome to use Mikrotik remote command line tool")

    def do_EOF(self, line):
        print("")
        return True

    def do_login(self, line):
        """
        Connect to device
        Usage: address user password
        """
        args = line.split()
        if len(args) < 1:
            print("Usage: login address [user] [password]")
            return
        if len(args) < 2:
            user = input("Username: "******"Password: "******"Password: "******"""
        Run command on remote Mikrotik.
        """
        if not self.m:
            print("login first!")
            return
        args = line.split()
        if len(args) < 1:
            print("Usage: run [args]")
            return
        command = args[0]
        arguments = {}
        queries = {}
        argument_dict = arguments
        expect_number = False
        for i in args[1:]:
            if i.lower() == 'where':
                argument_dict = queries
                continue
            if i.lower() in ['print', 'add']:
                command = "%s/%s" % (command, i.lower())
                continue
            if i.lower() in ['set', 'remove']:
                command = "%s/%s" % (command, i.lower())
                expect_number = True
                continue
            if i.isdigit() and expect_number:
                argument_dict['.id'] = '*%s' % i
                expect_number=False
                continue
            expect_number = False
            try:
                (a, b) = i.split("=",1)
                argument_dict[a] = b
            except ValueError:
                print("Invalid argument %s" % i)
                return

        response = self.m.run(command, attributes=arguments, queries=queries)
        for r in response:
            if r.status == "done":
                continue
            attributes = ' '.join(["%s%s%s=%s%s%s" % (colors.BLUE, k, colors.ENDC, colors.GREEN, v, colors.ENDC) for k, v in r.attributes.items()])
            if '.id' in r.attributes:
                _id = r.attributes['.id'].lstrip("*")
                print("!%s%s%s %s%s%s %s %s" % (colors.BOLD, colors.MAGENTA, r.status, colors.RED, _id, colors.ENDC, attributes, ' '.join(r.error)))
            else:
                print("!%s%s%s%s %s %s" % (colors.BOLD, colors.MAGENTA, r.status, colors.ENDC, attributes, ' '.join(r.error)))


    def do_logout(self, line):
        if self.m:
            self.m.disconnect()
            self.prompt = "> "
        else:
            self.prompt = "> "