views:

568

answers:

6

since my Linksys router doesn't assign a fixed local IP to the computers (PC and Mac and Linux), i'd like to write a script so that every minute, the computers will update to each other so that

http://localhost/list.html

on each machine will contain a list of names of all PC and Mac and a link to their apache server (pointing to http://192.168.1.102, etc)

it looks like a way to find out the local IP address is by ipconfig on PC, and ifconfig on the Mac and Linux, and to do it programmatically, it will be gethostbyname().

But I tried on Ruby, that

irb(main):001:0> require 'socket'
=> true

irb(main):002:0> p Socket::gethostbyname("localhost")
["Core2Duo", [], 2, "\177\000\000\001"]

irb(main):005:0> p Socket::gethostbyname("core2duo")
["Core2Duo", [], 2, "\300\250\001g"]
=> nil

and ipconfig actually shows

IPv4 Address. . . . . . . . . . . : 192.168.1.103

so is this the right way to do it? I can hack it by executing "ipconfig" in Ruby and use regular expression to get the result, but would be nice to do it using a more standard way.

+1  A: 

You can do this in Java using:

Socket s = new Socket();
s.getLocalSocketAddress();
Joshua
+2  A: 

Socket::getaddrinfo might be more of what you're looking for:

Socket::getaddrinfo('localhost', 'http')
[["AF_INET", 80, "localhost", "127.0.0.1", ...]]

Socket::getaddrinfo('core2duo', 'http')
[["AF_INET", 80, "Core2Duo", "192.168.1.103", ...]]

Or, you might just try:

Socket::getaddrinfo('core2duo', 'http')[0][3]
"192.168.1.103"
Jonathan Lonowski
A: 

The problem in the Ruby script might be that there is no DNS name resolution on your network, this is often hidden if you usually share stuff between computers with SMB/CIFS because that has it's own name discovery protocol.

robertc
+1  A: 

If you run Bonjour on your Windows systems and avahi on your Linux systems, you can do away with the need to determine each system's IP address. You can then simply address each system using "hostname.local". More info here.

sigjuice
my netgear router could assign a fixed local IP to each computer... but i found the linksys router to be more reliable. my friend told me his d-link router with wireless-n is fast and reliable and can assign fixed local IP too. maybe i will consider that if i use cable internet for speed and then DSL internet for reliability and buy another router.
動靜能量
With Bonjour, you don't need to have fixed local IPs. The hosts tell their IP addresses to each other using multicast DNS.
sigjuice
Bonjour is great for home networks. Not so good on corporate networks due to restrictions and blocking.
Joshua
I agree. I didn't think the OP was dealing with a corporate network. I'd expect fully functional DNS on one.
sigjuice
+1  A: 

Why not just turn off DHCP for those machines and assign them fixed IP addresses?

Edit in response to comments: At least as-of three years or so ago (last time I bought a router), Linksys routers allowed you to set the bottom IP address for the built-in DHCP server. Then, you go into the individual machines' network setup, disable DHCP, and assign physical addresses. For example, on my home network the router is 192.168.1.1, the Terastation is 1.2, the printer is 1.3, my Linux box is 1.99, and the router is configured to give out 1.100 and above.

kdgregory
can a linksys router do that too? so using DHCP is always optional and have no additional benefit?
動靜能量
My Linksys WRT54G router can do this.
sigjuice
A: 

If you are going to be broadcasting datagrams with the name/ip info in, then you don't really need a PC to know its own IP address. Just send the datagram containing its name, then use recvfrom() - or equivalents - to catch it at the other PCs. That way the receivers can extract the sender's IP address directly via the recvfrom() function.

As a bonus, that should work where the sending PC has more than one network adapter.

Bill99