Skip to content

Instantly share code, notes, and snippets.

@mrschyte
Last active August 27, 2018 08:28
Show Gist options
  • Save mrschyte/aa83ddad3c1d4f46308c6bb0a555af44 to your computer and use it in GitHub Desktop.
Save mrschyte/aa83ddad3c1d4f46308c6bb0a555af44 to your computer and use it in GitHub Desktop.
Replacement wait-online script
#!/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