Hi Demontager
I've put together a small script that monitors and prints out network cable status.
Status is determined as follows:
# The script monitors the file /sys/class/net/NIC/carrier
# where NIC is your Network Interface Card, eth0 for example.
# If the file contains 0, the cable is currently unplugged.
# If the file contains 1, the cable is currently plugged in.
# If the file does not exist, the NIC is down, possibly because:
# 1. The NIC was never brought up.
# 2. Someone ran the command ifconfig NIC down
# 3. Some other reason.
The script prints out the current status when it first starts, after that, it will print out status any time it changes:
tc@E310:~/net$ ./CableSense.sh
Cable is plugged in.
Cable was unplugged.
Cable was plugged in.
You will need to decide what commands (if any) you want to run when the script starts and when there is a state change.
Look at the 2 sections that begin with
case $Cable in and end in
esac.
Look at the
echo commands to pick where you want to execute a command(s) and add it/them like this:
1)
echo. "Cable was plugged in." >>$LogFile
Add your commands here
and here
and so on
;;
If you want to stop messages from being echoed to the screen, find the section labeled
# Where to send messages to:and
uncomment the appropriate line.
The script is fairly well commented, but feel free to ask questions.