How do I write a program to send a local IP address like 192.168.1.102?
since my Linksys router does not assign a fixed local IP address to computers (PC and Mac and Linux), I would like to write a script to have the computers update with each other every minute so that
each machine will contain a list of all PC and Mac names 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 ipconfig on PC and ifconfig on Mac and Linux, and for that programmatically it would be gethostbyname ().
But I tried 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 correct way to do it? I can hack it by doing "ipconfig" in Ruby and using a regular expression to get the result, but it would be nice to do it using a more standard way.
a source to share
Socket::getaddrinfo
there might be more of what you are 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 can just try:
Socket::getaddrinfo('core2duo', 'http')[0][3]
"192.168.1.103"
a source to share
Why not just turn off DHCP for these machines and assign them fixed IP addresses?
Edit in response to comments: At least three years ago (last time I bought a router) Linksys routers allowed you to set a lower IP address for the built-in DHCP server. Then you go to configure the network of individual computers, 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 is 1.99, and the router is configured to output 1.100 and above.
a source to share
If you're going to broadcast datagrams named / ip info, you don't need a computer to find out its own IP address. Just send a datagram containing its name, then use recvfrom () - or equivalents - to catch it on other PCs. This way, recipients can retrieve the sender's IP address directly through the recvfrom () function .
As a bonus, this should work where the sending PC has more than one network adapter.
a source to share