如何使用python flas以Html格式打印网络设备输出

2024-04-29 07:13:44 发布

您现在位置:Python中文网/ 问答频道 /正文

t以下是设备的输出

接受密码

acr01.aurora.co#显示版本

底盘类型:E6000

插槽说明硬件版本软件版本正常运行时间

0 UCAM-2备用UCAM-22431W/C10 CER_V06.04.00.0026 21天3:49:52

1 UCAM-2 UCAM-22431W/C10 CER_V06.04.00.0026 21天4:24:35

2 UCAM-2 UCAM-22431W/C10 CER_V06.04.00.0026 21天4:10:38

3 UCAM-2 UCAM-22431W/C10 CER_V06.04.00.0026 21天3:54:56

4 DCAM-B DCAM-08256W/D14 CER_V06.04.00.0026 21天4:24:37

5 DCAM-B DCAM-08256W/D05 CER_V06.04.00.0026 21天4:10:40

6 RSM A RSM-08241W/D03 CER_V06.04.00.0026 21天4:33:32

7 RSM B RSM-08241W/D08 CER_V06.04.00.0026 21天3:15:42

8 DCAM-B DCAM-08256W/D08 CER_V06.04.00.0026 21天3:54:58

9 DCAM-B DCAM-08256W/D05 CER_V06.04.00.0026 21天3:41:26

10 DCAM-B DCAM-08256W/D05 CER_V06.04.00.0026 21天3:36:09

11 DCAM-B DCAM-08256W/D05 CER_V06.04.00.0026 21天3:30:52

12 DCAM-B DCAM-08256W/D05 CER_V06.04.00.0026 21天3:25:35

13 DCAM-B备用DCAM-08256W/D05 CER_V06.04.00.0026 21天3:23:38

acr01.aurora.co#出口 我的代码是

from flask import Flask
from flask import render_template
import telnetlib
app = Flask(__name__)

@app.route("/")
def home():


    HOST = 'acr01.aurora.co.denver.comcast.net'
    user = 'username'+"\n"
    password = 'password'+"\n"


    tn = telnetlib.Telnet(HOST,23,50)
    tn.set_debuglevel(1)
    tn.read_until(b'Username: ')
    tn.write(user.encode('ascii'))
    tn.read_until(b'Enter PASSCODE: ')
    tn.write(password.encode('ascii'))
    #tn = telnetlib.Telnet(HOST)


    #cmd='show version'+"\n"
    tn.write(b'show version'+b"\n")
    #t1=(tn.read_eager())
    #print(t1)
    #cmd1='exit' + "\n"
    tn.write(b'exit'+b"\n")
    #t2=tn.read_eager()
    #print(t2)

    """while True:
        line = tn.read_until(b"\n")  # Read one line
        print(line)"""


    output=tn.read_all()
    """for row in output:
        print(row)
        tn.read_until(b"\n")"""
    return render_template("home.html",output=output)

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

home.html

<html>
  <body>
    <h1>Add book</h1>
    <form method="POST" action="/">



    <h1>Books</h1>
    <table>
      {% for book in output %}
        <tr>
          <td>
            {{book[0]}}
          </td>
          <td>
            {{book[1]}}
            {{book[1]}}
            {{book[2]}}
            {{book[3]}}
            {{book[4]}}
          </td>

        </tr>
      {% endfor %}
    </table>
    </form>
  </body>
</html>

Tags: readoutputhtmlh1tnwriteuntilprint