Waiting for a network interface before executing a command

I have a couple of ideas on how I will achieve this. Not sure how I could script.

Method 1: (probably the best choice)

Create a loop that pings the server before receiving a response, then run the command if the response is not received within X time / runs, continue the script.

Method 2:

Check if the network interface has a valid IP address, then continue the script

How can I add this functionality to the script. Will awk or grep be used in this situation? Thank you in advance for ANY input.

+6
source share
3 answers

This command must wait until it can contact Google or it tries 50 times:

for i in {1..50}; do ping -c1 www.google.com &> /dev/null && break; done

for i in {1..50} 50 , break. ping -c1 www.google.com 1 ping- google, &> /dev/null , . && break , , , ping .

+5

, DHCP.

, ( eth0), , IP- (, , ) DHCP, , .

, , / , (.. 8.8.8.8 www.google.com ). IP- , , IP .

while ! ip route | grep -oP 'default via .+ dev eth0'; do
  echo "interface not up, will try again in 1 second";
  sleep 1;
done
+1

- Geofferey

, ...

await-ipv4-address.sh

#!/usr/bin/env bash


## Lists IP addresses for given interface name
## @returns {number|list}
## @param {string} _interface      - Name of interface to monitor for IP address(es)
## @param {number} _sleep_intervel - Number of seconds to sleep between checks
## @param {number} _loop_limit     - Max number of loops before function returns error code
## @author S0AndS0
## @copyright AGPL-3.0
## @exampe As an array
##     _addresses_list=($(await_ipv4_address 'eth0'))
##     printf 'Listening address: %s\n' "${_addresses_list[@]}"
##     #> Listening address: 192.168.0.2
##     #> Listening address: 192.168.0.4
## @example As a string
##     _addresses_string="$(await_ipv4_address 'eth0' '1' '3')"
##     printf 'Listening address(es): %s\n' "${_addresses_string}"
##     #> Listening address(es): 192.168.0.2 192.168.0.4
await_ipv4_address(){
    local _interface="${1:?# Parameter_Error: ${FUNCNAME[0]} not provided an interface}"
    local _sleep_interval="${2:-1}"
    local _loop_limit="${3:-10}"
    if [ "${_sleep_interval}" -lt '0' ] || [ "${_loop_limit}" -le '0' ]; then
        printf 'Parameter_Error: %s requires positive numbers for second and third parameters\n' "${FUNCNAME[0]}" >&2
        return 1
    fi

    local _loop_count='0'
    local -a _ipv4_addresses
    while true; do
        for _address in $({ ip addr show ${_interface} | awk '/inet /{print $2}'; } 2>/dev/null); do
            _ipv4_addresses+=("${_address}")
        done

        if [ "${#_ipv4_addresses[@]}" -gt '0' ]; then
            printf '%s\n' "${_ipv4_addresses[*]}"
            break
        elif [ "${_loop_count}" -gt "${_loop_limit}" ]; then
            break
        fi

        let _loop_count+=1
        sleep "${_sleep_interval}"
    done

    [[ "${#_ipv4_addresses[@]}" -gt '0' ]]; return "${?}"
}

GitHub bash-utilities/await-ipv4-address, ReadMe Git .

...

source "await-ipv4-address.sh"

... ...

#!/usr/bin/env bash


## Enable sourcing via absolute path
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
    __SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"


source "${__DIR__}/modules/await-ipv4-address/await-ipv4-address.sh"


awk grep ? - Geofferey

; , echo , grep ... awk , , printf .

awk IPv4 IPv6 ....

    # ... trimmed for brevity
        for _address in $({ ip addr show ${_interface} | awk '/inet/{print $2}'; } 2>/dev/null); do
            # ... things that get done with an address
        done

... , .


-,

  1. IP eth0
_ip_addresses_list=($(await_ipv4_address 'eth0'))
  1. IP tun0
_ip_addresses_list=($(await_ipv4_address 'tun0' '1' '29'))
  1. IP wlan0 3
_ip_addresses_list=($(await_ipv4_address 'wlan0' '3' '19'))

, await_ipv4_address , , ...

_ip_addresses_list=($(await_ipv4_address 'wlan0' '3' '19' || true))

... , , - .


IP-...

for _ip_address in "${_ip_addresses_list[@]}"; do
    printf 'IP -> %s\n' "${_ip_address}"
done

, , ? - user207421

, , , , - Geofferey

, IP- /AP ; , IP- ... , , .

, , dig curl, ping DNS , .


, - , , ? - Geofferey

... ...

await_ipv4_address(){
    local _interface="${1:?# Parameter_Error: ${FUNCNAME[0]} not provided an interface}"
    local _sleep_interval="${2:-1}"
    local _loop_limit="${3:-10}"
    # ...
}
  • local , help local help declare,

  • "${something:?Error message}" Error message something

  • "${another_thing:-1}" "${another_thing:-1}" 1 another_thing

, man --pager='less -p ^"PARAMETERS"' bash Special Parameters man --pager='less -p "Parameter Expansion"' bash , .


if [ "${_sleep_interval}" -lt '0' ] || [ "${_loop_limit}" -le '0' ]; then
    printf 'Parameter_Error: %s requires positive numbers for second and third parameters\n' "${FUNCNAME[0]}" >&2
    return 1
fi
  • , _sleep_interval _loop_count - -lt (-lt) (-le)

  • , if true, || , false , as && &&, true

man operator

  • printf 'something\n' >&2 something ; ,

  • ,


while true; do
    # ... stuff
done
  • , , .

    for _address in $({ ip addr show ${_interface} | awk '/inet /{print $2}'; } 2>/dev/null); do
        _ipv4_addresses+=("${_address}")
    done
  • $({ command | parser; } 2>/dev/null) - ,

    • $(something) something
    • { one_thing | another_thing; } { one_thing | another_thing; }

    , man --pager='less -p "Compound Commands"' bash

    • 2>/dev/null ,
  • _preexisting_list+=("element") element _preexisting_list +=


        if [ "${#_ipv4_addresses[@]}" -gt '0' ]; then
            printf '%s\n' "${_ipv4_addresses[*]}"
            break
        elif [ "${_loop_count}" -gt "${_loop_limit}" ]; then
            break
        fi
  • if part , _ipv4_addresses 0 #, ${#_list_name[@]}

  • elif part ,

break while , .


        let _loop_count+=1
        sleep "${_sleep_interval}"
  • let _counter+=1 1 _counter _counter

  • sleep ,


    [[ "${#_ipv4_addresses[@]}" -gt '0' ]]; return "${?}"
  • A semicolon with check brackets ( [[ is_it_true ]]) instead of ||or &&calls returnto return the status if the number of IP addresses found is greater, 0regardless of the reliability of the test

If after all this there is something dubious, do not hesitate to leave comments to improve the answer.

0
source

Source: https://habr.com/ru/post/1530794/


All Articles