Detect if a network device is connected in a bash script
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Detect if a network device is connected in a bash script
I'm trying to get Gentoo going on my old laptop, and its going fairly well. I can boot to console in 20 seconds. Anyways what I want to do is make a bash script which handles the bringing up and bringing down of network interfaces based on a series of conditions.
Now in my laptop, eth0 will always exist, but wlan0 which may not exist (PCMCIA wifi card). Now this is what I want the bash script to do as pseudocode
->Bring up eth0
-->Is eth0 connected?
--->Yes: so run /etc/init.d/net.eth0 start
--->No: Bring eth0 down
---->Does wlan0 exist?
----->Yes: Run /etc/init.d/net.wlan0 start
I know how to do all that except the second line "Is eth0 connected?" Does anyone know how to do that without a lot of fuss?
Yeah, but I'm not sure on the exact syntax, I don't do much bash scripting. As another suggestion, you should probably bring the interface down, and ping the gateway to double check what the exact message is you need to grep for. The timeout switch (-W) might not be a bad idea either.
#!/sbin/runscript
# Copyright 2008 Jordan Bradley
# Original file name: net-select
# Distributed under the terms of the GNU General Public License v2
depend() {
before fuse
}
start() {
ebegin "Starting Networking"
/etc/init.d/net.eth0 start
if [ ping -c 1 192.168.1.1 | grep "Destination Host Unreachable" ]; then
/etc/init.d/net.eth0 stop
if [ -e /sys/class/net/wlan0 ]; then
/etc/init.d/net.wlan0 start
fi
fi
eend
}
But I get this error:
/etc/init.d/net-select: line 14: [: missing `]'
grep: ]: No such file or directory
I didn't add the timeout param because even if the ping was unsuccessful, ping would timeout before knowing it.
It looks like grep is interpreting that last bracket as part of the command. Try putting something in backhits (the apostrophe like thing on the tilde(~) key). I've never been very clear on shell syntax.
Sweet, thanks. I'll have to stick in my wifi card and see what happens.
[Edit]
I put in my Wifi card and I get this error:
Quote:
/etc/init.d/net-select: line 14: [: too many arguments
This is what I have at this moment:
Code:
#!/sbin/runscript
# Copyright 2008 Jordan Bradley
# Original file name: net-select
# Distributed under the terms of the GNU General Public License v2
depend() {
before fuse
}
start() {
ebegin "Starting Networking"
/etc/init.d/net.eth0 start
if [ `ping -c 1 192.168.1.1 | grep "Destination Host Unreachable"` ]; then
/etc/init.d/net.eth0 stop
if [ -e /sys/class/net/wlan0 ]; then
/etc/init.d/net.wlan0 start
fi
fi
eend
}
Sweet, thanks. I'll have to stick in my wifi card and see what happens.
I put in my Wifi card and I get this error:
This is what I have at this moment:
I made a small change and this seems to function at least on my Debian system, unfortunately I don't have a Gentoo system to test it against presently.
Code:
#!/sbin/runscript
# Copyright 2008 Jordan Bradley
# Original file name: net-select
# Distributed under the terms of the GNU General Public License v2
depend() {
before fuse
}
start() {
ebegin "Starting Networking"
/etc/init.d/net.eth0 start
VARI=`ping -c 1 192.168.1.1 | grep -i "unreachable" | wc -l`
if [ $VARI -gt 0 ]; then
/etc/init.d/net.eth0 stop
if [ -e /sys/class/net/wlan0 ]; then
/etc/init.d/net.wlan0 start
fi
fi
eend
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.