Skip to content

Bash function for scripts that wait for IPv4 address on a given interface

License

Notifications You must be signed in to change notification settings

network-utilities/await-ipv4-address

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Await IPv4 Address

await-ipv4-address.sh, contains a function that returns listening ports for a given service, even if it has to wait a few seconds, and even if the listening ports are not known in advance; such as in the case with librespot service.

The following covers how to install this branch as a submodule within your own project, and parameters that await-ipv4-address.sh currently responds to.

Byte size of await-ipv4-address.sh Open Issues Open Pull Requests Build Test Status Latest commits


Table of Contents


Quick Start

Bash Variables

_module_https_url='https://github.com/network-utilities/await-ipv4-address.git'
_module_relative_path='lib/modules/await-ipv4-address'

Git Commands

cd "your-project-path"

git submodule add\
 -b master\
 --name await-ipv4-address\
 "${_module_https_url}" "${_module_relative_path}"

Version Locking; recommended for those that audit every dependency...

git checkout tags/<tag_name> -b <branch_name>

... replace <tag_name> with the tag to checkout and <branch_name> with a custom name, eg...

git checkout tags/v0.0.1 -b loc-v0.0.1

Edit Your ReadMe File

Suggested additions so everyone has a good time with submodules

Clone with the following to avoid incomplete downloads


    git clone --recurse-submodules <url-for-your-project>


Update/upgrade submodules via


    git submodule update --init --recursive --remote --merge


Please review the official documentation for Git submodules...


    git help submodule


... or via [Submodules chapter][git_book__submodules] from Git.



[git_book__submodules]:
  https://git-scm.com/book/en/v2/Git-Tools-Submodules
  "Valuable information for daily use and recovering from a detached HEAD"

Example Bash Script

example-usage.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"


## Lists iptables rules targeting IP, waits up-to seven seconds for an IP on eth0 interface
something_networky(){
    local _assigned_ips=($(await_ipv4_address 'eth0' '1' '6'))
    if [ -z "${_assigned_ips}" ]; then
        printf '%s got board of waiting for an IP\n' "${FUNCNAME[0]}"
        return 1
    fi

    for _assigned_ip in "${_assigned_ips[@]}"; do
        printf 'Listing any iptables rules for -> %s\n' "${_assigned_ip%/*}"
        iptables -S | grep -- "${_assigned_ip%/*}"
    done
}


something_networky

Testing example-usage.sh

bash example-usage.sh || echo "Exit code -> ${?}"

Utilize the above example to make similar edits to any of your project scripts that should wait for an IP.

Commit and Push

git add .gitmodules
git add lib/modules/await-ipv4-address
git add README.md


git commit -F- <<'EOF'
:heavy_plus_sign: Adds network-utilities/await-ipv4-address dependency



**Additions**


- `.gitmodules` file, tracks other Git repository code utilized by this project

- `lib/modules/await-ipv4-address` submodule, Git tracked dependency


**Edits**


- `README.md` file, documentation updates for submodules
EOF


git push origin gh-pages

🎉 Excellent 🎉 your project is now ready to begin unitizing code of this repository!


Await IPv4 Address API

Param Type Description
$1 string required example eth0 or wlan0, the interface name to wait for an IP address to be active on
$2 number 1 Number of seconds to sleep between checks
$3 number 10 Max number of loops before function fails silently

Returns: <address|list>, IP address or space separated list of addresses

Throws Parameter_Error: await_ipv4_address not provided an interface, when first parameter is not defined

Example: as an array

_ip_addresses_list=($(await_ipv4_address 'eth0'))

printf 'Listening address: %s\n' "${_ip_addresses_list[@]}"
#> Listening address: 192.168.0.2
#> Listening address: 192.168.0.4

Example: as a string

_ip_addresses_string="$(await_ipv4_address 'eth0')"

printf 'Listening address(es): %s\n' "${_ip_addresses_string}"
#> Listening address(es): 192.168.0.2 192.168.0.4

Notes

Pull Requests are welcomed! Check the Community section for development tips and code of conduct.


Attribution


License

Await IPv4 Address submodule quick start documentation
Copyright (C) 2019  S0AndS0

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation; version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

About

Bash function for scripts that wait for IPv4 address on a given interface

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

Languages