Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

Launch Python Script by clicking a HTML button mixed with an Ajax Function

I wrote a Python script, on one side, which should let me send some commands to network devices. On the other side, i have a html page that calls an Ajax Function which is supposed to launch th Python script. Please find the code below.

  • The HTML page and the ajax function:

    <!--<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 + "
    "},
                    context: document.body
                  }).done(function() {
                   alert('finished python script');;
                  });
              }
          </script>
    
  • And the python script :

    !/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
')
            chan.send('int gi0/6
')
            time.sleep(2)
            chan.send(output.value)
            time.sleep(5)
            ###close ssh session
            client.close()
            print (ip)
            f.close()

The python script itself just works fine. As soon as i try to launch it from the text input field and click button, it doesn't work. I obtain an Internal Server Error at 9ms:

[HTTP/1.1 500 Internal Server Error 9ms]

Can you please let me know what could be the error in my code guys?

Thanks in advance and all the best ;-) !!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...