-1

Here's the code

In 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:')

In 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', {})

however i m getting the error:-

NetMikoTimeoutException at /Automation_page/ Connection to device timed-out: cisco_ios ip_address:22 Request Method: POST Request URL: http://127.0.0.1:8000/Automation_page/ Django Version: 2.2.3 Exception Type: NetMikoTimeoutException Exception Value:
Connection to device timed-out: cisco_ios ip_address:22 Exception Location: C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib\site-packages\netmiko\base_connection.py in establish_connection, line 864 Python Executable: C:\Users\karti\AppData\Local\Programs\Python\Python37-32\python.exe Python Version: 3.7.3 Python Path:
['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']

why i m getting the time-out though my devices are up and running.

thanx to those willing to help.! :-)

Mystery
  • 81
  • 11
  • Possible duplicate of [How do I get user IP address in django?](https://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django) – heemayl Jul 10 '19 at 07:46
  • nope. its not a duplicate as the code in link (if u have observed) relates to getting user ip address by their location (not manual typing) and in my question i want to get ip address to be typed by user as i m an automation engineer. – Mystery Jul 10 '19 at 07:51

2 Answers2

1

probably the line

'ip':'ip_address',

should read

'ip': form.cleaned_data['ip_address'],
tato
  • 5,439
  • 4
  • 31
  • 27
0

the only change i did is:-

from netmiko import ConnectHandler
        ipInsert = request.POST.get('ip_address', '')
        devices = {
        'device_type':'cisco_ios',
        'ip':ipInsert,
        'username':'mee',
        'password':'12345',
        'secret':'12345',

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

and it works just as i wanted ;-)

Mystery
  • 81
  • 11