Skip to content

Instantly share code, notes, and snippets.

@istepanov
Created December 12, 2016 05:22
Show Gist options
  • Select an option

  • Save istepanov/a1d706fb48138795c0ab0d7e50f39bbc to your computer and use it in GitHub Desktop.

Select an option

Save istepanov/a1d706fb48138795c0ab0d7e50f39bbc to your computer and use it in GitHub Desktop.

Revisions

  1. istepanov created this gist Dec 12, 2016.
    22 changes: 22 additions & 0 deletions watchdoge.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/usr/bin/env python

    import os
    import datetime

    address = '8.8.8.8'
    ping_timeout = 5
    attempts = 4
    log_file = '/var/log/ping_test.log'

    failed_attempts = 0

    for i in range(0, attempts):
    result = os.system('/bin/ping -c 1 -w %i %s > /dev/null 2>&1' % (ping_timeout, address))
    if result != 0:
    failed_attempts += 1

    if failed_attempts == attempts:
    with open(log_file, 'a') as logfile:
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
    logfile.write("%s 'ping %s' failed attempts: %i. Rebooting...\n" % (now, address, failed_attempts))
    os.system('/sbin/reboot')