Subject: Re: Obtaining a host name?
To: Jan Danielsson <jan.danielsson@gmail.com>
From: Steven M. Bellovin <smb@cs.columbia.edu>
List: netbsd-users
Date: 08/14/2005 11:32:23
In message <42FF5D14.3020004@gmail.com>, Jan Danielsson writes:
>This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
>--------------enigABEF1F0A864B7FC6FD237B88
>Content-Type: text/plain; charset=ISO-8859-1
>Content-Transfer-Encoding: 7bit
>
>Hell all,
>
>   My universities student network (broadband at home) requires us to
>login before we get access to Internet. To automate the login procedure,
>I have written a python script which logs in during boot. I wanted the
>script to validate its dhcp-assigned ip against that which is printed in
>the login page. (Don't ask.. I guess you can say it was for the hell of
>it). I'm kind of glad I did, because I ran into something which made me
>curious.
>
>This works on my Windows laptop, and on a friends Linux box:
>
>>>> import socket
>>>> print socket.gethostbyaddr(socket.gethostname())
>
>It will print my ip address - the one I was given assigned by the dhcp.
>
>However, when I run it on my NetBSD-system, it says:
>
>socket.gaierror: (7, 'No address associated with hostname')
>
>   Now, when I connect to some servers with my Windows system, the
>server will reply "Hello REGULUS3.student.uu.se", or something like
>that. I haven't set that "REGULUS"-variable anywhere, so I assume that
>it is sent to me by the dhcp server(?). Now, my NetBSD system doesn't
>seem to do that, and what's more: I have set my own hostname in rc.conf.
>
>   I assume that is why it fails on NetBSD, gethostname() isn't
>returning a "proper" name?
>
>   Is there any way to fix so that gethostname() will return that dhcp
>assigned name in NetBSD?
>
>Thankful for any pointers.


Very interesting.  I'm getting inconsistent results on the different 
NetBSD machines I've tried it on, and I'm not sure why.

However -- didn't you mean to say

	print socket.gethostbyname(socket.gethostname())

That does indeed print the IP address, even on machines where your 
example is misbehaving.  (Note that you're trying to look up a hostname 
by its *address*, not its name.  There may not be a proper reverse DNS 
entry.)


		--Steven M. Bellovin, http://www.cs.columbia.edu/~smb