netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.83k stars 2.54k forks source link

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

Closed romarioZ1000 closed 2 years ago

romarioZ1000 commented 2 years ago

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

julianstolp commented 2 years ago

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/)