-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_vlan_on_Mikrotik.py
More file actions
62 lines (51 loc) · 2.12 KB
/
add_vlan_on_Mikrotik.py
File metadata and controls
62 lines (51 loc) · 2.12 KB
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
#add netmiko to the local_requirements.txt
from django.utils.text import slugify
import paramiko
import netbox.settings
from extras.scripts import *
from netmiko import ConnectHandler
from routeros_ssh_connector import MikrotikDevice
class RunCommand(Script):
class Meta:
name = "VLAN on Mikrotik"
description = "Configure Mikrotik via SSH"
field_order = [
'input_ip',
'input_bridge',
'input_vlan_name',
'input_vlan_id'
]
input_ip = StringVar(
description="Enter the IP Address:"
)
input_bridge = StringVar(
description="Name Bdrige:"
)
input_vlan_name = StringVar(
description="Vlan Name:"
)
input_vlan_id = StringVar(
description="Vlan id:"
)
def run(self, data, commit):
command = '/interface bridge add name='+data['input_bridge'] + '\n'
command1 = '/interface vlan add interface=ether1 name='+data['input_vlan_name'] + '' ' vlan-id='+data['input_vlan_id'] + '\n'
command2 = '/interface bridge port add bridge='+data['input_bridge'] + '' ' interface='+data['input_vlan_name'] + '' 'add bridge='+data['input_bridge'] + '' ' interface=ether2 \n'
command3 = '/interface bridge port add bridge='+data['input_bridge'] + 'interface=ether2 \n'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(data['input_ip'], username='admin+ct80h', password='admin')
stdin, stdout, stderr = client.exec_command(command + command1 + command2 + command3)
cisco1 = {
"device_type": "mikrotik_routeros",
"host": data['input_ip'],
"username": "admin+ct",
"password": "admin",
}
with ConnectHandler(**cisco1) as net_connect:
output = net_connect.send_command('/interface print \n')
self.log_success("Configured Mikrotik via SSH")
return ''.join(output)
for line in stdout:
print(line.strip('\n'))
client.close()