-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmikrotik-cmd.py
executable file
·120 lines (104 loc) · 3.44 KB
/
mikrotik-cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# encoding: utf-8
import cmd
import getpass
from mikrotik import Mikrotik, MikrotikAPIError
class colors:
MAGENTA = '\033[35m'
BLUE = '\033[34m'
GREEN = '\033[32m'
WARNING = '\033[33m'
FAIL = '\033[31m'
RED = '\033[31m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
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: ")
passwd = getpass.getpass("Password: ")
elif len(args) < 3:
user = args[1]
passwd = getpass.getpass("Password: ")
else:
user = args[1]
passwd = args[2]
self.m = Mikrotik(args[0])
self.m.login(user, passwd)
self.prompt = '%s> ' % args[0]
def do_run(self, line):
"""
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 = "> "
if __name__ == '__main__':
MikrotikCommandLoop().cmdloop()