LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Detect if a network device is connected in a bash script (https://www.linuxquestions.org/questions/linux-general-1/detect-if-a-network-device-is-connected-in-a-bash-script-692824/)

jordanwb 12-24-2008 05:19 PM

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?

tmerriam 12-24-2008 05:44 PM

Ping its gateway? You could grep the result for "Network Unreachable"

jordanwb 12-24-2008 09:47 PM

Quote:

Originally Posted by tmerriam (Post 3386485)
Ping its gateway? You could grep the result for "Network Unreachable"

That's not a bad idea. To grep would I do:

"ping -c 1 192.168.1.1 | grep Network Unreachable"?

tmerriam 12-25-2008 06:30 PM

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.

jordanwb 12-25-2008 07:56 PM

I've never done any bash scripting, but I can learn by looking at other scripts. Thanks for the suggestion about timeout.

jordanwb 12-27-2008 02:35 PM

Ok this is what I have:

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
}

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.

tmerriam 12-27-2008 08:23 PM

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. :(

jordanwb 12-27-2008 08:34 PM

Like this:

Code:

if [ ping -c 1 192.168.1.1 | grep "Destination Host Unreachable" `]; then
Because I have no clue whatsoever, this is my first bash script that I've made.

camh 12-29-2008 01:52 AM

More like:

Code:

if [ `ping -c 1 192.168.1.1 | grep "Destination Host Unreachable"` ]; then

jordanwb 12-29-2008 08:30 AM

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
}


rweaver 12-29-2008 10:39 AM

Quote:

Originally Posted by jordanwb (Post 3390351)
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
}


jordanwb 12-29-2008 11:55 AM

I'll give it a shot but my install is currently broken.


All times are GMT -5. The time now is 09:12 PM.