-
Notifications
You must be signed in to change notification settings - Fork 4
/
auto-update-no-ip
executable file
·87 lines (63 loc) · 1.95 KB
/
auto-update-no-ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# coding: UTF-8
import sys
import netrc
import os.path
import no_ip_updater as noip
import no_ip_updater.getip as getip
'''
This script automatically gets No-IP login from netrc file and updates the
specified hosts with the current IP.
Add these lines to your ~/.netrc file, replacing <username> and <password>
accordingly:
machine no-ip.com
login <username>
password <password>
Add each host to a ".no-ip-hosts" file inside your home directory, or hardcode
the "hosts" variable inside this script.
'''
# Hosts to update #
# hosts = [ "example.no-ip.org" ]
hosts = []
HOSTS_FILE = "$HOME/.no-ip-hosts"
if __name__ == "__main__":
HOST = "no-ip.com"
try:
cred = netrc.netrc().authenticators( HOST )
except IOError as err:
print("Can't access .netrc file")
print(err)
sys.exit(1)
else:
if cred is not None:
username, account, password = cred
else:
print("No login for \"{}\" host in .netrc file".format(HOST))
sys.exit(1)
fileName = os.path.expandvars(HOSTS_FILE)
try:
with open(fileName, 'r') as f_in:
lines = (line.strip() for line in f_in) # All lines including the blank ones
lines = (line for line in lines if line) # Non-blank lines
except IOError as err:
if not hosts:
print("Can't access \"{}\" file".format(HOSTS_FILE))
print(err)
sys.exit(1)
else:
print("WARNING: Can't access \"{}\" file")
else:
if lines:
hosts = lines
elif not hosts:
print("No host found in \"{}\"".format(HOSTS_FILE))
sys.exit(1)
print("\n=== No-IP Updater ===")
print("Detecting current IP...")
try:
ip = getip.get_ip()
except:
print("[ERROR] Can't detect current IP.\n")
else:
noip.updateHosts(username, password, ip, hosts)
print("=====================\n")