Dispaly IP address in binary during boot
This commit is contained in:
parent
0ce13fc9c8
commit
fab04d1b15
91
monitor.py
91
monitor.py
|
@ -1,46 +1,83 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# version 1.0
|
||||
#
|
||||
#
|
||||
# For debugging we use the bottom left corner pixel and:
|
||||
#
|
||||
# show red dot failed to get data
|
||||
# show red dot if failed to get data
|
||||
# show blue dot if failed to refresh the cookie.
|
||||
# show orage when we start refreshing data starts.
|
||||
# show yellow when initial login fails.
|
||||
# show orange when we start refreshing data.
|
||||
# show yellow when login fails.
|
||||
#
|
||||
#
|
||||
# ver 1.1 added dispaly of ip address on boot in binary format.
|
||||
#
|
||||
#
|
||||
|
||||
|
||||
import time
|
||||
import json
|
||||
import requests
|
||||
import time
|
||||
import unicornhat as unicorn
|
||||
import urllib3
|
||||
import socket
|
||||
import commands
|
||||
|
||||
print("Run colours based on data")
|
||||
urllib3.disable_warnings()
|
||||
|
||||
unicorn.set_layout(unicorn.PHAT)
|
||||
unicorn.rotation(180)
|
||||
unicorn.brightness(0.4)
|
||||
height,width=unicorn.get_shape()
|
||||
|
||||
# show one blue pixel to indcatie the script started
|
||||
unicorn.set_pixel(3,1,0,0,255)
|
||||
unicorn.set_pixel(4,1,0,0,255)
|
||||
unicorn.set_pixel(3,2,0,0,255)
|
||||
unicorn.set_pixel(4,2,0,0,255)
|
||||
|
||||
# login details for your APIC
|
||||
|
||||
apic = '10.50.138.215'
|
||||
username = 'aci-health'
|
||||
password = 'aci-health'
|
||||
|
||||
|
||||
|
||||
# show blue pixels in each corner
|
||||
def show_icon(timedel):
|
||||
unicorn.set_pixel(0,0,0,0,255)
|
||||
unicorn.set_pixel(0,3,0,0,255)
|
||||
unicorn.set_pixel(7,0,0,0,255)
|
||||
unicorn.set_pixel(7,3,0,0,255)
|
||||
unicorn.show()
|
||||
time.sleep(timedel)
|
||||
|
||||
|
||||
|
||||
# show ipv4 address in binary on the 4x8 LEDs
|
||||
def show_ip(timedel):
|
||||
for address in filter(None, commands.getoutput("hostname -I").split(" ")):
|
||||
show_icon(1)
|
||||
try:
|
||||
print "processing: ",address
|
||||
for octetidx,octet in enumerate(address.split(".")):
|
||||
bin_octet="{0:08b}".format(int(octet))
|
||||
for idx,c in enumerate(bin_octet[::-1]):
|
||||
if c == "0":
|
||||
unicorn.set_pixel(idx,octetidx,0,255,0)
|
||||
elif c == "1":
|
||||
unicorn.set_pixel(idx,octetidx,255,0,0)
|
||||
print "display: ", address
|
||||
unicorn.show()
|
||||
time.sleep(timedel)
|
||||
except:
|
||||
print "skip ipv6"
|
||||
pass
|
||||
unicorn.clear()
|
||||
unicorn.show()
|
||||
return()
|
||||
|
||||
# some colour defs
|
||||
red=(255,0,0)
|
||||
orange=(255,128,0)
|
||||
green=(0,255,0)
|
||||
|
||||
# login details for your APIC
|
||||
|
||||
apic = '1.1.1.1'
|
||||
username = 'admin'
|
||||
password = 'cisco123'
|
||||
|
||||
#
|
||||
# get data from APIC function
|
||||
#
|
||||
|
@ -136,14 +173,14 @@ def loginAPIC():
|
|||
while r is None:
|
||||
try:
|
||||
r = requests.post( url, data=json.dumps(auth), timeout=5, verify=False )
|
||||
print "status ",r.status_code
|
||||
print "logging in, status ",r.status_code
|
||||
cook = r.cookies
|
||||
print "cook is:",cook
|
||||
except:
|
||||
unicorn.clear()
|
||||
unicorn.set_pixel(0,0,255,255,0)
|
||||
unicorn.show()
|
||||
print "timeout logging in, but we will keep trying"
|
||||
show_ip(5)
|
||||
pass
|
||||
|
||||
return(cook)
|
||||
|
@ -152,8 +189,22 @@ def loginAPIC():
|
|||
# Main program loop starts here
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
# Perform initial login, later we will need to refresh the cookie frequently
|
||||
print "Stared monitorig script..."
|
||||
|
||||
show_icon(1)
|
||||
|
||||
print "waiting for network"
|
||||
|
||||
while len(filter(None, commands.getoutput("hostname -I").split(" "))) < 1:
|
||||
print "Waiting for at least 1 ip"
|
||||
|
||||
print "got an ip address"
|
||||
|
||||
# show ip addresses for 10s (1s delay between if more IPs found)
|
||||
|
||||
show_ip(10)
|
||||
|
||||
# Perform initial login, later we will need to refresh the cookie frequently
|
||||
|
||||
cook = loginAPIC()
|
||||
|
||||
|
|
Loading…
Reference in New Issue