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

Local IP address resolution fails on macOS #16

Open
sysophost opened this issue Jul 20, 2022 · 2 comments
Open

Local IP address resolution fails on macOS #16

sysophost opened this issue Jul 20, 2022 · 2 comments

Comments

@sysophost
Copy link

The agent uses socket.gethostbyname() to retrieve the IP address of the host by looking up the hostname, returned from socket.gethostname(), against either the local resolver or the hosts file.

On macOS the device hostname is not present in /etc/hosts by default, so looking up the hostname fails, causing the agent to exit.

@sysophost
Copy link
Author

A potential workaround would be:

def getIpAddress(self):
    ip_address = ""
    try:
        ip_address = socket.gethostbyname(socket.gethostname())
    except socket.gaierror:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((re.sub("https?://", "", self.agent_config["Server"]), int(self.agent_config["Port"])))
        ip_address = s.getsockname()[0]
    finally:
        return ip_address

Then call this from the checkIn() function with "ip": self.getIpAddress().

This also requires importing re.

I haven't tested this with other transport methods, so the protocol removal using re.sub() would likely need some work to accommodate this.

@sysophost
Copy link
Author

I've implemented this fix here but haven't raised a PR as I haven't applied the fix for Py2 or tested any other platforms/transports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant