我有一个打印功能的原始输出,看起来像下面这样:
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
我想解析上面的txt,并且当我单击网页上的按钮时仅显示232.235.000.x。
正在检查是否可以以以下格式显示输出:
Multicast IP
------------
232.235.000.001
232.235.000.002
232.235.000.003
到目前为止,这是我的view.py:
如果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")
答案 0 :(得分:0)
这是解析命令输出的一种方法:
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")
如果要在网页上呈现它,则需要将已解析的IP地址发送到模板,以在其中构建简单的HTML表。
return render(request, 'ip_address_template.html', {
'ips': ips
})
ip_address_template.html
模板可能看起来像这样:
<table>
<th>Multicast IP</th>
{% for ip in ips %}
<tr><td>{{ ip }}</td></tr>
{% endfor %}
</table>