Aug-04-2020, 12:40 AM
Hello. I have a python script that SSH's (Netmiko) into a Cisco switch, runs an "show interface status" command and outputs it to the screen. When run the script from the CLI the output looks normal on a line by line bases. If I put this same script into a Flask setup, I get the output but the problem is there is no line by line separation as in CLI. It's all crammed together. So my question is, is there a way to format Flask to output it as it does on the CLI but in HTML? Below is my code and the outputs from both CLI and Flask. Thank you in advance.
my_flask_app.py file
my_flask_app.py file
from flask import Flask, request from processing import do_calculation app = Flask(__name__) app.config["DEBUG"] = True @app.route("/", methods=["GET", "POST"]) def adder_page(): if request.method == "POST": number1 = None number1 = str(request.form["number1"]) if number1 is not None: result = do_calculation(number1) return ''' <html> <body> {result} </body> </html> '''.format(result=result) return ''' <html> <body> <p>Enter switch IP:</p> <form method="post" action="."> <p><input name="number1" /></p> <p><input type="submit" value="Check Status" /></p> </form> </body> </html> '''Processing.py file
from netmiko import ConnectHandler def do_calculation(number1): iosv_l2 = {'device_type': 'cisco_ios', 'username': 'user1', 'password': 'password123', 'ip': number1} net_connect = ConnectHandler(**iosv_l2) result = net_connect.send_command('show int status') return resultOutput from Flask - not desired
Output:Port Name Status Vlan Duplex Speed Type Gi0/1 1st Flr AP notconnect 1 auto auto 10/100/1000BaseTX Gi0/2 2nd Flr AP connected trunk a-full a-1000 10/100/1000BaseTX Gi0/3 notconnect 1 auto auto 10/100/1000BaseTX Gi0/4 ESXi Server connected 101 a-full a-100 10/100/1000BaseTX Gi0/5 Ooma VoIP connected 101 a-full a-100 10/100/1000BaseTX Gi0/6 Ring Alarm connected 101 a-full a-100 10/100/1000BaseTX Gi0/7 1st Flr AP connected trunk a-full a-1000 10/100/1000BaseTX Gi0/8 Bench notconnect 101 auto auto 10/100/1000BaseTX Gi0/9 notconnect 1 auto auto 10/100/1000BaseTX Gi0/10 Living Room TV connected 101 a-full a-100 10/100/1000BaseTX Gi0/11 notconnect 1 auto auto 10/100/1000BaseTX Gi0/12 notconnect 1 auto auto 10/100/1000BaseTX Gi0/13 trunk to VM Server connected trunk a-full a-100 10/100/1000BaseTX Gi0/14 notconnect 1 auto auto 10/100/1000BaseTX Gi0/15 to lab-s1 connected trunk a-full a-1000 10/100/1000BaseTX Gi0/16 notconnect 1 auto auto 10/100/1000BaseTX Gi0/17 notconnect 1 auto auto 10/100/1000BaseTX Gi0/18 notconnect 1 auto auto 10/100/1000BaseTX Gi0/19 notconnect 1 auto auto 10/100/1000BaseTX Gi0/20 notconnect 1 auto auto 10/100/1000BaseTX Gi0/21 pfSense Trunk Re0 notconnect 1 auto auto 10/100/1000BaseTX Gi0/22 Office switch connected trunk a-full a-1000 10/100/1000BaseTX Gi0/23 to OPT1 (RE0) on F connected trunk a-full a-1000 10/100/1000BaseTX Gi0/24 to LAN (RE2) on FW connected 101 a-full a-1000 10/100/1000BaseTX Gi0/25 notconnect 1 auto auto Not Present Gi0/26 notconnect 1 auto auto Not Present Gi0/27 notconnect 1 auto auto Not Present Gi0/28 notconnect 1 auto auto Not Present
Output from CLI - desiredOutput:Interface IP-Address OK? Method Status Protocol
Vlan1 unassigned YES unset administratively down down
Vlan101 192.168.1.2 YES manual up up
GigabitEthernet0/1 unassigned YES unset down down
GigabitEthernet0/2 unassigned YES unset up up
GigabitEthernet0/3 unassigned YES unset down down
GigabitEthernet0/4 unassigned YES unset up up
GigabitEthernet0/5 unassigned YES unset up up
GigabitEthernet0/6 unassigned YES unset up up
GigabitEthernet0/7 unassigned YES unset up up
GigabitEthernet0/8 unassigned YES unset down down
GigabitEthernet0/9 unassigned YES unset down down
GigabitEthernet0/10 unassigned YES unset up up
GigabitEthernet0/11 unassigned YES unset down down
GigabitEthernet0/12 unassigned YES unset down down
GigabitEthernet0/13 unassigned YES unset up up
GigabitEthernet0/14 unassigned YES unset down down
GigabitEthernet0/15 unassigned YES unset up up
GigabitEthernet0/16 unassigned YES unset down down
GigabitEthernet0/17 unassigned YES unset down down
GigabitEthernet0/18 unassigned YES unset down down
GigabitEthernet0/19 unassigned YES unset down down
GigabitEthernet0/20 unassigned YES unset down down
GigabitEthernet0/21 unassigned YES unset down down
GigabitEthernet0/22 unassigned YES unset up up
GigabitEthernet0/23 unassigned YES unset up up
GigabitEthernet0/24 unassigned YES unset up up
GigabitEthernet0/25 unassigned YES unset down down
GigabitEthernet0/26 unassigned YES unset down down
GigabitEthernet0/27 unassigned YES unset down down
GigabitEthernet0/28 unassigned YES unset down down