LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 12-24-2008, 05:19 PM   #1
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Rep: Reputation: 0
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?
 
Old 12-24-2008, 05:44 PM   #2
tmerriam
Member
 
Registered: Dec 2008
Distribution: slackware
Posts: 50

Rep: Reputation: 16
Ping its gateway? You could grep the result for "Network Unreachable"

Last edited by tmerriam; 12-24-2008 at 05:45 PM.
 
Old 12-24-2008, 09:47 PM   #3
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tmerriam View Post
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"?

Last edited by jordanwb; 12-24-2008 at 09:50 PM.
 
Old 12-25-2008, 06:30 PM   #4
tmerriam
Member
 
Registered: Dec 2008
Distribution: slackware
Posts: 50

Rep: Reputation: 16
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.
 
Old 12-25-2008, 07:56 PM   #5
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
I've never done any bash scripting, but I can learn by looking at other scripts. Thanks for the suggestion about timeout.
 
Old 12-27-2008, 02:35 PM   #6
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
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.
 
Old 12-27-2008, 08:23 PM   #7
tmerriam
Member
 
Registered: Dec 2008
Distribution: slackware
Posts: 50

Rep: Reputation: 16
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.
 
Old 12-27-2008, 08:34 PM   #8
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
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.
 
Old 12-29-2008, 01:52 AM   #9
camh
Member
 
Registered: Feb 2005
Distribution: Slack/Debian
Posts: 163
Blog Entries: 2

Rep: Reputation: 33
More like:

Code:
if [ `ping -c 1 192.168.1.1 | grep "Destination Host Unreachable"` ]; then
 
Old 12-29-2008, 08:30 AM   #10
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
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
}

Last edited by jordanwb; 12-29-2008 at 08:34 AM.
 
Old 12-29-2008, 10:39 AM   #11
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by jordanwb View Post
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
}

Last edited by rweaver; 12-29-2008 at 11:01 AM.
 
Old 12-29-2008, 11:55 AM   #12
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
I'll give it a shot but my install is currently broken.
 
  


Reply

Tags
bash, devices, gentoo, network


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect bash script source vs. direct execution Jessard Programming 11 11-30-2010 06:43 AM
How to detect wheather usb drive is connected thru script linux1 Linux - Newbie 2 06-05-2008 09:34 AM
Bash script to detect hardware jimigoon Programming 4 06-30-2007 09:22 AM
Bash Script to Detect USB drive mount status nutthick Programming 6 02-02-2005 08:17 AM
bash script to detect scsi address at boot ewto Programming 2 10-20-2003 02:47 AM


All times are GMT -5. The time now is 01:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration