LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   update /etc/hosts (https://www.linuxquestions.org/questions/linux-general-1/update-etc-hosts-937386/)

shogun1234 03-31-2012 04:57 AM

update /etc/hosts
 
I setup 4 virtualbox vms which have dynamic ip (my env uses dhcp ).

Now I need to ssh to each vm so when ip is changed, I have to check vm's ip and update /etc/hosts so that when next time ssh user@vm_host would work.

So I wrote a simple script and want to update /etc/hosts once vms are started up. But it seems the content of /etc/hosts won't be changed after executing the script, and the console shows '127.0.0.1: not found' What may go wrong? And hot to fix it?

Below is the script content.

Code:

for i in 1 2 3 4
do
        IP=`VBoxManage guestproperty get box$i "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{print $2}'`
       
        `sudo sed 's/^(.*)                box$i\.domain\.com        box$i/^$IP        box$i\.domain\.com        box$i/g' /etc/hosts`
done


/etc/hosts looks as below:
Code:

127.0.0.1        localhost
192.168.1.111        my_host.domain.com        my_host
192.168.1.112        box1.domain.com        box1
192.168.1.114        box2.domain.com        box2
192.168.1.159        box3.domain.com        box3
192.168.1.199        box4.domain.com        box4

Thanks for advice.

ericson007 03-31-2012 06:13 AM

I know this is not specifically what you have asked, however, why don't you just configure the individual machines with a static ip? There are only 4, it is not like there are thousands.

shogun1234 03-31-2012 06:29 AM

I share network environment with other people in the building, so ip would be conflict if using the same one.

Quote:

Originally Posted by ericson007 (Post 4641349)
I know this is not specifically what you have asked, however, why don't you just configure the individual machines with a static ip? There are only 4, it is not like there are thousands.


TobiSGD 03-31-2012 06:34 AM

Just choose a different subnet for the VMs and you should be fine.

ericson007 03-31-2012 06:38 AM

You can also create a seperate subnet as the previous post said, or if you have control over the DHCP server or know what the address pool range is, assign 4 ip addresses that is not in the shared pool. That way there won't be conflicts either.

catkin 03-31-2012 07:32 AM

If you are getting the IPs OK, this is a straight sed question.

The sed command will not work as intended because the single quotes prevent substitution of the "$i"s. Change from single to double quotes and see if that works.

BTW the first part doesn't work for me:
Code:

c@CW8:~/d$ VBoxManage guestproperty get CW8vDS "/VirtualBox/GuestInfo/Net/0/V4/IP"
No value set!


shogun1234 04-01-2012 02:09 AM

Quote:

Originally Posted by catkin (Post 4641380)
If you are getting the IPs OK, this is a straight sed question.

The sed command will not work as intended because the single quotes prevent substitution of the "$i"s. Change from single to double quotes and see if that works.

BTW the first part doesn't work for me:
Code:

c@CW8:~/d$ VBoxManage guestproperty get CW8vDS "/VirtualBox/GuestInfo/Net/0/V4/IP"
No value set!


In addition to change from single quote to double quote. Adding -r for extended-re is required. Following command achieve the effect.
Code:

#!/bin/sh

for i in 1 2 3 4
do
        IP=`VBoxManage guestproperty get box$i "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{print $2}'`
        `sudo sed -i~ -r "s/^(.*)\tbox$i\.domain\.com\tbox$i/$IP\tbox$i\.domain\.com\tbox$i/g" /etc/hosts`
done

Thanks for all your help.


All times are GMT -5. The time now is 02:46 PM.