🔴 Blink in proportion to the number of active ssh sessions! ⚪
Because plain blink sample is super boring. And also this kind of useful!
#!/usr/bin/env python | |
from subprocess import Popen, PIPE | |
from shlex import split | |
import OPi.GPIO as GPIO | |
from time import sleep | |
pin = 7 | |
def number_of_active_ssh(): | |
p1 = Popen(split("ps ax"), stdout=PIPE) | |
p2 = Popen(split("grep sshd"), stdin=p1.stdout, stdout=PIPE) | |
p2 = Popen(split("grep -v grep"), stdin=p2.stdout, stdout=PIPE) | |
lines = p2.stdout.readlines() | |
if len(lines) < 2: | |
return 0 | |
else: | |
return len(lines)-1 | |
# Setting up GPIO | |
GPIO.setboard(GPIO.ZERO) | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(pin, GPIO.OUT) | |
# Main loop | |
try: | |
print ("Press CTRL+C to exit") | |
while True: | |
active_sessions = number_of_active_ssh() | |
if active_sessions == 0: | |
GPIO.output(pin, 0) | |
sleep(1) | |
# print("No Active Session") | |
else: | |
# print("There are total of {} active session(s).".format(active_sessions)) | |
blink_rate = 1/(2.0*active_sessions) | |
for i in range(active_sessions): | |
GPIO.output(pin, 1) | |
sleep(blink_rate) | |
GPIO.output(pin, 0) | |
sleep(blink_rate) | |
except KeyboardInterrupt: | |
GPIO.output(pin, 0) | |
GPIO.cleanup() |