简体   繁体   English

显示RAW打印输出的表格视图 - Django

[英]Display table view of RAW print output - Django

I have a raw output from a print function which looks something like the below: 我有一个打印功能的原始输出,看起来像下面的样子:

b'\r\nsw-dektec#\r\nsw-dektec#terminal length 0\r\nsw-dektec#sh mvr members\r\nMVR Group IP        Status         Member          Membership \r\n-------------------------------------------------------------\r\n232.235.000.001     ACTIVE/UP      Gi1/0/21        Dynamic    \r\n232.235.000.002     ACTIVE/UP      Gi1/0/21        Dynamic    \r\n232.235.000.003     ACTIVE/UP      Gi1/0/21        Dynamic

I want to parse the above txt and only display 232.235.000.x when i click a button on my webpage. 我想解析上面的txt,并且当我单击网页上的按钮时仅显示232.235.000.x。

And am checking if we can display the output in the below format: 我正在检查是否可以按以下格式显示输出:

Multicast IP
------------
232.235.000.001

232.235.000.002

232.235.000.003

Here is my view.py so far: 到目前为止,这是我的view.py:

if 'RETRIEVE' in request.POST: 如果在request.POST中'RETRIEVE':

  remote_conn_pre = paramiko.SSHClient()
  remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  remote_conn_pre.connect(hostname='172.31.255.4', port=22, username='admin',
                        password='******',
                        look_for_keys=False, allow_agent=False)

  remote_conn = remote_conn_pre.invoke_shell()


  remote_conn.send("\n")


  remote_conn.send("terminal length 0\n")
  remote_conn.send("sh mvr members\n")
  time.sleep(1)
  iptv = remote_conn.recv(65535)
  print (iptv)
  for line in iptv:

      remote_conn.send("end\n")
      remote_conn.send("exit\n")

Here's one way you could parse the command output: 这是解析命令输出的一种方法:

iptv = remote_conn.recv(65535)

ips, rec = [], False
for line in iptv.decode('utf-8').split('\r\n'):
    if '---' in line:
        rec = True
    elif rec:
        ip, *_ = line.split() 
        ips.append(ip)

remote_conn.send("end\n")
remote_conn.send("exit\n")

If you're looking to render that on your web page, then you'd need to send the parsed IP addresses to a template where you could construct a simple HTML table. 如果您希望在网页上呈现该内容,则需要将解析后的IP地址发送到可以构建简单HTML表的模板。

return render(request, 'ip_address_template.html', {
    'ips': ips
})

The ip_address_template.html template may look something like this: ip_address_template.html模板可能如下所示:

<table>
    <th>Multicast IP</th>
    {% for ip in ips %}
        <tr><td>{{ ip }}</td></tr>
    {% endfor %}
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM