无法通过 flask 将表格发送到 HTML

问题描述 投票:0回答:0

我正在尝试获取开关接口的最后输入时间并通过 flask 将其发布到 html 但即使我设法获得了列表,我也无法将其推送到 html。

我的错误是什么?

import logging
from netmiko import ConnectHandler
from contextlib import redirect_stdout
from flask import Flask, render_template
import logging

app = Flask(__name__)

@app.route('/')

# Enable debug logging for Netmiko and write to a file
def index():
    logging.basicConfig(filename='netmiko.log', level=logging.DEBUG)
    device = {
    'device_type':'cisco_ios_telnet',
    'ip': '10.10.20.250',
    'username': 'admin',
    'password': 'cisco',
    'port':23,
}

with ConnectHandler(**device) as net_connect:
    # Enter enable mode
    net_connect.enable()

    # Send "show interfaces" command and get output
    show_output = net_connect.send_command('show interfaces')

    net_connect.disconnect()

interfaces_list = []
for line in show_output.splitlines():
    if line.startswith('Gigabit'):
        # extract the interface name
        interface_name = line.split()[0]
        # extract the last input time
    if line.startswith('  Last input '):
        last_input_time = line.split('  Last input ')[1].split(',')[0]
        interfaces_list.append(f"{interface_name} - Last input {last_input_time}")
    else:
        continue    
        
return render_template('index.html', interfaces=interfaces_list)

if __name__ == '__main__':
    app.run(debug=True)  

这是我的 html 代码(在模板下保存为 index.html)

    <!DOCTYPE html>
    <html>
    <head>
        <title>Show Interfaces</title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>Interface</th>
                    <th>Last Input</th>
                </tr>
            </thead>
            <tbody>
                {% for interface in interfaces %}
                <tr>
                    <td>{{ interface.interface }}</td>
                    <td>{{ interface.last_input }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </body>
    </html>

对于那些不知道show interfaces的输出是什么的人,我会给你这个例子

     GigabitEthernet3/0 is up, line protocol is up (connected)
     Hardware is iGbE, address is 0c82.6bf0.000c (bia 0c82.6bf0.000c)
     Description: VPC
     MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec,
        reliability 255/255, txload 1/255, rxload 1/255
     Encapsulation ARPA, loopback not set
     Keepalive set (10 sec)
     Auto Duplex, Auto Speed, link type is auto, media type is RJ45
     output flow-control is unsupported, input flow-control is unsupported
     ARP type: ARPA, ARP Timeout 04:00:00
     Last input 00:21:33, output 00:00:01, output hang never
     Last clearing of "show interface" counters never
     Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
     Queueing strategy: fifo
     Output queue: 0/0 (size/max)
     5 minute input rate 0 bits/sec, 0 packets/sec
     5 minute output rate 0 bits/sec, 0 packets/sec
        7 packets input, 1816 bytes, 0 no buffer
        Received 6 broadcasts (6 multicasts)
        0 runts, 0 giants, 0 throttles
        0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
        0 watchdog, 6 multicast, 0 pause input
        749 packets output, 60803 bytes, 0 underruns
        0 output errors, 0 collisions, 2 interface resets
        0 unknown protocol drops
        0 babbles, 0 late collision, 0 deferred
        0 lost carrier, 0 no carrier, 0 pause output
        0 output buffer failures, 0 output buffers swapped out
     GigabitEthernet3/1 is up, line protocol is up (connected)
     Hardware is iGbE, address is 0c82.6bf0.000d (bia 0c82.6bf0.000d)
     Description: VPC
     MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec,

提前谢谢你

html python-3.x flask cisco
© www.soinside.com 2019 - 2024. All rights reserved.