问题描述
一方面,我编写了一个 Python 脚本,它应该可以让我向网络设备发送一些命令。另一方面,我有一个 html 页面,它调用一个应该启动 Python 脚本的 Ajax 函数。 请在下面找到代码。
-
<!--<h1 style="font-size:10px"> Launch Python script to Cisco Devices </h1>--> <input name="command" type="text" id="command"> <input type="button" style="background-color:#3F7FBF;color:white;border-radius: 25%;" id='script' name="scriptbutton" value="Run Script Cisco" onclick="cisco_script()"></br> <script src="http://code.jquery.com/jquery-3.3.1.js"></script> <script> function cisco_script(){ $.ajax({ type:"POST",url: "cisco_script.py",data: {param: document.getElementById('command').value + "\n"},context: document.body }).done(function() { alert('finished python script');; }); } </script>
-
还有python脚本:
!/usr/bin/env python
import sys
import time
import paramiko
import os
import cmd
import datetime
import cgi,cgitb
cgitb.enable() # display error messages in web browser
Create instance of FieldStorage
data = cgi.FieldStorage()
Get data from fields
output = data["param"]
set date and time
Now = datetime.datetime.Now()
authentication
USER = 'login'
PASSWORD = 'password'
start FOR ...in
f = open('cisco.device.list2')
for ip in f.readlines():
ip = ip.strip()
###session start
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip,username=USER,password=PASSWORD)
###ssh shell
chan = client.invoke_shell()
time.sleep(2)
###send new config
chan.send('conf t\n')
chan.send('int gi0/6\n')
time.sleep(2)
chan.send(output.value)
time.sleep(5)
###close ssh session
client.close()
print (ip)
f.close()
python 脚本本身运行良好。 一旦我尝试从文本输入字段启动它并单击按钮,它就不起作用。我在 9 毫秒 时收到内部服务器错误:
[HTTP/1.1 500 Internal Server Error 9ms]
提前致谢,祝一切顺利;-) !!!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)