首页 > 解决方案 > 想要从用户那里获取 IP 地址以手动输入(而不是通过他们的位置)

问题描述

这是代码

在forms.py

from django import forms

class CmdForm(forms.Form):
    ip_address = forms.CharField(label='Enter IP address:')
    command = forms.CharField(label='Command to execute:')

在 Views.py 中

from django.shortcuts import render
from first_app.forms import CmdForm
from django.http import HttpResponse

def index(request):
 my_dict = {'insert_me': ""}
 return render(request,'first_app/index.html',context=my_dict)

def form_name_view(request):
   if request.method == "POST":
     form = CmdForm(request.POST)
       if form.is_valid():
        from netmiko import ConnectHandler

        devices = {
        'device_type':'cisco_ios',
        'ip':'ip_address',
        'username':'mee',
        'password':'12345',
        'secret':'12345',

        }
        ipInsert = request.POST.get('ip_address', '')
        cmd = request.POST.get('command', '')
        netconnect = ConnectHandler(**devices)
        #print("connection established with", devices['ip'])
        getIP = netconnect.send_command(ipInsert)
        output = netconnect.send_command(cmd)

        return render(request,'first_app/forms.html', {'form': form, 'output':output, 'getIP':getIP})
       else:
        form = CmdForm()
       return render(request,'first_app/forms.html', {'form': form})
   else:
       return render(request,'first_app/forms.html', {})

但是我得到了错误: -

NetMikoTimeoutException at /Automation_page/ 连接到设备超时:cisco_ios ip_address:22 请求方法:POST 请求 URL:http: //127.0.0.1 :8000/Automation_page/ Django 版本:2.2.3 异常类型:NetMikoTimeoutException 异常值:
连接到设备超时:cisco_ios ip_address:22 异常位置:C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib\site-packages\netmiko\base_connection.py 在建立连接,第 864 行 Python 可执行文件: C:\Users\karti\AppData\Local\Programs\Python\Python37-32\python.exe Python 版本:3.7.3 Python 路径:
['K:\Work\DevNet\first_project', 'C:\Users\karti\AppData\Local\Programs\Python\Python37-32\python37.zip', 'C:\Users\karti\AppData\Local\Programs \Python\Python37-32\DLLs', 'C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib', 'C:\Users\karti\AppData\Local\Programs\Python\Python37 -32', 'C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib\site-packages']

为什么我的设备已启动并正在运行,但我却超时了。

感谢那些愿意提供帮助的人。!:-)

标签: pythonhtmldjango

解决方案


可能是这条线

'ip':'ip_address',

应该读

'ip': form.cleaned_data['ip_address'],

推荐阅读