0

由于 netmiko 指定应该有一个设备类型、IP 地址、用户名和密码,所以我创建了一个名为 devices 的模型,我想要做的是当我单击表中创建的每个设备时,它应该使用四个凭据执行 netmiko例如所选或单击的特定设备的设备类型、IP 地址、用户名和密码,它应该允许我键入可以通过按下按钮执行的命令

你能帮我写代码吗

Model.py
class Device(models.Model):
    CISCO1 = 1
    CISCO2 = 2
    CISCO3 = 3
    CISCO4 = 4
    DEVICE_TYPES = (
        (CISCO1, 'cisco_ios'),
        (CISCO2, 'cisco_nxos_ssh'),
        (CISCO3, 'cisco_s300'),
        (CISCO4, 'cisco_tp_tcce'),
    )
    device_name = models.CharField(max_length=50)
    publication_date = models.DateField(null=True)
    IP_address = models.CharField(max_length=50)
    username = models.CharField(max_length=30)
    password = models.CharField(max_length=30)
    device_type = models.PositiveSmallIntegerField(choices=DEVICE_TYPES)
    timestamp = models.DateField(auto_now_add=True, auto_now=False)

    def __str__(self):
        return self.device_name
View.py

This is the connection code but I am confused how to get data from the Model (Device) and pass it here or when I select a device from the view it should get the credential to this method

def connection_manage(request):
    if request.method == "POST":
        form = CommandForm(request.POST)
        if form.is_valid():
            from netmiko import ConnectHandler
            device = {}
            device['device_type'] = 'cisco_ios'
            device['ip'] = 'DESKTOP-CT4RSIT'
            device['username'] = ''
            device['password'] = ''
            cmd = request.POST.get('command', '')
            conn = ConnectHandler(**device)
            output = conn.send_command(cmd)
            return render(request, 'connect.html', {'form': form, 'output': output})
    else:
        form = CommandForm()
        return render(request, 'connect.html', {'form': form})
form.py

class CommandForm(BSModalForm):
    command = forms.CharField(label='Command to execute')
    class Meta:
        model = Device
        exclude = ['timestamp', 'publication_date', 'device_type']
connect.html

{% load static %}
<!doctype html>

<head>
    <title>Mannai Co.</title>
    <link href="{% static 'assets/css/bootstrap.min.css' %}" rel="stylesheet">
    <link rel="stylesheet" href="{% static 'assets/css/login.css' %}">
</head>

<body>
    <div class="wrapper fadeInDown">

        <div id="formContent">

            <div class="fadeIn first">
                <br>
                <img src="{% static 'assets/img/manni-png.png' %}" id="icon" alt="User Icon">
            </div>
            <h3 class="fadeIn second">Netminko APP</h3>
            <p>Run command:</p>
            <form method="POST">
                {% csrf_token %}
                {{form}}
                <br>
                <input type="submit" value="Run command!" class="fadeIn fourth" />
            </form>
            {% if request.POST %}
            <p>Command output:</p>
            <pre>{{ output }}</pre>
            {% endif %}

        </div>
    </div>
    <script src="{% static 'assets/js/bootstrap.min.js' %}"></script>
    <script src="{% static 'assets/js/jquery.min.js' %}"></script>

</body>

</html>

urs.py path('execute/',connection_manage, name='execute_device'),

当我点击这个时需要第一个设置它应该使我能够手动输入所有详细信息并按执行 b

手动输入所有字段并执行这是来自网站的视图,但无论我输入什么,它仍然会连接到 cisco_ios xxx

输出 zzz

当我单击每个字段按钮时需要的第二个设置它应该弹出一个模式,该模式当前正在处理填充的所有字段,并且当我键入和命令并执行它应该为指定的设备运行

桌子

跑步

4

1 回答 1

0

这不起作用:

device['ip'] = 'DESKTOP-CT4RSIT'

这要么需要:

device['host'] = fqdn.domain.com

或者

device['ip'] = 1.2.3.4      # The IP address of the device
于 2020-01-13T19:04:13.007 回答