Configure Network Devices via SSH (for example Mikrotik) #6533

Closed
opened 2025-12-29 19:42:02 +01:00 by adam · 1 comment
Owner

Originally created by @romarioZ1000 on GitHub (Jun 3, 2022).

NetBox version

v3.2.2

Feature type

New functionality

Proposed functionality

Hello, netbox community!

I have an idea to create scripts to manage network devices from web interface.

for example, here is a ready-made simple script for adding VLAN to Mikrotik.

2022-06-03 094552

###########################################

#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 = "Set VLAN on Mikrotik"
description = "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']
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)

mik1 = {
    "device_type": "mikrotik_routeros",
    "host": data['input_ip'],
    "username": "admin+ct",
    "password": "admin",
}

with ConnectHandler(**mik1) as net_connect:
    output = net_connect.send_command('/interface print \n')

    self.log_success("Mikrotik via SSH")
return ''.join(output)

for line in stdout:
    print(line.strip('\n'))
client.close()

###################################################

2022-06-03 094632

In this script, I enter variables myself, but I would like to enter variables from the netbox database -
for example through ObjectVar or MultiObjectVar.

############################################

devices = ObjectVar(
    model=Device,
)

Interface_in = ObjectVar(
    model=Interface,
)

Interface_out = ObjectVar(
    model=Interface,
)

Interfacebridge = ObjectVar(
    model=Interface,
)

vlan_id = MultiObjectVar(
    model=VLAN,
    label='VLAN (ID)',
)

#################################

2022-06-03 135614

I can't pass these variables to commands that need to be written on the remote device !
'/interface bridge add name='+[bridge from Interfacebridge ] + '\n'

Can you tell me, at least with one example, how can I do this?
All scripts will be laid out in open form, so that everyone can use them!

Thanks a lot!

Use case

Use netbox as a platform for template configuration of network devices using the netbox database.
(Mikrotik, Cisco etc)

Database changes

No response

External dependencies

No response

Originally created by @romarioZ1000 on GitHub (Jun 3, 2022). ### NetBox version v3.2.2 ### Feature type New functionality ### Proposed functionality Hello, netbox community! I have an idea to create scripts to manage network devices from web interface. for example, here is a ready-made simple script for adding VLAN to Mikrotik. <img width="536" alt="2022-06-03 094552" src="https://user-images.githubusercontent.com/67051805/171839994-7bde0f23-ff6f-46bf-92b1-a522c141ccde.png"> ########################################### #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 = "Set VLAN on Mikrotik" description = "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'] 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) mik1 = { "device_type": "mikrotik_routeros", "host": data['input_ip'], "username": "admin+ct", "password": "admin", } with ConnectHandler(**mik1) as net_connect: output = net_connect.send_command('/interface print \n') self.log_success("Mikrotik via SSH") return ''.join(output) for line in stdout: print(line.strip('\n')) client.close() ################################################### <img width="518" alt="2022-06-03 094632" src="https://user-images.githubusercontent.com/67051805/171840159-a4de97f1-c779-4085-a300-5c8f580f6d00.png"> In this script, I enter variables myself, but I would like to enter variables from the netbox database - for example through ObjectVar or MultiObjectVar. ############################################ devices = ObjectVar( model=Device, ) Interface_in = ObjectVar( model=Interface, ) Interface_out = ObjectVar( model=Interface, ) Interfacebridge = ObjectVar( model=Interface, ) vlan_id = MultiObjectVar( model=VLAN, label='VLAN (ID)', ) ################################# <img width="509" alt="2022-06-03 135614" src="https://user-images.githubusercontent.com/67051805/171841203-ddc44e87-9192-4895-88be-19c2ff0ccbb3.png"> I can't pass these variables to commands that need to be written on the remote device ! '/interface bridge add name='+[bridge from Interfacebridge ] + '\n' Can you tell me, at least with one example, how can I do this? All scripts will be laid out in open form, so that everyone can use them! Thanks a lot! ### Use case Use netbox as a platform for template configuration of network devices using the netbox database. (Mikrotik, Cisco etc) ### Database changes _No response_ ### External dependencies _No response_
adam closed this issue 2025-12-29 19:42:02 +01:00
Author
Owner

@julianstolp commented on GitHub (Jun 3, 2022):

I guess this topic should be posted in https://github.com/netbox-community/netbox/discussions/categories/ideas next time.
Anyway, inside these scripts you have complete access to the netbox database via the netboxshell (see documentation: https://docs.netbox.dev/en/stable/administration/netbox-shell/)

@julianstolp commented on GitHub (Jun 3, 2022): I guess this topic should be posted in https://github.com/netbox-community/netbox/discussions/categories/ideas next time. Anyway, inside these scripts you have complete access to the netbox database via the netboxshell (see documentation: https://docs.netbox.dev/en/stable/administration/netbox-shell/)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6533