Created
April 28, 2015 18:55
-
-
Save openfly/2c230566ab65b2f1fb15 to your computer and use it in GitHub Desktop.
BCF demo cgi script for interop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF-8 -*-# enable debugging | |
import cgitb | |
import sys | |
import json | |
import requests | |
import pprint | |
import codecs | |
import getpass | |
cgitb.enable() | |
print("Content-Type: text/html;charset=utf-8\n\n") | |
pp = pprint.PrettyPrinter(indent=4, width=30, depth=3) | |
bcf_url = "URI of BCF REST API" | |
bcf_user = "USERNAME" | |
bcf_pass = "PASSWORD" | |
def authenticate(): | |
header = {'Content-type':'application/json'} | |
data = {"password":str(bcf_pass),"user":str(bcf_user)} | |
r = requests.post(bcf_url + "auth/login", data=json.dumps(data), headers=header, verify=False) | |
authObj = json.loads(r.text) | |
if authObj["session_cookie"]: | |
return authObj["session_cookie"] | |
else: | |
return "FAIL" | |
def show_version(): | |
header= {'Content-type':'application/json'} | |
header["Cookie"] = "session_cookie=%s" % session_cookie | |
r = requests.get(bcf_url + "data/controller/core/version/appliance", data="{}", headers=header, verify=False) | |
return r.text | |
def show_endpoints(): | |
header= {'Content-type':'application/json'} | |
header["Cookie"] = "session_cookie=%s" % session_cookie | |
r = requests.get(bcf_url + "data/controller/applications/bcf/info/endpoint-manager/endpoint", data="{}", headers=header, verify=False) | |
return r.text | |
def show_inventory(): | |
header= {'Content-type':'application/json'} | |
header["Cookie"] = "session_cookie=%s" % session_cookie | |
r = requests.get(bcf_url + "data/controller/applications/bcf/info/fabric/switch", data="{}", headers=header, verify=False) | |
return r.text | |
session_cookie = authenticate() | |
if session_cookie == "FAIL": | |
print "authentication failed" | |
sys.exit(1) | |
a = show_version() | |
b = json.loads(a) | |
for bs in b: | |
name = bs["name"] | |
version = bs["version"] | |
print "<HTML>" | |
print "<HEAD>" | |
print "<TITLE>" | |
print " A little BCF demo python cgi hitting our REST API at Interop! " | |
print "</TITLE>" | |
print "</HEAD>" | |
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/styles.css\">" | |
print "<link type=\"text/css\" href=\"css/css3.css\" rel=\"stylesheet\">" | |
print "<BODY>" | |
print "<h1> %s </h1>" % name | |
print "<p><i> Version: %s </i></p>" % version | |
a = show_inventory() | |
#pp.pprint(a) | |
b = json.loads(a) | |
print "<h2> Frabric Inventory </h2>" | |
print "<table>" | |
print "<tr class=\"alt\">" | |
print "<td>Switch Model</td>" | |
print "<td>Fabric Role</td>" | |
print "<td>State</td>" | |
print "<td>Last Seen</td>" | |
print "</tr>" | |
for bs in b: | |
# pp.pprint(bs) | |
model = bs["model-number-description"] | |
role = bs["fabric-role"] | |
state = bs["fabric-connection-state"] | |
last_seen = bs["fabric-last-seen-time"] | |
print "<tr class=\"alt\">" | |
print "<td>%s </td>" % model | |
print "<td>%s </td>" % role | |
print "<td>%s </td>" % state | |
print "<td>%s&nsbp;</td>" % last_seen | |
print "</tr>" | |
print "</table>" | |
print "</body>" | |
print "</html>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment