You are not logged in.

#1 2007-12-05 05:50:13

0l33l
Member
Registered: 2007-09-15
Posts: 28

/etc/hosts ip update

Hi all smile

What I would like to do is have DHCP (through NetworkManager) update the /etc/hosts file when it gets an IP. The problem I am running into now is that I have to go and manually edit my hosts file to update my current IP, otherwise a program the networking code in my java program breaks sad
In other words, I need 192.168.1.106 to be kept current.
/etc/hosts

#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1       localhost.localdomain   localhost
192.168.1.106   arch61  localhost
# End of file

/etc/resolv.conf

# generated by NetworkManager, do not edit!

search hsd1.ca.comcast.net.


nameserver 68.87.76.178
nameserver 68.87.78.130

/etc/rc.conf

HOSTNAME="arch61"
lo="lo 127.0.0.1"
eth0="dhcp"
INTERFACES=(lo !eth0)
#gateway="default gw 192.168.0.1"
ROUTES=(!gateway)

Thanks smile

Offline

#2 2007-12-05 09:06:05

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: /etc/hosts ip update

i don't know much if anything about nm, but i do think there might be a solution in a script of sorts, an cron

might i just ask however, why not just point the names to 127.0.0.1 ,, 192.168.0.106 is just your LAN ip,
it goes to the router/modem and effectively bounces back, which is a little, well redundant imo

or maybe go static, this i think is the better option, then you almost certainly won't need to worry about changing addresses

Offline

#3 2007-12-05 10:02:30

0l33l
Member
Registered: 2007-09-15
Posts: 28

Re: /etc/hosts ip update

kumico wrote:

i don't know much if anything about nm, but i do think there might be a solution in a script of sorts, an cron

might i just ask however, why not just point the names to 127.0.0.1 ,, 192.168.0.106 is just your LAN ip,
it goes to the router/modem and effectively bounces back, which is a little, well redundant imo

or maybe go static, this i think is the better option, then you almost certainly won't need to worry about changing addresses

Thanks for the reply smile

The problem with making it a static IP is that when I go to campus, I get a random IP. I've seen 169.xxx.xxx.xxx IPs, or 128.xxx.xxx.xxx IPs. I can't really ask the school to assign only one IP to me, especially when I go between buildings.

If I do something like this

#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1       localhost.localdomain   localhost
127.0.0.1       arch61  localhost
# End of file

I run into the bug with java where it resolves my computer's IP to be 127.0.0.1. The fix for this error is modifying some java files, which I don't have the source to.

Offline

#4 2007-12-05 11:14:49

mykey
Member
From: out of the blue
Registered: 2007-03-02
Posts: 113

Re: /etc/hosts ip update

If it says in your rc.conf

eth0="dhcp"

you just can't 'keep the IP current' because you leave it up to the DHCP server in the network to take care of it.

DHCP - Dynamic Host Configuration Protocol

Offline

#5 2007-12-05 15:45:29

abhidg
Member
From: City of Kol
Registered: 2006-07-01
Posts: 184
Website

Re: /etc/hosts ip update

If  the dhcp daemon has an option for automatically running some script when it gets a new IP, then you can write a small script which takes the output of ifconfig and puts it into /etc/hosts everytime you get a new IP. I dont know though if this is possible.

Offline

#6 2007-12-05 17:26:37

0l33l
Member
Registered: 2007-09-15
Posts: 28

Re: /etc/hosts ip update

mykey wrote:

If it says in your rc.conf

eth0="dhcp"

you just can't 'keep the IP current' because you leave it up to the DHCP server in the network to take care of it.

DHCP - Dynamic Host Configuration Protocol

I'm well aware that I can't keep the current IP. That's why I am asking the question smile

Offline

#7 2007-12-05 17:43:13

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: /etc/hosts ip update

Why do you need to do this?  Why can't your java program use the loopback interface?  Or check which interfaces are available and their IP addresses?  (I'm pretty sure there's a kernel API function for the latter, but I don't recall it specifically.)

Offline

#8 2007-12-07 05:29:28

0l33l
Member
Registered: 2007-09-15
Posts: 28

Re: /etc/hosts ip update

kumico: Thank you for the reply. I tried to get your python script working, and I couldn't.

I backed up my /etc/hosts file, and ran "sudo python ipup.py". It doesn't change the file, or even update saying "updated ip address". And yes, I did update the interface to "wlan0", which is my intel 4965AGN card. When I run it, it doesn't output anything, or seem to change anything either. The IP address in the hosts file does differ from my current IP address.

Offline

#9 2007-12-07 08:32:39

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: /etc/hosts ip update

updated,,, better error handling, etc
ipup.py

#!/usr/bin/env python
#################################notes#################################
# ipup.py r3                                                          #
# ipup.py updates the hosts file,with the current *AN ip address      #
#                               ~configure~                           #
# hosts = '/etc/hosts' = location of hosts file                       #
# interface = 'ath0|wlan0|etc0' = name of the inerface to poll        #
#######################################################################
import re
from subprocess import PIPE, Popen

hosts = '/etc/hosts'
interface = 'ath0'

handle = open(hosts, 'r')
hbuf = handle.read()
handle.close()
ibuf = Popen(['ifconfig', interface], stdout=PIPE).stdout.read()
ipat = re.compile('inet addr:(\d+\.\d+\.\d+\.\d+) ')
ip = ipat.search(ibuf)
ip = ip and ip.group(1)
hpat = re.compile('(.*\D)(\d+\.\d+\.\d+\.\d+)(.*#ip~up#.*)', re.DOTALL)
ih = hpat.search(hbuf)
if ih and ip:
  if ip != ih.group(2):
    try:
      out = ih.group(1) + ip + ih.group(3)
      handle = open(hosts, 'w')
      handle.write(out)
      print 'updated ip address to', ip
    except:
      print 'failed to update hosts file,'
      print 'maybe you don\'t have permision to write to', hosts
  else:
    print hosts, 'is already up to date'
else:
  ih = ih and ih.group(2)
  print 'failed to get ip address(es)'
  print 'old ip(' + hosts + '):', ih
  print '  new ip(ifconfig):', ip

~~~~~~~~~~~~~~~~~~~~~~~~~~~

don't forget, the hosts file needs to have the line
`````````````````````````````````````````
127.0.0.1 arch61 localhost #ip~up#
``````````````````````````````````````````

Last edited by kumico (2007-12-07 08:57:12)

Offline

#10 2007-12-07 08:38:39

0l33l
Member
Registered: 2007-09-15
Posts: 28

Re: /etc/hosts ip update

dburban ~  $  sudo python ipup.py 
failed to get ip address(s)
old ip(/etc/hosts):
Traceback (most recent call last):
  File "ipup.py", line 38, in <module>
    print 'old ip(' + hosts + '):', ih.group(2)
AttributeError: 'NoneType' object has no attribute 'group'
dburban ~  $

My hosts file:

dburban ~  $  cat /etc/hosts
#
# /etc/hosts: static lookup table for host names
#

#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1       localhost.localdomain   localhost
192.168.1.112   arch61  localhost
# End of file

Offline

#11 2007-12-07 08:58:36

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: /etc/hosts ip update

revision 3 above handles ,most errors properly ,, also don't forget to add '#ip~up# to the end of the line you need updating'

Offline

#12 2007-12-07 10:30:43

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: /etc/hosts ip update

You're fixing the wrong thing. The Java program is broken.

It should not try to gain the current IP from the hosts file. The hosts file probably shouldnt even contain your current IP if you use dhcp. You're better off aliasing your hostname to 127.0.0.1, not your current IP.

If your java application needs to connect to the computer it's running on, it should use loopback (127.0.0.1). If it wants the computer's IP, it should get it via whatever methods Java offers -- and it will have something.

Offline

#13 2007-12-07 10:50:04

0l33l
Member
Registered: 2007-09-15
Posts: 28

Re: /etc/hosts ip update

kumico wrote:

revision 3 above handles ,most errors properly ,, also don't forget to add '#ip~up# to the end of the line you need updating'

Works great!

Thank you!

Offline

#14 2007-12-07 10:51:16

0l33l
Member
Registered: 2007-09-15
Posts: 28

Re: /etc/hosts ip update

iphitus wrote:

You're fixing the wrong thing. The Java program is broken.

It should not try to gain the current IP from the hosts file. The hosts file probably shouldnt even contain your current IP if you use dhcp. You're better off aliasing your hostname to 127.0.0.1, not your current IP.

If your java application needs to connect to the computer it's running on, it should use loopback (127.0.0.1). If it wants the computer's IP, it should get it via whatever methods Java offers -- and it will have something.

I understand. But I do not have the source to the jar with the problem. Otherwise I would fix it, as I found the solution to this error online.

Offline

#15 2007-12-07 10:52:23

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: /etc/hosts ip update

yw:)

Offline

Board footer

Powered by FluxBB