Replacement wait-online script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from subprocess import check_output | |
from time import sleep | |
import click, sys | |
def is_interface_up(iface): | |
try: | |
return b'state DOWN' not in check_output(["/usr/bin/ip", "link", "show", "dev", iface]) and \ | |
b'inet' in check_output(["/usr/bin/ip", "addr", "show", "dev", iface]) | |
except Exception as ex: | |
return False | |
def wait_online(ifaces, waitsec, maxwait): | |
elapsed = 0 | |
for iface in ifaces: | |
while not is_interface_up(iface): | |
print('[%] Interface {} is down, sleeping.'.format(iface)) | |
sleep(waitsec) | |
elapsed += waitsec | |
if elapsed > maxwait: | |
print('[!] Unable to bring interfaces up'.format(iface)) | |
sys.exit(0) | |
print('[+] Interface {} is online!'.format(iface)) | |
sys.exit(0) | |
@click.command() | |
@click.option("--waitsec", default=5) | |
@click.option("--maxwait", default=60) | |
@click.argument("interfaces", nargs=-1) | |
def cli(interfaces, waitsec, maxwait): | |
if interfaces: | |
print('[+] Waiting for interfaces {} to come up'.format(interfaces)) | |
wait_online(interfaces, waitsec, maxwait) | |
else: | |
print('[+] No interfaces are specified, exiting'.format(interfaces)) | |
sys.exit(0) | |
if __name__ == "__main__": | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment