PDA

View Full Version : Need help with bash script - edit etc/hosts file - beginner level



cekma0404
April 26th, 2016, 08:38 AM
HI,

after several unsuccessful attempts to create a simple bash script that will solve my problem at work, I decided to ask the question at this forum.
Please Help me, I 'm a beginner in the bash scripting .
At the company we work in Linux environment.
Because of our job nature, we are daily modifying file at path etc/hosts, and currently hosts file has 80+ lines.
Most of these hosts have the same name but different IP addresses, and depending on which system are we connecting, inside the hosts file we comment all the other lines that we do not need at this point.
Therefore I am trying to make a script that will automatically comment/comment out lines by some keyword in hosts file.

It looks something like this:


127.0.0.1 localhost
127.0.1.1 admin-pc

# TELECOM1
10.1.x.x xxx.provision.lan
10.1.x.x xxx.dhcp.lan
192.1.x.x xxx.dhcp.out

# TELECOM2
10.2.x.x xxx.provision.lan
10.2.x.x xxx.dhcp.lan
192.2.x.x xxx.dhcp.out

# TELECOM3
10.3.x.x xxx.provision.lan
10.3.x.x xxx.dhcp.lan
192.3.x.x xxx.dhcp.out

# TELECOM4
10.5.x.x xxx.provision.lan
10.5.x.x xxx.dhcp.lan
192.5.x.x xxx.dhcp.out

# TELECOM5
10.8.x.x xxx.provision.lan
10.8.x.x xxx.dhcp.lan
192.8.x.x xxx.dhcp.out


So if we want to connect to lets say TELECOM5 we are comment all other lines so it looks like this:


127.0.0.1 localhost
127.0.1.1 admin-pc

# TELECOM1
#10.1.x.x xxx.provision.lan
#10.1.x.x xxx.dhcp.lan
#192.1.x.x xxx.dhcp.out

# TELECOM2
#10.2.x.x xxx.provision.lan
#10.2.x.x xxx.dhcp.lan
#192.2.x.x xxx.dhcp.out

# TELECOM3
#10.3.x.x xxx.provision.lan
#10.3.x.x xxx.dhcp.lan
#192.3.x.x xxx.dhcp.out

# TELECOM4
#10.5.x.x xxx.provision.lan
#10.5.x.x xxx.dhcp.lan
#192.5.x.x xxx.dhcp.out

# TELECOM5
10.8.x.x xxx.provision.lan
10.8.x.x xxx.dhcp.lan
192.8.x.x xxx.dhcp.out



The idea is to write a script that wil do that in most simple way possible.
So lets say the script is called comment.sh and we want to comment everything execept TELECOM5 so we call script in this manner:

./comment.sh TELECOM5


Is that possible? Can you give me an advice how to do this?

Currently I have several hosts file (grouped by telecom names) and I have script that cp and overwrite original hosts file with other (but this is ugly solution).

Thanks in advance.

TheFu
April 26th, 2016, 01:36 PM
Are the "xxx.provision.lan" always identical or are they each different for each different subnet? If different, theres no need to have this script at all. Honestly, doing it this way is an extremely poor practice. The software should have a config file where which hosts are used for these things is configured, not the system. Fix the code, not the system (which is behaving like it should).

Assuming that isn't possible, I'd do it this way. Create a lookup table file, /etc/hosts.lookup.file. Looking like this:


TELECOM5 10.5.x.x xxx.provision.lan
TELECOM5 10.5.x.x xxx.dhcp.lan
TELECOM5 192.5.x.x xxx.dhcp.out

TELECOM8 10.8.x.x xxx.provision.lan
TELECOM8 10.8.x.x xxx.dhcp.lan
TELECOM8 192.8.x.x xxx.dhcp.out
but with all the unique names at the beginning of the line for each IP. Notice how I changed the end to match the subnet 8 for 8, 5 for 5. Why make things non-intuitive?

Then I'd use grep and sed (or perl) to put only the lines I wanted into the /etc/hosts file.

#!/bin/bash

echo "# file generated by comment.sh

127.0.0.1 localhost
127.0.1.1 `hostname`
"

egrep "$1" /etc/hosts.lookup.file | sed -e 's/$1//g'

That would put the desired lines on stdout, which could be redirected into the /etc/hosts file. /path/to/comment.sh TELECOM5 > /tmp/hosts.new
If you need some specific lines at the top of the file, just echo those before the egrep line. Also might want to verify that $1 was passed in.

I would not directly modify the /etc/hosts file until after much end-user testing. Because the resulting file could easily be empty and that would be bad. Really bad.

Hope this helps. There are 5,000 other possible solutions too. You could put the lookup file INSIDE the bash script and do the same thing. If it were perl, I'd create an array of hashes with the data - that would be really easy, but this method above is even easier.