#!/data/data/com.termux/files/usr/bin/sh
for i in {1..10}; do
if pgrep -x dbus-daemon >/dev/null; then
break
fi
sleep 1
done
AVAHI=$PREFIX/bin/avahi-daemon
exec $AVAHI
解析mDNS域名、更新hosts
通过调用avahi-resolve-host-name就可以了
import subprocess
def getDomain(dom):
out = subprocess.check_output(['avahi-resolve-host-name', '-4', '-n', dom], encoding='utf-8')
if out.startswith("Failed"):
raise Exception(out)
try:
domain, ip = out.strip().split('\t')
print("domain resolved:", dom, "ip:", repr(ip))
assert set(ip) - set('1234567890.') == set()
return ip
except Exception as e:
#print(e)
raise Exception(out)
def getDomainSafe(dom):
try:
return getDomain(dom)
except Exception as e:
print("domain resolve fail:", str(e))
return None
DOMAINS = [
"DESKTOP-697LVJS.local",
"LAPTOP-1Q45LAA4.local",
]
def doUpdate():
domainIPs = [getDomainSafe(c) for c in DOMAINS]
with open("/system/etc/hosts", "r") as f:
hostsContent = f.read()
hostLines = hostsContent.split('\n')
def updateHostLines(domain, ip):
newLine = "%s %s" % (ip, domain)
for i,l in enumerate(hostLines):
if domain in l:
hostLines[i] = newLine
break
else:
hostLines.append(newLine)
for i in range(len(DOMAINS)):
domain = DOMAINS[i]
ip = domainIPs[i]
if ip is not None:
updateHostLines(domain, ip)
if hostLines and hostLines[-1] != '':
hostLines.append('')
hostContent = '\n'.join(hostLines)
with open("/system/etc/hosts", "w") as f:
f.write(hostContent)
print("finish updating hosts!")
if __name__ == "__main__":
doUpdate()