简体   繁体   中英

Raspberry Pi (Raspbian Linux flavor) run script on wifi up

Currently I am experience issues with the script automatic run after wifi adapter connects to a network.

After ridiculously extended research, I've made several attempts to add script to a /etc/network/if-up.d/. Manually my script works; however it does not automatically.

User permissions:

 ls -al /etc/network/if-up.d/*
 -rwxr-xr-x 1 root root  703 Jul 25  2011 /etc/network/if-up.d/000resolvconf
 -rwxr-xr-x 1 root root  484 Apr 13  2015 /etc/network/if-up.d/avahi-daemon
 -rwxr-xr-x 1 root root 4958 Apr  6  2015 /etc/network/if-up.d/mountnfs
 -rwxr-xr-x 1 root root  945 Apr 14  2016 /etc/network/if-up.d/openssh-server
 -rwxr-xr-x 1 root root   48 Apr 26 03:21 /etc/network/if-up.d/sendemail
 -rwxr-xr-x 1 root root 1483 Jan  6  2013 /etc/network/if-up.d/upstart
 lrwxrwxrwx 1 root root   32 Sep 17  2016 /etc/network/if-up.d/wpasupplicant -> ../../wpa_supplicant/ifupdown.sh

Also, I've tried to push the command directly in /etc/network/interfaces by adding a row

post-up /home/pi/r/sendemail.sh

Contents of sendemail.sh :

#!/bin/sh
python /home/pi/r/pip.py

After the reboot, nothing actually happen. I've even tried sudo in front

I assume that wpasupplicant is the thing which causes that, but I cannot get how to run my script in ifupdown.sh script under /etc/wpa_supplicant.

Appreciate your help!

If you have no connectivity prior to initializing the wifi interface, I would suggest adding a cron job of a bash or python script that checks for connectivity every X minutes.

Ping (host);

If host is up then run python commands or external command.

This is rather ambiguous but hopefully is of some help.

Here is an example of a script that will check if a host is alive;

import re,commands


class CheckAlive:
def __init__(self):
        myCommand = commands.getstatusoutput('ping ' + 'google.com)
        searchString = r'ping: unknown host'

        match = re.search(searchString,str(myCommand))

        if match:
            # host is not alive
            print 'no alive, don't do stuff';
        else:
            # host is alive
            print 'alive, time do stuff';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM