Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auto conf creation dynamically and updated location #10

Merged
merged 2 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ Configuration File

The configuration file is just a basic INI file, containing one section per host;

The configuration file is located at `~/.config/bentasker.Wake-On-Lan-Python/wol_config.ini`

The following is an example of hosts save in `wol_config.ini`

[General]
broadcast=192.168.1.255

[MyPc]
mac=00:13:0d:e4:60:61



License
--------

PSF v2, see [LICENSE](LICENSE)
PSF v2, see [LICENSE](LICENSE)
66 changes: 40 additions & 26 deletions wol.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def wake_on_lan(host):
macaddress = macaddress.replace(macaddress[2], '')
else:
raise ValueError('Incorrect MAC address format')

# Pad the synchronization stream.
data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
send_data = b''
Expand All @@ -56,36 +56,54 @@ def wake_on_lan(host):
return True


def loadConfig():
""" Read in the Configuration file to get CDN specific settings

"""
global mydir
global myconfig
Config = configparser.ConfigParser()
Config.read(mydir+"/.wol_config.ini")
sections = Config.sections()
dict1 = {}
for section in sections:
options = Config.options(section)

sectkey = section
myconfig[sectkey] = {}


for option in options:
myconfig[sectkey][option] = Config.get(section,option)
def writeConfig(conf):
""" Write configuration file to save local settings """
global conf_path
conf.write(open(conf_path+'/wol_config.ini', 'w'))


return myconfig # Useful for testing
def loadConfig():
""" Read in the Configuration file to get CDN specific settings """
global conf_path
global myconfig
Config = configparser.ConfigParser()
# Create conf path if does not exists
if not os.path.exists(conf_path):
os.makedirs(conf_path, exist_ok=True)
# generate default config file if does not exists
if not os.path.exists(conf_path+'/wol_config.ini'):
# get broadcast ip dynamicly
local_ip = socket.gethostbyname(socket.gethostname())
local_ip = local_ip.rsplit('.', 1)
local_ip[1] = '255'
broadcast_ip = '.'.join(local_ip)
# Load default values to new conf file
Config['General'] = {'broadcast': broadcast_ip}
# two examples for devices
Config['myPC'] = {'mac': '00:2a:a0:cf:83:15'}
Config['myLaptop'] = {'mac': '00:13:0d:e4:60:61'}
writeConfig(Config) # Generate default conf file
Config.read(conf_path+"/wol_config.ini")
sections = Config.sections()
dict1 = {}
for section in sections:
options = Config.options(section)

sectkey = section
myconfig[sectkey] = {}

for option in options:
myconfig[sectkey][option] = Config.get(section, option)

return myconfig # Useful for testing

def usage():
print('Usage: wol.py [hostname]')



if __name__ == '__main__':
mydir = os.path.dirname(os.path.abspath(__file__))
conf_path = os.path.expanduser('~/.config/bentasker.Wake-On-Lan-Python')
conf = loadConfig()
try:
# Use macaddresses with any seperators.
Expand All @@ -102,7 +120,3 @@ def usage():
print('Magic packet should be winging its way')
except:
usage()