{"record":{"id":"9b060e02fd100ac0","repo":"XX-net/XX-Net","slug":"get-ip-address-e-r","errorCode":null,"errorMessage":"get ip address e:%r","messagePattern":"get ip address e:%r","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"code/default/smart_router/local/dns_query.py","lineNumber":62,"sourceCode":"        return socket.inet_ntoa(fcntl.ioctl(\n            s.fileno(),\n            0x8915,  # SIOCGIFADDR\n            struct.pack('256s', NICname[:15].encode(\"UTF-8\"))\n        )[20:24])\n\n    if sys.platform.startswith(\"linux\"):\n\n        if os.path.isfile(\"/system/bin/dalvikvm\") or os.path.isfile(\"/system/bin/dalvikvm64\") or \\\n                \"android.googlesource.com\" in sys.version:\n            ips = [b'127.0.0.1']\n        else:\n            try:\n                proc = subprocess.Popen([\"ip addr show | egrep inet | awk '{{print $2}}' | awk -F'/' '{{print $1}}'\"],\n                                        stdout=subprocess.PIPE, shell=True)\n                x = proc.communicate()[0]\n                ips = x.strip().split(b\"\\n\")\n            except Exception as e:\n                xlog.warn(\"get ip address e:%r\", e)\n                ips = [b'127.0.0.1']\n    elif sys.platform == \"darwin\":\n        try:\n            proc = subprocess.Popen([\"ifconfig | egrep inet | awk '{{print $2}}' | awk -F'/' '{{print $1}}'\"],\n                                    stdout=subprocess.PIPE, shell=True)\n            x = proc.communicate()[0]\n            ips = x.strip().split(b\"\\n\")\n        except Exception as e:\n            xlog.warn(\"get ip address e:%r\", e)\n            ips = [b'127.0.0.1']\n    elif sys.platform == \"ios\":\n        ips = [b'127.0.0.1']\n    elif sys.platform == \"win32\":\n        try:\n            ips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]]\n            ips = utils.to_bytes(ips)\n            if b\"127.0.0.1\" not in ips:\n                ips.append(b\"127.0.0.1\")","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/XX-net/XX-Net/blob/cfa5bc17b67676e467f37ec50766127e0ab5f0aa/code/default/smart_router/local/dns_query.py#L44-L80","documentation":"On Linux, get_local_ips() shells out to 'ip addr show | egrep inet | awk ...' to enumerate local interface addresses; any failure (missing ip/awk binaries, shell error, encoding issue) is caught, logged with this warning, and the function degrades to returning [b'127.0.0.1']. Functional but reduces smart_router accuracy.","triggerScenarios":"Calling start() on the smart router (which calls get_local_ips) on a minimal Linux system without iproute2 or awk, or where /sbin is not on PATH for the subprocess, or where subprocess/shell invocation raises OSError.","commonSituations":"Docker/alpine/minimal containers lacking iproute2; hardened environments restricting shell=True subprocesses; PATH differences when launched from a service manager; busybox systems with different output formatting.","solutions":["Install iproute2 and gawk (or verify 'ip addr show' runs in the same environment the launcher uses).","Prefer a pure-Python fallback using socket.if_nameindex()/get_ip_address (the branch already used for other platforms) instead of shelling out.","Check PATH when running under systemd/supervisor; use absolute paths like /sbin/ip.","Ignore the warning if loopback-only detection is acceptable for your deployment."],"exampleFix":"// before\nproc = subprocess.Popen([\"ip addr show | egrep inet | awk '{{print $2}}' | awk -F'/' '{{print $1}}'\"], stdout=subprocess.PIPE, shell=True)\n\n// after\nips = []\nfor ix in socket.if_nameindex():\n    ip = get_ip_address(ix[1])\n    if ip:\n        ips.append(ip)","handlingStrategy":"fallback","validationCode":"import shutil\nif not shutil.which('ip'):\n    # pre-check before triggering smart router start\n    print('iproute2 missing; local IP detection will fall back to 127.0.0.1')","typeGuard":null,"tryCatchPattern":"try:\n    ips = get_local_ips()\nexcept Exception:\n    ips = [b'127.0.0.1']  # function already self-falls-back; guard callers against empty lists","preventionTips":["Install iproute2/awk in minimal containers that run the smart router.","Prefer pure-Python interface enumeration over shelling out on Linux.","Test under the same environment (systemd/docker) that production uses, since PATH differs."],"tags":["linux","network-interfaces","subprocess","smart-router","ip-address"],"backgroundTag":"local-ip-enumeration-failed","analyzedSha":"cfa5bc17b67676e467f37ec50766127e0ab5f0aa","analyzedAt":"2026-08-27T19:28:28.225Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}