diff options
author | Benjamin Robin | 2015-06-11 20:46:47 +0200 |
---|---|---|
committer | Benjamin Robin | 2015-06-11 20:47:19 +0200 |
commit | 488da71e7c0bfcfbc193c900156c3594479021b1 (patch) | |
tree | 815f38ccf38c85267893433a934cac1388b38e11 | |
download | aur-network-wait-online.tar.gz |
Copied from AUR 3
-rw-r--r-- | .SRCINFO | 18 | ||||
-rw-r--r-- | PKGBUILD | 23 | ||||
-rw-r--r-- | network-wait-online.install | 22 | ||||
-rw-r--r-- | network-wait-online.sh | 189 | ||||
-rw-r--r-- | network-wait-online@.service | 14 |
5 files changed, 266 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 000000000000..1626f3b80ccc --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,18 @@ +pkgbase = network-wait-online + pkgdesc = Network wait connection ready + pkgver = 1.0 + pkgrel = 3 + url = http://forums.archlinux.fr/topic12900.html + install = network-wait-online.install + arch = any + license = GPL + depends = iputils + depends = iproute2 + depends = systemd + source = network-wait-online.sh + source = network-wait-online@.service + md5sums = 474bb67b387f65129a5dd28f15949ab9 + md5sums = 6dcbded3675e36174921c2d6d162e837 + +pkgname = network-wait-online + diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 000000000000..1ea5f56b0ef6 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,23 @@ +# Maintainer: Benjamin Robin <benjarobin> +# Contributor: Tetsumaki <http://goo.gl/YMBdA> + +pkgname=network-wait-online +pkgver=1.0 +pkgrel=3 +pkgdesc="Network wait connection ready" +arch=('any') +url="http://forums.archlinux.fr/topic12900.html" +license=('GPL') +depends=('iputils' 'iproute2' 'systemd') +install='network-wait-online.install' +source=('network-wait-online.sh' + 'network-wait-online@.service') + +md5sums=('474bb67b387f65129a5dd28f15949ab9' + '6dcbded3675e36174921c2d6d162e837') + +package() +{ + install -Dm755 "${srcdir}/network-wait-online.sh" "${pkgdir}/usr/bin/network-wait-online" + install -Dm644 "${srcdir}/network-wait-online@.service" "${pkgdir}/usr/lib/systemd/system/network-wait-online@.service" +} diff --git a/network-wait-online.install b/network-wait-online.install new file mode 100644 index 000000000000..dc46eb937f25 --- /dev/null +++ b/network-wait-online.install @@ -0,0 +1,22 @@ + +## arg 1: the new package version +post_install() { + post_upgrade $1 0 +} + + +## arg 1: the new package version +## arg 2: the old package version +post_upgrade() { + echo "" + echo "To enable the service provided by this package, you need to run:" + echo "=> systemctl enable network-wait-online@address-to-wait.service" + echo "where 'address-to-wait' can be an IP or a domain..." + echo "" + echo "If the defaults options do not fit your need (timeout, method to wait...), " + echo "or if the service used to connect to the network is not dhcpcd@.service: " + echo "=> copy the service to '/etc/systemd/system/', " + echo "add desired options to the ExecStart= line, and changed the After= line." + echo "List of options can be provided by running: 'network-wait-online -h'" + echo "" +} diff --git a/network-wait-online.sh b/network-wait-online.sh new file mode 100644 index 000000000000..563ee1a1fbdb --- /dev/null +++ b/network-wait-online.sh @@ -0,0 +1,189 @@ +#!/bin/bash +# +# network-wait-online +# +# Copyright (c) 2013 Benjamin Robin <benjarobin> +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +VERSION="1.0" + +################################### +### OPTIONS ### +################################### + +VERBOSE=0 +QUIET=0 +TIMEOUT_WAIT=30 +ADDR_WAIT="" +METHOD_WAIT="ping" + +################################### +### General functions ### +################################### + +# -ab --long -> -a -b --long +# set $OPTS +explode_args() { + unset OPTS + local arg=$1 + while [[ $arg ]]; do + [[ $arg = "--" ]] && OPTS+=("$@") && break; + if [[ ${arg:0:1} = "-" && ${arg:1:1} != "-" ]]; then + OPTS+=("-${arg:1:1}") + (( ${#arg} > 2 )) && arg="-${arg:2}" || { shift; arg=$1; } + else + OPTS+=("$arg"); shift; arg=$1 + fi + done +} + +usage() { +cat << EOF +usage: $0 [options] address + +This is a utility to find out whether we are online. +It is done by pinging or checking that the address passed to this script can be accessed. +Waits until the specified address can be accessed, or that the specified timeout expires. +On exit, the returned status code should be checked. + +OPTIONS: + -h, --help Show this message + -q, --quiet Do not show any message + -v, --verbose Display every error message + -m, --method Select the method to wait on the specified address: "ping", "ping6" or "route" + -t, --timeout Set the maximum time to wait in second + -V, --version Display the version + +ADDRESS: An IP address in case of routing, for ping can be a domain... +EOF +} + +version() { + echo "network-wait-online $VERSION" +} + +################################### +### Argument parsing ### +################################### + +# Explode arguments +explode_args "$@" +set -- "${OPTS[@]}" +unset OPTS + +while [[ $1 ]]; do + case "$1" in + -h|--help) usage; exit 0;; + -q|--quiet) QUIET=1; VERBOSE=0;; + -v|--verbose) QUIET=0; VERBOSE=1;; + -m|--method) shift; METHOD_WAIT="$1";; + -t|--timeout) shift; TIMEOUT_WAIT="$1";; + -V|--version) version; exit 0;; + *) if [ -z "$ADDR_WAIT" ] ; then ADDR_WAIT="$1"; else usage; exit 10; fi ;; + esac + shift +done + +# Check that the address is specified +if [ -z "$ADDR_WAIT" ] ; then + usage + exit 10 +fi + +################################### +### Wait functions ### +################################### + +wait_addr_ping() { + + local retCode=12 + local timeEnd=$(( $(date +%s) + TIMEOUT_WAIT )) + local timeLeft=$TIMEOUT_WAIT + + while [ $timeLeft -gt 0 ] ; do + + $METHOD_WAIT -c1 -W1 $ADDR_WAIT + retCode=$? + if [ $retCode -eq 0 ] ; then + break + else + sleep 0.3 + timeLeft=$(( timeEnd - $(date +%s) )) + fi + + done + return $retCode +} + +wait_addr_route() { + + local retCode=13 + local nbStepLeft=$(( TIMEOUT_WAIT * 3 )) + + while [ $nbStepLeft -gt 0 ] ; do + + ip route get $ADDR_WAIT + retCode=$? + if [ $retCode -eq 0 ] ; then + break + else + sleep 0.33 + nbStepLeft=$(( nbStepLeft - 1 )) + fi + + done + return $retCode +} + +################################### +### Program ### +################################### + +exitCode=11 + +if [ "$METHOD_WAIT" == "ping" ] || [ "$METHOD_WAIT" == "ping6" ] ; then + + if [ $VERBOSE -eq 1 ] ; then + wait_addr_ping + else + wait_addr_ping &>/dev/null + fi + exitCode=$? + +elif [ "$METHOD_WAIT" == "route" ] ; then + + if [ $VERBOSE -eq 1 ] ; then + wait_addr_route + else + wait_addr_route &>/dev/null + fi + exitCode=$? + +else + usage + exit 10 +fi + +# Display result if not quiet +if [ $QUIET -eq 0 ]; then + if [ $exitCode -ne 0 ] ; then + echo "Failed to $METHOD_WAIT $ADDR_WAIT" 1>&2 + else + echo "Successfully $METHOD_WAIT $ADDR_WAIT" + fi +fi + +exit $exitCode diff --git a/network-wait-online@.service b/network-wait-online@.service new file mode 100644 index 000000000000..cded481b9fcf --- /dev/null +++ b/network-wait-online@.service @@ -0,0 +1,14 @@ +[Unit] +Description=Network Wait Online +After=dhcpcd@.service +Conflicts=shutdown.target +Wants=network.target +Before=network.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/network-wait-online -m ping -t 60 %I +RemainAfterExit=yes + +[Install] +WantedBy=network.target |