3

i want to create real time cli i'm sending commandes with the input (netmiko) and i recive the results in the text aria but it doesn't work my script :

<script type="text/javascript">
            $(document).('submit','#post-form',function(e)){
                e.preventDefault();
                $.ajax({
                    type:'POST',
                    url:'cli_run',
                    data:{
                        cmds:$('cmds').val(),
                        csrfmiddlewaretoken:$('input[cmds=csrfmiddlewaretoken]').val(),
                    },
                    outputs: function(data){
                        $('h2').html(data);}});}
        </script> -->

and this is my template :

div class="row">
        <div class="card col-12 col-md-6">
            <div class="card-header">
            <label> <h3>ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ Cli : </h3></label>
            </div>
            <br>
            <div class="card-body text-center">
            <form action="{% url 'cli_run' device.id%}" method="POST">
            {% csrf_token %}
            <textarea name="cmds" id="cmds" rows="10" cols="80"></textarea>
            <input type="submit" name="Submit">
            </form>
            </div>
        </div>



        <div class="card col-12 col-md-6">
            <div class="card-header">
            <label> <h3>output : </h3></label>
            </div>
            <br>
            <div class="card-body text-center">
            <textarea  rows="10" cols="80"> {% for i in outputs %} {{i}}     {% endfor %}    </textarea>
            </div>
        </div>

and this is my view , i connect with the decice with 'RTR' and i use a list (output ) to show the results

def cli_run(request, pk ):
    device = Device.objects.get(id=pk)

    RTR = {'ip': device.ipadress,'username': device.hostname,'password': 
     device.password,'device_type': 'cisco_ios',}
       net_connect = ConnectHandler(**RTR)
      output = net_connect.send_command(request.POST.get("cmds"))
      outputs.append(output)
    
    context = {'device':device,'output':output,'RTR':RTR,'outputs':outputs,"devices":devices}    
    return render(request, 'accounts/cli_run.html',context)
  • Hi, here `$('cmds').val()` you are missing `#` i.e : `$('#cmds').val()` , `$('input[cmds=csrfmiddlewaretoken]')` it should be `$('input[name=csrfmiddlewaretoken]').val()` and `outputs` should be `success` . After changing this all show output of `data` i.e : `console.log(data)` you will see result inside your browser console. – Swati Jun 10 '21 at 04:30

0 Answers0