我有一个打印函数的原始输出,如下所示:
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")
发布于 2019-03-19 13:38:44
以下是解析命令输出的一种方法:
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>
https://stackoverflow.com/questions/55241144
复制