You are not logged in.
Pages: 1
Hi all
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
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
Offline
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
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 imoor 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
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
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
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
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
Offline
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
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
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
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
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
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
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
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
yw:)
Offline
Pages: 1