dotfiles/config.X11/i3blocks/blocks/ethernet

42 lines
834 B
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import subprocess
import sys
label = ''
color_up = '#9ec600'
color_down = '#676767'
if __name__ == "__main__":
output = subprocess.check_output(
"ip -br -j addr show",
shell=True
)
json_str = output.decode('utf8')
ifaces = json.loads(json_str)
ethernet = list(filter(lambda x: x['ifname'].startswith('e'),
ifaces))
ethernet_up = list(filter(lambda x: x['operstate'] == 'UP',
ethernet))
if not ethernet:
sys.exit(0)
eth = None
if not ethernet_up:
eth = ethernet.pop()
else:
eth = ethernet_up.pop()
print(label)
print(label)
if eth['operstate'] == 'DOWN':
print(color_down)
else:
print(color_up)