#!/usr/bin/python
from subprocess import check_output, Popen, PIPE

def shell_cmd(cmd):
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    out, err = p.communicate()
    if err:
        print err
        return -1
    else:
        #print out
        return out

def is_service_running(serv):
    cmd = ['systemctl', 'status', serv,]
    resp = shell_cmd(cmd)
    if resp == -1:
        return -1

    if 'running' in resp:
        return True
    else:
        return False

def service_restart(serv):
    cmd = ['systemctl', 'restart', serv,]
    resp = shell_cmd(cmd)
    if resp == -1:
        return -1
    else:
        return 0

def service_start(serv):
    '''If not running, will try to restart'''
    #serv = 'udhcpd'
    resp = is_service_running(serv)
    print '{} running status: {}'.format(serv, resp)
    if resp:
        return 0
    elif not resp:
        if service_restart(serv) == -1:
            return -1
        else:
            resp = is_service_running(serv)
            print '{} running status: {}'.format(serv, resp)
            if not resp:
                return -1
            elif resp:
                return 0

def ap_address(addr='192.168.42.1'):
    resp = shell_cmd(['hostname', '-I',])
    if resp == -1:
        return -1
    print 'addresses: {}'.format(resp)
    if addr in resp:
        return 0
    else:
        resp = shell_cmd(['ifconfig', 'wlan0', addr,])
    if resp == -1:
        return -1
    else:
        resp = shell_cmd(['hostname', '-I',])
        if resp == -1:
            return -1
        print 'addresses: {}'.format(resp)
        if addr in resp:
            return 0
        else:
            return -1


print 'Testing...'
print ap_address()
serv = 'hostapd'
print service_start(serv)
serv = 'udhcpd'
print service_start(serv)

@@ -11,16 +11,26 @@
 /python%0A
+import os%0A
 from sub
@@ -69,16 +69,226 @@
 en, PIPE
+%0Amagic_file = 'ap' %0Ahomedir = '/home/pi/'%0A%0Aprint 'Starting check of ap status...'%0Af = os.path.join(homedir, magic_file)%0Aif not os.path.exists(f):%0A    print 'Exit. Magic file, %7B%7D, missing.'.format(f)%0A    exit(0)
 %0A%0Adef sh
@@ -1477,32 +1477,38 @@
 n -1%0A    print '
+Found 
 addresses: %7B%7D'.f
@@ -1891,27 +1891,8 @@
 1%0A%0A%0A
-print 'Testing...'%0A
 prin

Check ap status runs when file /home/pi/ap exists