The idea is reduce energy, radio emission, heat emission: make a handles scripts that leave switched-on only one interface with a minimal usage of network script.
The simplest way is just to create a cron script that enable or disable interface in order to leave eth0 up, or wifi if eth0 is down.
What do we need to perform that? it is necessary to know if the interface have a valid ip then disable the other accordly:
Get the IP from interface
The main script /sbin/ifconfig
provide all the information:
pi@reflu:~ $ /sbin/ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.178.34 netmask 255.255.255.0 broadcast 192.168.178.255
inet6 fe80::375d:dd72:2185:6c17 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:b9:90:a5 txqueuelen 1000 (Ethernet)
RX packets 10089732 bytes 15115613319 (14.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5222734 bytes 356142008 (339.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
but here we have too many information, so need to filter it 😉
pi@reflu:~ $ /sbin/ifconfig eth0 | grep 'netmask' | awk '{ print $2 }'
192.168.178.34
do we need really the IP? maybe no, maybe just need to known if the eth0 have the IP
/sbin/ifconfig eth0 | grep -q 'netmask'
# 0 is preset, 1 is not present
echo $?
Enable/Disable interface
This command is easy… just use iwconfig wlan0 txpower off
disable the Wifi and for the enabling iwconfig wlan0 txpower auto
but take attention about the last command: it’s come in error! I don’t have any idea about the reason it, the facto before this command the Wifi restart to work… it’s the troll of raspberry
The Final Scripts
Install the script
The final steps, just run every minutes… the cron help on it: add the command in root crontab with the command
sudo crontab -e -u root
and the line is just
* * * * /bin/bash -c ". /root/.bashrc; /usr/local/sbin/disable-wifi.sh > /var/log/disable-wifi.log 2>&1"
In the crontab entry, there should be 5 asterisks, not 4. also, after saving the script, chmod +x should be run to make the script executable. Mine was not executable until I set it to be executable.
Thanks, I was looking for a pi config to do just this.