etherswitch/etherswitch.sh

46 lines
663 B
Bash
Raw Normal View History

2015-03-23 01:36:38 +01:00
#!/bin/env bash
2015-03-25 02:13:51 +01:00
switch_state=0
2015-03-23 01:36:38 +01:00
function check_fn {
type $1 &> /dev/null || {
echo "$1 is undefined"
exit 1
}
}
2015-03-23 01:36:38 +01:00
source ~/.etherswitch
2015-03-25 01:23:28 +01:00
if [ -z "$device" ]
2015-03-25 01:21:52 +01:00
then
2015-03-25 01:23:28 +01:00
echo "device is not set"
2015-03-25 01:21:52 +01:00
exit 1
fi
2015-03-25 01:23:28 +01:00
# check if $device is a network device
ip link show $device > /dev/null
2015-03-25 01:21:52 +01:00
if [ $? -eq 1 ]; then exit 1; fi
check_fn "on_switch_close"
check_fn "on_switch_open"
2015-03-23 01:36:38 +01:00
while :
do
2015-03-25 01:23:28 +01:00
ip link show $device | grep -q "LOWER_UP"
2015-03-23 01:36:38 +01:00
if [ $? -eq 1 ]
then
2015-03-25 02:13:51 +01:00
if [ $switch_state -eq 1 ]
2015-03-23 01:36:38 +01:00
then
2015-03-25 02:13:51 +01:00
switch_state=0
on_switch_open
2015-03-23 01:36:38 +01:00
fi
else
2015-03-25 02:13:51 +01:00
if [ $switch_state -eq 0 ]
2015-03-23 01:36:38 +01:00
then
2015-03-25 02:13:51 +01:00
switch_state=1
on_switch_close
2015-03-23 01:36:38 +01:00
fi
fi
sleep 0.3
done