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? |
Ping its gateway? You could grep the result for "Network Unreachable"
|
Quote:
"ping -c 1 192.168.1.1 | grep Network Unreachable"? |
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.
|
I've never done any bash scripting, but I can learn by looking at other scripts. Thanks for the suggestion about timeout.
|
Ok this is what I have:
Code:
#!/sbin/runscript /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. :(
|
Like this:
Code:
if [ ping -c 1 192.168.1.1 | grep "Destination Host Unreachable" `]; then |
More like:
Code:
if [ `ping -c 1 192.168.1.1 | grep "Destination Host Unreachable"` ]; then |
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:
Code:
#!/sbin/runscript |
Quote:
Code:
#!/sbin/runscript |
I'll give it a shot but my install is currently broken.
|
All times are GMT -5. The time now is 09:12 PM. |