-1

i have a test for a device where i am using: "if-config lan1 up" and right after check if it was able to connect(succeed with if-config up) with "ethtool lan1".

but before it can get up it already check if LAN 1 is connected, so it telling me its not connected, even so after some second it succeed to connect. can i make ethtool wait for "if-config up" to finish or fail before it check the connection? can i do it with no sleep command???? i tried wait but it didn't work

code below:

 function ethernet_up_and_test(){
ifconfig $1 up
interface=$1
expected_link_speed=$2
interfaceName=$3
ethtool_response=`ethtool ${interface}`
link_detected=`echo -e "${ethtool_response}" | grep "Link detected:" | cut -d" " -f3`
if [ "x${link_detected}" != "xyes" ]; then
    echo -e "*** ${interfaceName} - no link detected ***"
    return -1
fi
actual_link_speed=`echo -e "${ethtool_response}" | grep "Speed:" | cut -d" " -f2`
if [ "x${actual_link_speed}" != "x${expected_link_speed}" ]; then
    echo -e "link speed is: ${actual_link_speed}"
    return -1
fi
echo -e "PASSED"
return 0
}

for example it get ethernet_up_and_test lan1 1000Mb/s LAN1

ishash
  • 1
  • 1
  • Please put the script in here. Also tidy up your problem description. – sjsam Jan 18 '18 at 20:25
  • BTW, the `[ "x$foo" != "xbar" ]` formulation is completely unnecessary; it's a workaround for bugs that don't exist in *any* POSIX-compliant shell so long as you both (1) use correct quoting, and (2) don't use deprecated syntax (that is to say, avoid `-a` and `-o` to combine multiple tests in a single invocation). – Charles Duffy Jan 18 '18 at 20:44
  • Also, `echo -e` simply should not be used, *ever*; the POSIX standard prohibits it from doing anything other than printing `-e` on output, and while bash violates that standard out-of-the-box, (1) many common shells don't, and (2) even bash will honor the standard if given appropriate compile-time or runtime configuration. See [the POSIX specification for `echo`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), particularly the section labeled APPLICATION USAGE, suggesting that `printf` be used instead. – Charles Duffy Jan 18 '18 at 20:45
  • Also, `function funcname() {` is a mix of ksh syntax (wherein a function declaration is `function funcname {` with no `()`), and POSIX sh syntax (which defines a function with `funcname() {`, with no `function`); while being compatible with neither. Moreover, the *behavior* of the ksh form in bash doesn't match the behavior of the ksh form in ksh (where it makes variables defined within the function body local by default, for example). See http://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Jan 18 '18 at 20:46
  • Also, `return -1` is not well-defined behavior -- exit status is reflected as an unsigned 8-bit integer; when you return a negative value, it wraps around and becomes a large positive value. Better to use `return 1` and not have your values mangled. – Charles Duffy Jan 18 '18 at 20:48
  • Anyhow -- your code **already does** block until `ifconfig $1 up` finishes. The problem is that `ifconfig $1 up` is finished as soon as the OS has been told that the software stack wants the interface enabled -- long before the kernel and network stack are done making adjustments in accordance with that flag update, so you need to poll their status to figure out when they're done. – Charles Duffy Jan 18 '18 at 20:49
  • (BTW, it should be `ifconfig "$1" up` -- consider running your code through http://shellcheck.net/ and fixing what it finds). – Charles Duffy Jan 18 '18 at 20:50

2 Answers2

0

The interface is not necessarily up when ifconfig $1 up returns, so you'll need some step where you wait for the system to finish configuring things. Try polling the state periodically:

interface=$1
expected_link_speed=$2
interfaceName=$3
ifconfig "$interface" up

#Wait for the interface to come up. 
sleep_int=.25  #Sleep interval on each loop
x="0"          #Loop counter
iter="20"      #Number of loops
link_detected=$(ethtool "$interface" | grep "Link detected:" | cut -d" " -f3)
while [[ "$link_detected" != "yes" && $x -lt $iter ]]
do
    link_detected=$(ethtool "$interface" | grep "Link detected:" | cut -d" " -f3)
    sleep $sleep_int
    x=$((x + 1))
done

#Check if the link is there    
if [[ "$link_detected" != "yes" ]]
then
    echo -e "*** ${interfaceName} - no link detected ***"
    return 1
fi

#... Do whatever after the interface is up
shay
  • 755
  • 10
  • 22
-1

I'm not sure if i understand you correctly but you could try chaining your commands with "&&", such as:

ifconfig eth0 up && apt-get update

Or whatever you need to run in some particular order.

mpvqz
  • 64
  • 2