Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 2451  / 3 Years ago, tue, august 10, 2021, 8:32:50

I'm writing a shell script that brings up an Ethernet interface (configured for DHCP) to determine whether it's connected to a network. If it isn't, I'd like to be able to report in a timely manner (within a few seconds) that there's no connection. Unfortunately, in the absence of a connection, ifup seems to wait several minutes before timing out, and the man page doesn't mention a timeout parameter. Is there any way to stop it after a few seconds?



This is on Ubuntu Server 11.10; NetworkManager is not in use.


More From » server

 Answers
3

I finally found a way.



As it turns out, since what I really want to do is check for link, I don't need ifup at all. Instead, I can use ethtool (though it has to be installed first), like so:



connected=$(ethtool eth0 | awk '/Link detected:/ { print $3 }')


If, however, I did need to check ifup, I could use a background process and the $! shell variable. If I just wanted to check whether it was still running after the timeout, I could do this:



ifup eth0 &
sleep 5
if ps -p $! | grep -q "$!"; then
connected='no'
kill $!
else
connected='yes'
fi


If I wanted to capture the output from ifup, my best bet would probably be to redirect it to a temporary file. (I looked into named pipes, but I don't think they'd work for this purpose; ifup would block until another process was reading its output.)


[#41250] Wednesday, August 11, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
diffeah

Total Points: 78
Total Questions: 130
Total Answers: 98

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;