Python 上一次迭代结果覆盖(在web上)以前的输出
在参考中,我做了一些调整,并进一步添加了一些功能 在循环中,对多个IP和单个命令进行迭代,但现在我希望tje循环对多个IP和多个命令进行迭代&为此,我又为循环添加了一个 正如在代码中一样,写入文件时一切正常,但在HttpResponse中,上一次输出的结果将覆盖上一次输出 views.pyPython 上一次迭代结果覆盖(在web上)以前的输出,python,html,django,Python,Html,Django,在参考中,我做了一些调整,并进一步添加了一些功能 在循环中,对多个IP和单个命令进行迭代,但现在我希望tje循环对多个IP和多个命令进行迭代&为此,我又为循环添加了一个 正如在代码中一样,写入文件时一切正常,但在HttpResponse中,上一次输出的结果将覆盖上一次输出 views.py 从django.exe导入渲染 从第一个app.forms导入CmdForm 从django.http导入HttpResponse,HttpResponseRedirect 导入netmiko 从netmik
从django.exe导入渲染
从第一个app.forms导入CmdForm
从django.http导入HttpResponse,HttpResponseRedirect
导入netmiko
从netmiko导入ConnectHandler
从netmiko.ssh_异常导入NetMikoTimeoutException
从paramiko.ssh_异常导入SSHException
从netmiko.ssh_异常导入AuthenticationException
导入日期时间、时间、系统
从django.url反向导入
#在这里创建您的视图。
def表单名称视图(请求):
如果request.method==“POST”:
form=CmdForm(request.POST)
如果form.is_有效():
从netmiko导入ConnectHandler
ipInsert=request.POST.get('ip_地址','')#获取多个ip
ipIns=ipInsert.split(',')#正在拆分IPs CSV
cmd=request.POST.get('command','')#调用多个命令
cmdlist=cmd.split(',')#拆分命令
对于ipIn中的ipIn:#对于IP列表中IP中的eqch
对于cmdlist中的cmdSingle:#对于命令列表中的eah命令
设备={
“设备类型”:“思科ios”,
"ip":ipIn,,
“用户名”:“mee”,
“密码”:“12345”,
“秘密”:“12345”,
}
尝试:
netconnect=ConnectHandler(**设备)
除外(AuthenticationException):
re='身份验证失败。!请重试{}。格式(ipIn)
打印(re)
返回呈现(请求,'first_app/forms.html',{'form':form,'reprinting':re})
通过
除(特殊情况):
re='SSH问题。您确定已启用SSH吗?{}格式(ipIn)
打印(re)
返回呈现(请求,'first_app/forms.html',{'form':form,'reprinting':re})
通过
除了(NetMikoTimeoutException):
re='设备{}超时。'格式(ipIn)
打印(re)
返回呈现(请求,'first_app/forms.html',{'form':form,'reprinting':re})
通过
除(EOR)外:
re='尝试设备{}时文件结束。'格式(ipIn)
打印(re)
返回呈现(请求,'first_app/forms.html',{'form':form,'reprinting':re})
通过
除了作为未知错误的异常:
re='someothererror{}'。格式(未知错误)
打印(re)
返回呈现(请求,'first_app/forms.html',{'form':form,'reprinting':re})
通过
输出=netconnect.send_命令(cmdSingle)#向路由器发送命令
now=time.strftime(“%Y_%m_%d_u%H_%m_%S”)#在web上打印命令
file=sys.stdout#txt文件中的标准输出
file=open(“C:/Users/OneDrive/Desktop/frontend/”+now+“\uuuu”+ipIn+“.txt”,mode='a+”)#打开并附加文件
file.write(“IP地址为\n”+ipIn)#将IP地址写入文件
file.write(“\n\n执行的命令:\n”+cmdSingle)#将命令写入文件
文件。写入(“\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”)
file.write(“\n\n已执行命令的输出:\n\n\n”+输出)#将输出写入文件
file.close
返回render(请求,'first_app/forms.html',{'form':form,'output':output,'getIP':ipIn,
“日期时间”:现在,“ipList”:IPIN,
“cmdlist”:cmdlist,“cmdSingle”:cmdSingle})#web上的http响应
其他:
form=CmdForm()
返回呈现(请求,'first_app/forms.html',{'form':form})
其他:
返回呈现(请求,'first_app/forms.html',{})
forms.html
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>FORMS</title>
<style>
table, th, td {
style=width:100%;
max-height:100px;
border: 1px solid black;
}
#lengthy{
max-height:50px;
overflow-y: scroll;
height:50px;
}
</style>
</head>
<body>
<h1> Run Commands </h1>
<form method="POST"> {% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }} <br>
{{ field.label_tag }} <br/>
{{ field }}
</div>
{% endfor %}
<br><br>
<input type="submit" value="Click Here to run Commands" />
<br>
{% if request.POST %}
<pre>{{ reprinting }}</pre>
{% endif %}
<br>
{% if request.POST %}
{% csrf_token %}
<table>
<tr>
<th>Current date and time</th>
<th>IP address </th>
<th>Command Executed </th>
<th>Output</th>
</tr>
{% for getIP in ipList %}
{% for cmdSingle in cmdlist %}
<tr>
<td style="width:20%"> {{ date_time }} </td>
<td style="width:15%"><pre> {{ getIP }} </pre></td>
<td style="width:20%"><pre> {{ cmdSingle }} </pre></td>
<td id="lengthy" style="width:90%"><pre>{{ output }}</pre></td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% endif %}
</form>
</body>
</html>
{%load staticfiles%}
形式
表,th,td{
样式=宽度:100%;
最大高度:100px;
边框:1px纯黑;
}
#冗长的{
最大高度:50px;
溢出y:滚动;
高度:50px;
}
运行命令
{%csrf_令牌%}
{%形式的字段为%}
{{field.errors}}
{{field.label_tag}}
{{field}}
{%endfor%}
{%if request.POST%}
def form_name_view(request):
if request.method == "POST":
form = CmdForm(request.POST)
if form.is_valid():
# rest of the code
output_list = list()
for ipIn in ipIns:
for cmdSingle in cmdlist:
# try catch block
output = netconnect.send_command(cmdSingle)
output_list.append([ipIn, cmdSingle, output]) # appending ip, cmd and output as a list to output list
# rest of the code
return render(request,'first_app/forms.html', {'form': form, 'output_list':output_list, 'date_time':now})
{{重印}
{%endif%}
{%if request.POST%}
def form_name_view(request):
if request.method == "POST":
form = CmdForm(request.POST)
if form.is_valid():
# rest of the code
output_list = list()
for ipIn in ipIns:
for cmdSingle in cmdlist:
# try catch block
output = netconnect.send_command(cmdSingle)
output_list.append([ipIn, cmdSingle, output]) # appending ip, cmd and output as a list to output list
# rest of the code
return render(request,'first_app/forms.html', {'form': form, 'output_list':output_list, 'date_time':now})
{%csrf_令牌%}
当前日期和时间
IP地址
执行命令
输出
{ipList%中getIP的%s}
{cmdlist%中cmdSingle的%
{{date\u time}
{{getIP}}