Created
March 14, 2021 23:55
-
-
Save simryang/e0e4bf739fd409d05b4bb9bd4b3b2fee to your computer and use it in GitHub Desktop.
Get DNS1, DNS2 addresses in openwrt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getSystemDns() -> tuple: | |
| """returns dns1, dns2 from /tmp/resolv.conf.auto | |
| openwrt: OpenWrt | |
| """ | |
| dns1 = "" | |
| dns2 = "" | |
| try: | |
| with open("/tmp/resolv.conf.auto") as f: | |
| dnscount = 0 | |
| for line in f.readlines(): | |
| # print(f"line = {line}, count={dnscount}, dns1={dns1}, dns2={dns2}") | |
| if "nameserver" in line: | |
| dnscount += 1 | |
| if dnscount == 1: | |
| dns1 = line.split(" ")[1].strip("\n") | |
| continue | |
| elif dnscount == 2: | |
| dns2 = line.split(" ")[1].strip("\n") | |
| else: | |
| # only up to 2 addresses are supported | |
| break | |
| line = f.readline() | |
| line = None | |
| except Exception as e: | |
| print(f"getting dns from /tmp/resolv.conf.auto is FAILED::{e}") | |
| finally: | |
| return (dns1, dns2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment