VMware Cloud Community
dobyrne
Contributor
Contributor
Jump to solution

adding text to hosts file

I want to be able to automate in a scripting process on how to add a server name to the hosts file in the folder etc.

for example 10.2.2.2 esxserver004.company.com.au esxserver004

I want to add esxserver004 to the end of the line. any ideas?

0 Kudos
1 Solution

Accepted Solutions
rpartmann
Hot Shot
Hot Shot
Jump to solution

Hi,

grep -v is a inverted grep, it prints all the lines NOT containing the pattern and then

echo add the whole line.

or try as i posted the cat file | sed .....

bye,

Reinhard

.

ps: Award points if you find answers helpful. Thanks.

ps: Award points if you find answers helpful. Thanks.

View solution in original post

0 Kudos
8 Replies
mcowger
Immortal
Immortal
Jump to solution

perl -p -i -e 's/10.2.2.2 esxserver004.company.com.au/10.2.2.2 esxserver004.company.com.au esxserver004' /etc/hosts

--Matt

--Matt VCDX #52 blog.cowger.us
0 Kudos
rpartmann
Hot Shot
Hot Shot
Jump to solution

hi,

mv /etc/hosts /tmp/hosts

cat /tmp/hosts | sed 's/\(.esxserver004.$\)/\1 esxserver004/ig' > /etc/hosts

hth,

Reinhard

ps: Award points if you find answers helpful. Thanks.

ps: Award points if you find answers helpful. Thanks.
0 Kudos
dwight
Enthusiast
Enthusiast
Jump to solution

If you just want to append a new entry, with the items tab seperated, the easiest option would be to use echo.

cp /etc/hosts /etc/hosts.backup

grep -v "esxserver004.company.com.au" /etc/hosts.backup > /etc/hosts

echo -e "10.2.2.2\tesxserver004.company.com.au\tesxserver004" >> /etc/hosts

-


RHCE, VCP

Blog: http://computing.dwighthubbard.info

RHCE, VCP Blog: http://computing.dwighthubbard.info
dobyrne
Contributor
Contributor
Jump to solution

HI Matt,

I tried the command and got substitution replacement not terminated at -e line 1. not sure what that mean?? I changed the line to.

perl -p -i -e 's/10.2.70.148 esxbnevh004.mbf.com.au/10.2.70.148 esxbnevh004.mbf.com.au esxbnevh004' /etc/hosts

Damon

Damon O'Byrne

MBF Australia Ltd

Intel Team Lead

Lv 1: 50 Bridge St

Sydney, NSW 2000

Telephone +61 2 9323 9684

Fax +61 2 8874 1966

Facsimile +61 2 9323 9054

Email: damon.obyrne@mbf.com.au

>>> mcowger <communities-emailer@vmware.com> 31/01/2008 9:22 am >>>

,

A new message was posted in the thread "adding text to hosts file":

http://communities.vmware.com/message/852528

Author : mcowger

Profile : http://communities.vmware.com/people/mcowger

Message:

0 Kudos
mcowger
Immortal
Immortal
Jump to solution

sorry, that should have an extra / at the end:

perl -p -i -e 's/10.2.70.148 esxbnevh004.mbf.com.au/10.2.70.148 esxbnevh004.mbf.com.au esxbnevh004/' /etc/hosts

--Matt

--Matt VCDX #52 blog.cowger.us
0 Kudos
dobyrne
Contributor
Contributor
Jump to solution

I tried it though wan to add the line at the entry at the end of the line instead of the appending to anther line. any ideas?

Damon

10.2.70.154 esxbnevh004.mbf.com.au esxbnevh004 (this at the end of the line)

echo -e "10.2.70.154\tesxbnevh004.mbf.com.au\tesxbnevh004" >> /etc/hosts

Damon O'Byrne

MBF Australia Ltd

Intel Team Lead

Lv 1: 50 Bridge St

Sydney, NSW 2000

Telephone +61 2 9323 9684

Fax +61 2 8874 1966

Facsimile +61 2 9323 9054

Email: damon.obyrne@mbf.com.au

>>> "dwight@dwightandamy.com" <communities-emailer@vmware.com> 31/01/2008 9:53 am >>>

,

A new message was posted in the thread "adding text to hosts file":

http://communities.vmware.com/message/852555

Author : dwight@dwightandamy.com

Email : dwight@dwightandamy.com

Profile : http://communities.vmware.com/people/dwight@dwightandamy.com

Message:

0 Kudos
rpartmann
Hot Shot
Hot Shot
Jump to solution

Hi,

grep -v is a inverted grep, it prints all the lines NOT containing the pattern and then

echo add the whole line.

or try as i posted the cat file | sed .....

bye,

Reinhard

.

ps: Award points if you find answers helpful. Thanks.

ps: Award points if you find answers helpful. Thanks.
0 Kudos
dobyrne
Contributor
Contributor
Jump to solution

mv /etc/hosts /tmp/hosts

cat /tmp/hosts | sed 's/\(.esxserver004.$\)/\1 esxserver004/ig' &gt; /etc/hosts

I will go wit hthis answer as it seems towork for me - thanks guys.

0 Kudos