Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

append contents to /etc/hosts 2

Status
Not open for further replies.

gatetec

MIS
Mar 22, 2007
420
US
I need to add printer queues to /etc/hosts for a variety of reasons; this is not a recommended approach though. I am running two sets of dual nodes; prod1 & prod2; prodstby1 & prodstby2.

What I am trying to do is:

1) modify /etc/hosts of prod1 and let /etc/hosts of prod2 pick up the change on /etc/hosts of prod1
We can simply overwrite /etc/hosts of prod2 with /etc/hosts of prod1, but if there is a way that it just appends the new entry added of prod1 onto /etc/hosts of prod2, that will be great. Also, we can run crontab to overwrite/append on certain intervals (every hour, every two hours, etc). But, I am wondering if it recognizes the change on /etc/hosts of prod1 and then overwrite/append whenever the change occurs.

2) make copy of /etc/hosts file under /etc
Backing up file is critical always. I like to back up /etc/hosts with the naming convention i.e. hosts200708011235 (yyyymmddhhmm) so that I know when the backup was made

3) Now, copying /etc/hosts to prodstby1 & prodstby2
prod1 & prod2; prodstby1 & prodstby2 have two different /etc/hosts entries, so I can’t just overwrite /etc/hosts of prodstby1 & prodstby2 with the one of prod1 & prod2. If I can append the change on /etc/hosts of prod1, it will be great. If not, I need to rcp the latest /etc/hosts file, i.e. hosts200708011235, to prodstby1 & prodstby2.

Please let me know how to accomplish these tasks.

thx so much
 
You seem to be making problems for yourself

Why not add the printer to all systems simultaneously by creating a script which looks like
Code:
ipadd=$1
printer_name=$2
bkupnum=$£
for host in prod1 prod2 prodstb1 prodstb2
do
  ssh $host "cp /etc/host /etc/hosts.$bkupnum"
  ssh $host "echo $ipadd $printer_name >> /etc/hosts
done
And tnen call the script with
Code:
myamendhosts.ksh 192.168.0.1 new_printer 12345
Of course you should add some sanity checks to the code. Checking that all three prameters exist would be a start and, as someone who once added an invalid address to host, I would also recommend vetting the ip address. Something like
Code:
[[ $# -eq 3 ]] || { echo invalid paramter count; exit; }
expr $ipadd : "[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*" || { echo invalid IP address; exit; }
I'm sure someone can tidy up the expr statement

Ceci n'est pas une signature
Columb Healy
 
It is a great suggestion. A question for you, please.

>> bkupnum=$£

I like to put like hosts200708011235 (yyyymmddhhmm), but $£ put s nothing but hosts.$£.

thx much
 
Actually, NOW=$(date +"%m%d%Y""%H%M") works for me.

where shoudl I put this expr in?

[[ $# -eq 3 ]] || { echo invalid paramter count; exit; }
expr $ipadd : "[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*" || { echo invalid IP address; exit; }
 
I'm guessing columb's "$£" was a typo and should have been "$3".

You should put the line with "-eq 3" in it as the first executable line, since it's checking the number of parameters.

The expr line should be right before the line with the "for" statement, since the ipadd variable needs to be set before you reach it (plus it's generally a good idea to make sure you've saved off all of your command line arguments before you start mucking about with anything else).

- Rod

IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

A Simple Code for Posting on the Web
 
Rod is spot on with the typo.
So, if you're dropping the backup number the script becomes
Code:
#!/bin/ksh
ipadd=$1
printer_name=$2
[[ $# -eq 3 ]] || { echo invalid paramter count; exit; }
expr $ipadd : "[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*" || { echo invalid IP address; exit; }
NOW=$(date +"%m%d%Y""%H%M")
for host in prod1 prod2 prodstb1 prodstb2
do
  ssh $host "cp /etc/host /etc/hosts.$NOW"
  ssh $host "echo $ipadd $printer_name >> /etc/hosts
done

It's a little overkill but...
As you may end up forgetting how the script works
Code:
#!/bin/ksh

print_usage()
  {
  echo $@
  echo "Usage myaddprinterscript <Ip Address> <Printer Name>"
  exit 1
  }

ipadd=$1
printer_name=$2
[[ $# -eq 3 ]] || print_usage "invalid paramter count"
expr $ipadd : "[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*" || print_usage "invalid IP address"
NOW=$(date +"%m%d%Y""%H%M")
for host in prod1 prod2 prodstb1 prodstb2
do
  ssh $host "cp /etc/host /etc/hosts.$NOW" || print_usage "hosts copy failed on $host"
  ssh $host "echo $ipadd $printer_name >> /etc/hosts || print_usage "Failed to add $printer_name to $host"
done

Ceci n'est pas une signature
Columb Healy
 
I tried with valid IP and printer name, but it gives me:
"invalid paramter count".

./appendhosts.ksh 10.3.3.16 prtnursebp1
invalid paramter count


Am I missing anything?

I like to add one more entry on the input:

ipadd=$1
printer_name=$2
hostq_name=$3

so, I will need to enter i.e.

./appendhosts.ksh 10.3.3.16 prtnursebp1 prt-nurseb-ps1
where prt-nurseb-ps1 is the host name of printer.

However, on the /etc/hosts file, I need to let it put:

10.3.3.16 prtnursebp1 ##hostname: prt-nurseb-ps1

Appending IP and pinter name is done, but I'm not sure how to take care of '##hostname: and the hostQ name' piece.

How do I put ##hostname: and the hostQ name like the example above?

thx much
 
Ooops

I/we dropped the number of parameters but didn't amend the test so it was expecting three parameters when two was correct.
Code:
[[ $# -eq 3 ]] || print_usage "invalid paramter count"
should be
Code:
[[ $# -eq [b]2[/b] ]] || print_usage "invalid paramter count"

However, having said that, you now want to add a third parameter, the host name, to be added as a comment
The code now becomes
Code:
#!/bin/ksh

print_usage()
  {
  echo $@
  echo "Usage myaddprinterscript <Ip Address> <Printer Name> <host>"
  exit 1
  }

ipadd=$1
printer_name=$2
host_name=$3
[[ $# -eq 3 ]] || print_usage "invalid paramter count"
expr $ipadd : "[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*" || print_usage "invalid IP address"
NOW=$(date +"%m%d%Y""%H%M")
for host in prod1 prod2 prodstb1 prodstb2
do
  ssh $host "cp /etc/host /etc/hosts.$NOW" || print_usage "hosts copy failed on $host"
  ssh $host "echo $ipadd $printer_name \\#\\#$host_name >> /etc/hosts || print_usage "Failed to add $printer_name to $host"
done
Note the double backslashes before the '#'s, one to escape the backslash in the local shell, one to escape the hash in the remote shell.

Ceci n'est pas une signature
Columb Healy
 
thanks so much. It works super well, and saves me a ton of time. I really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top