auto create DHCP hostnames for a scope
Following on from private IP address pools for your cluster, you might want a way to automatically create entries for all your machines in DHCP or in your /etc/hosts file - perhaps you have a Cisco router and don't want to edit that config file by hand?
N=1; for x in $(seq -w 254); do echo -e "10.0.0.$N \t node$x"; N=$(expr $N+1); done >> /etc/hosts
sample output:
...
10.0.0.250 node250
10.0.0.251 node251
10.0.0.252 node252
10.0.0.253 node253
10.0.0.254 node254
Using echo -e interprets the escaped \t as a tab character correctly.
(I gather that tab is sometimes the recommended whitespace format e.g. used in the Windows\system32\drivers\etc\hosts file?, rather than just spaces. I also included spaces for legibility here though)
We use (seq -w 254) which iterates through all the numbers 001, 002, ... 254 and the -w flag keeps the leading zeros. We use this value, assigned to $x, to generate consistent looking hostnames, 'node001' etc.
We also take N (starting at 1) and increase it by 1 on each iteration. We use that as the value of the node's IP number in the echo statement.
Finally, it appends to the /etc/hosts file, although you can redirect this output anywhere if you want to check or edit it before use.
Note:
Take care if modifying the command to loop through multiple subnets, eg 10.0.$N.$M - if you are not careful you could end up with 255*65535 lines of output, or in other words, have ip addresses going from 10.0.1.1 to 10.0.1.65535, which is not what you would want!!
=)
Also Note:
If we used $x instead of $N, we may have difficulty resolving nodes < 100.
For example, 'ping node001' could end up trying a dns lookup on "node001.defaultsearch.domain" instead of pinging '10.0.0.1' as expected.
I've seen this behaviour on CentOS / BlueQuartz - The leading zeros on ips lower than x.y.z.100 seemed to confuse the resolver
It had nothing to do with nsswitch.conf, "hosts: files,dns" or /etc/resolv.conf. Restarting nscd (name service caching daemon) by logging in as root and trying
/etc/init.d/nscd restart
/etc/rc.d/init.d/nscd restart (on other flavours perhaps)
to flush cached DNS entries didn't work either, especially since nscd wasn't running at the time...
N=1; for x in $(seq -w 254); do echo -e "10.0.0.$N \t node$x"; N=$(expr $N+1); done >> /etc/hosts
sample output:
...
10.0.0.250 node250
10.0.0.251 node251
10.0.0.252 node252
10.0.0.253 node253
10.0.0.254 node254
Using echo -e interprets the escaped \t as a tab character correctly.
(I gather that tab is sometimes the recommended whitespace format e.g. used in the Windows\system32\drivers\etc\hosts file?, rather than just spaces. I also included spaces for legibility here though)
We use (seq -w 254) which iterates through all the numbers 001, 002, ... 254 and the -w flag keeps the leading zeros. We use this value, assigned to $x, to generate consistent looking hostnames, 'node001' etc.
We also take N (starting at 1) and increase it by 1 on each iteration. We use that as the value of the node's IP number in the echo statement.
Finally, it appends to the /etc/hosts file, although you can redirect this output anywhere if you want to check or edit it before use.
Note:
Take care if modifying the command to loop through multiple subnets, eg 10.0.$N.$M - if you are not careful you could end up with 255*65535 lines of output, or in other words, have ip addresses going from 10.0.1.1 to 10.0.1.65535, which is not what you would want!!
=)
Also Note:
If we used $x instead of $N, we may have difficulty resolving nodes < 100.
For example, 'ping node001' could end up trying a dns lookup on "node001.defaultsearch.domain" instead of pinging '10.0.0.1' as expected.
I've seen this behaviour on CentOS / BlueQuartz - The leading zeros on ips lower than x.y.z.100 seemed to confuse the resolver
It had nothing to do with nsswitch.conf, "hosts: files,dns" or /etc/resolv.conf. Restarting nscd (name service caching daemon) by logging in as root and trying
/etc/init.d/nscd restart
/etc/rc.d/init.d/nscd restart (on other flavours perhaps)
to flush cached DNS entries didn't work either, especially since nscd wasn't running at the time...
Labels: /etc/hosts, config, dhcp, dns, files, flush, hostnames, hosts, script

