Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. from flask import Flask, request, Response, render_template
  2. import ssh_client
  3. import helpers
  4. import json
  5. import routers
  6.  
  7. app = Flask(__name__)
  8.  
  9. # main route where we render the html
  10. @app.route('/', methods=['GET'])
  11. def home():
  12. return render_template('index.html')
  13.  
  14. # GET route in order to fetch the available routers for a given ASN
  15. @app.route('/routers/<asn>', methods=['GET'])
  16. def get_routers(asn):
  17. results = []
  18. for r in routers.routers_list:
  19. if r['asn'] == asn:
  20. # only return the address/hostname, the location and the OS type of the matching routers
  21. results.append(dict(name=r['address'][0],
  22. location=r['location'],
  23. type=r['type'])
  24. )
  25. return json.dumps(results)
  26.  
  27. # POST route in order to invoke the looking glass service
  28. @app.route('/lg', methods=['POST'])
  29. def lg():
  30. # get parameters from the request body
  31. req_data = request.get_json()
  32. # obtain the router object and the ready-to-enter command
  33. router, command = helpers.get_vars(req_data['router'], req_data['cmd'], req_data['ipprefix'])
  34. # instantiate our SSH_Client class
  35. client = ssh_client.SSH_Client(router)
  36. # run the command
  37. output_stream = client.run(command)
  38. # generator function
  39. def generate():
  40. for chunk in output_stream:
  41. yield chunk
  42. # close the underlaying transport session of the ssh client
  43. client.close()
  44. # each yield iteration in generate() is sent directly to the browser
  45. return Response(generate())
  46.  
  47. if __name__ == '__main__':
  48. app.run(host='0.0.0.0', debug=True, port=5000)
Add Comment
Please, Sign In to add comment