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.
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.