Hello à toutes et à tous.
Je cherche à lancer un script lorsque je débranche le câble Ethernet de ma carte.
J’ai mis ceci dans le fichier :
/etc/network/interfaces
#DHCP
source /etc/network/interfaces.d/*
# Ethernet
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
post-up sudo bash /usr/local/bin/accueil.sh
post-down sudo bash /usr/local/bin/accueil.sh
# Local loopback
auto lo
iface lo inet loopback
# Wifi
allow-hotplug wlan0
iface wlan0 inet static
address 10.0.0.1
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
dns-nameservers 8.8.8.8
post-up sudo bash /usr/local/bin/accueil.sh
post-down sudo bash /usr/local/bin/accueil.sh
Je pensais que le fait de débrancher le câble créerait un post-down.
Mais ce n’est pas le cas
Le but est de mettre à jour en ‘temps réel’ les pictogrammes de l’écran de ma Raspberry pi :
J’en ai un pour le wifi et un pour l’éthernet.
Voici le code python pour ‘piloter’ l’écran :
#!/usr/bin/env python
import time
import sys
from gfxhat import lcd, backlight, fonts
from PIL import Image, ImageFont, ImageDraw
#Parametres
wifi_etat = sys.argv[1]
wifi_ssid = sys.argv[2]
eth_etat = sys.argv[3]
eth_mode = sys.argv[4]
#Allume les backlights
lcd.clear()
backlight.set_all(237,127,16)
backlight.show()
lcd.clear()
#Texte
width, height = lcd.dimensions()
image = Image.new('P', (width, height))
draw = ImageDraw.Draw(image)
#menu
text = "Menu"
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 100
y = 50
draw.text((x, y), text, 1, font)
#Infos
text2 = "Info"
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 60
y = 50
draw.text((x, y), text2, 1, font)
#Test debit
text3 = "Test debit"
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 0
y = 50
draw.text((x, y), text3, 1, font)
#SSID
if wifi_etat =="up":
if wifi_ssid =='nope':
text4 = "Pas d'acces wifi"
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 30
y = 30
draw.text((x, y), text4, 1, font)
else:
text5 = "SSID :"
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 50
y = 25
draw.text((x, y), text5, 1, font)
text4 = wifi_ssid
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 05
y = 35
draw.text((x, y), text4, 1, font)
else:
text4 = "Wifi non active"
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 30
y = 30
draw.text((x, y), text4, 1, font)
#Eth mode
if eth_etat =="up":
text5 = eth_mode
else:
text5 = " "
font = ImageFont.truetype("/diagbox/systeme/lcd/ttf/OpenSans-Regular.ttf", 10)
x = 60
y = 05
draw.text((x, y), text5, 1, font)
for x in range(128):
for y in range(64):
pixel = image.getpixel((x, y))
lcd.set_pixel(x, y, pixel)
#Images
width, height = lcd.dimensions()
image6 = Image.new('L', (width, height))
image6 = Image.open("/diagbox/systeme/lcd/logo/ethernet.bmp")
if eth_etat =="up":
for x6 in range(20):
for y6 in range(20):
pixel6 = image6.getpixel((x6, y6))
if pixel6 ==0:
pixel6 = 1
else:
pixel6 = 0
lcd.set_pixel(x6+30, y6, pixel6)
image7 = Image.new('L', (width, height))
image7 = Image.open("/diagbox/systeme/lcd/logo/wifi.bmp")
if wifi_etat =="up":
for x7 in range(20):
for y7 in range(20):
pixel7 = image7.getpixel((x7, y7))
if pixel7 ==0:
pixel7 = 1
else:
pixel7 = 0
lcd.set_pixel(x7, y7, pixel7)
lcd.show()
Et le code bash qui lance le code python, car j’ai besoin de récupérer des infos :
#!/bin/bash
#Variables
readonly error_log=/diagbox/web/tmp/error.log
readonly led_or_lcd=/tmp/led_or_lcd.txt
readonly wifi_etat=/tmp/wifi_etat
readonly eth_etat=/tmp/eth_etat
readonly wifi_ssid=/tmp/wifi_ssid
readonly eth_mode=/etc/network/interfaces
readonly hostapd_fichier=/etc/hostapd/hostapd.conf
readonly script_accueil=/diagbox/systeme/scripts/principal/accueil.py
read result_led_or_lcd <$led_or_lcd
wifietat='etat wifi'
ethetat='etat eth'
wifissid='ssid'
ethmode='eth mode'
#Efface les traces précédentes
if [ -f $error_log ];then
echo "suppression Logs erreur"
sudo rm $error_log
fi
if [ -f $wifi_etat ];then
echo "suppression log wifi etat"
sudo rm $wifi_etat
fi
if [ -f $eth_etat ];then
echo "suppression log eth etat"
sudo rm $eth_etat
fi
if [ -f $wifi_ssid ];then
echo "suppression log eth mode"
sudo rm $wifi_ssid
fi
#Verifier etat wlan0
sudo ip link ls dev wlan0 >> $wifi_etat
if sudo grep "state UP" $wifi_etat;then
echo "wifi up"
wifietat="up"
sudo rm $wifi_etat
else
echo "wifi down"
wifietat="down"
sudo rm $wifi_etat
fi
#Verifier etat eth0
sudo ip link ls dev eth0 >> $eth_etat
if sudo grep "state UP" $eth_etat;then
echo "eth up"
ethetat="up"
sudo rm $eth_etat
else
echo "eth down"
ethetat="down"
sudo rm $eth_etat
fi
#Recuperer ssid
if [ -f $hostapd_fichier ];then
echo "recuperation ssid"
if grep ssid /etc/hostapd/hostapd.conf | cut -d= -f2 >> $wifi_ssid;then
echo "recuperation reussie"
read wifissid <$wifi_ssid
else
echo "Echec recuperation ssid" >> $error_log
wifissid="nope"
fi
sudo rm wifi_ssid
else
wifissid="nope"
fi
#Recuperer eth mode
echo "recuperation eth mode"
read ethmode <$eth_mode
if [ $result_led_or_lcd = "led" ];then
echo "led"
if [ $ethmode = "#ONT" ];then
sudo python /diagbox/web/scripts/gpio/led_eth0_ont.py
fi
if [ $ethmode = "#RESCUE" ];then
sudo python /diagbox/web/scripts/gpio/led_eth0_rescue.py
fi
if [ $ethmode = "#DHCP" ];then
sudo python /diagbox/web/scripts/gpio/led_eth0_dhcp_et_static.py
fi
if [ $ethmode = "#STATIC" ];then
sudo python /diagbox/web/scripts/gpio/led_eth0_dhcp_et_static.py
else
sudo python /diagbox/web/scripts/gpio/led_eth0_off.py
fi
fi
if [ $result_led_or_lcd = "lcd" ];then
echo "lcd"
echo $wifietat
echo $wifissid
echo $ethetat
echo $ethmode
sudo python $script_accueil $wifietat $wifissid $ethetat $ethmode
else
echo "Echec pour déterminer si il y a des leds ou un lcd sur les gpio" >> $error_log
exit
fi
exit
En vous remerciant par avance