5

So I'm trying to get the LAN IP Address of the machine the program is running on and compare it to IP Addresses passed to it via UDP.

However when I use:

print str(socket.gethostbyname(socket.gethostname()))

It returns 127.0.0.1 which should be 192.168.1.9.

I've looked through the linux machine and its getting the IP Address of the lo (loopBack) port? I don't know exactly what that is but it should be getting the IP Address of eth0.

I've found that I can subprocess the bash command "ifconfig eth0" but that returns a big block of a string. I can process it down to what I need, but this is going to be running around 3 times a second on a beaglebone so I'd like it to be a little more effecient.

Is there a more elegant way of doing this?

Can I just change the target of gethostname?

Why is it targeting the lo port?

Thanks for your help maners.

Poodimizer
  • 590
  • 1
  • 6
  • 18
  • If anyone is trying to do this and they have a server that will always have internet access there's site http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/ that has a way that resolved the issue. But my server won't always have internet so I can't use it. – Poodimizer Mar 13 '13 at 20:01

2 Answers2

4

Try returning the fully qualified domain name of the machine:

print str(socket.gethostbyname(socket.getfqdn()))

/etc/hosts probably has an entry resolving hostname to 127.0.0.1, which is why socket.gethostbyname() doesn't return what you expect.

Original question asked and answered here, but the socket.getfqdn() solution didn't stick out at a quick glance. Here's the solution for parsing ifconfig output if you decide to go that route. Standard library seems more than sufficient for solving your problem.

Community
  • 1
  • 1
Bryan
  • 17,112
  • 7
  • 57
  • 80
  • I was under the impression that the hosts file was just a dns lookup table for external servers. The print str(socket.gethostbyname(socket.getfqdn())) just returned the same thing though (127.0.1.1). – Poodimizer Mar 13 '13 at 18:04
  • @Poodimizer What does `socket.getfqdn()` return? May also be helpful to see your `hosts` file. – Bryan Mar 13 '13 at 18:05
  • You were right the hosts file had: 127.0.0.1 localhost 127.0.1.1 omap I'm running into an error right now so I can't run socket.fqdn() right now. have to reboot the server. – Poodimizer Mar 13 '13 at 18:24
  • and getfqdn() returns omap. So what is that file based on? Should I have to manually change that file at bootup to match eth0's IP Address? – Poodimizer Mar 13 '13 at 18:32
  • If `socket.getfqdn()` returns `omap` (which I assume is hostname), then the valid IP should be returned from `socket.gethostbyname()`. Shouldn't have to modify `hosts` file. Resolve the error you're receiving and check results. – Bryan Mar 13 '13 at 18:35
  • Hmm, it does resolve to omap, but the hosts file is wrong if its supposed to have the valid LAN IP Address of the ethernet port. – Poodimizer Mar 13 '13 at 18:38
1

netifaces seems like a pretty sweet python module which should do the trick for you.

Josh Bothun
  • 1,324
  • 9
  • 9