Created
May 22, 2013 14:21
-
-
Save gswallow/5627901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Customize this | |
domain="indigobio.com" | |
ns1="8.8.8.8" | |
ns2="4.2.2.2" | |
ndots="2" | |
usage() { | |
echo | |
echo | |
echo "/usr/local/bin/set-ip.sh <hostname> <subdomain> <ip/mask>" | |
echo " hostname = hostname (not an FQDN)" | |
echo " subdomain = qa | test | ascent" | |
echo " ip/mask = IP address / mask (CIDR format, 8-30. Usually /24.)" | |
echo | |
echo " *** A CIDR mask of /24 is 255.255.255.0 ***" | |
echo | |
echo " e.g. set-ip.sh web1 qa 10.120.18.200/24" | |
echo | |
echo | |
} | |
red() { | |
local red='\033[31m' | |
local clear='\033[0m' | |
echo -e "${red}${*}${clear}" | |
} | |
green() { | |
local red='\033[32m' | |
local clear='\033[0m' | |
echo -e "${red}${*}${clear}" | |
} | |
validatepair() { | |
if [ `expr index "$1" '/'` -eq "0" ]; then | |
red "You need to specify an IP/mask." | |
usage | |
exit 1; | |
fi | |
} | |
validateip() { | |
if [ `expr match "$1" '.*[^0-9.]'` -gt "0" ]; then | |
red "Wrong IP format." | |
usage | |
exit 1; | |
fi | |
local ipparts=($(echo $1 | tr "." "\n")) | |
for ((i=0;i<4;i+=1)); do | |
if (( ${ipparts[$i]} > 255 )); then | |
red "Wrong IP format." | |
usage | |
exit 1; | |
fi | |
done | |
} | |
validatecidr() { | |
if (( `expr match "$1" '.*[^0-9]'` > 0 || $1 > 30 )); then | |
red "Wrong netmask format." | |
usage | |
exit 1; | |
fi | |
} | |
validategw() { | |
local gwparts=($(echo $1 | tr "." "\n")) | |
local networkparts=($(echo $2 | tr "." "\n")) | |
local bcastparts=($(echo $3 | tr "." "\n")) | |
for ((i=0;i<4;i+=1)); do | |
if (( ${gwparts[$i]} < ${networkparts[$i]} || ${gwparts[$i]} > ${bcastparts[$i]} )); then | |
return 1; | |
fi | |
done | |
return 0; | |
} | |
cidr2mask() { | |
local mask="" | |
local netaddr="" | |
local bcast="" | |
local gateway="" | |
local full_octets=$(($2/8)) | |
local partial_octet=$(($2%8)) | |
local ipparts=($(echo $1 | tr "." "\n")) | |
for ((i=0;i<4;i+=1)); do | |
if [ $i -lt $full_octets ]; then | |
mask+=255 | |
netaddr+=${ipparts[$i]} | |
bcast+=${ipparts[$i]} | |
gateway+=${ipparts[$i]} | |
elif [ $i -eq $full_octets ]; then | |
mask+=$((256 - 2**(8-$partial_octet))) | |
netaddr+=$(( ${ipparts[$i]} - ( ${ipparts[$i]} % ( 2 ** ( 8 - $partial_octet )) ) )) | |
bcast+=$(( ${ipparts[$i]} + (2**(8-$partial_octet) - (${ipparts[$i]} % (2**(8-$partial_octet) ))) - 1)) | |
if [ $i -lt 3 ]; then | |
gateway+=$(( ${ipparts[$i]} - ( ${ipparts[$i]} % ( 2 ** ( 8 - $partial_octet ) )) )) | |
else | |
gateway+=$(( ${ipparts[$i]} - ( ${ipparts[$i]} % ( 2 ** ( 8 - $partial_octet ) )) +1 )) | |
fi | |
else | |
mask+=0 | |
gateway+=1 | |
netaddr+=0 | |
bcast+=255 | |
fi | |
test $i -lt 3 && mask+=. | |
test $i -lt 3 && netaddr+=. | |
test $i -lt 3 && bcast+=. | |
test $i -lt 3 && gateway+=. | |
done | |
echo $mask $netaddr $bcast $gateway | |
} | |
case "$1" in | |
-h|--help) | |
usage | |
exit 0; | |
;; | |
*) | |
hostname=$1 | |
;; | |
esac | |
shift | |
if [ $# -ne 2 ]; then | |
red "You need to supply three arguments." | |
usage | |
exit 1; | |
fi | |
case "$1" in | |
qa|test|ascent) | |
class=$1 | |
;; | |
*) | |
red "Subdomain must be either qa, test, or ascent." | |
usage | |
exit 1; | |
;; | |
esac | |
shift | |
# Confirm that the IP address is address/mask | |
addrmask=$1 | |
validatepair $addrmask | |
ip=${addrmask%/*} | |
validateip $ip | |
cidrmask=${addrmask#*/} | |
validatecidr $cidrmask | |
# Calculate mask, network address, broadcast and supposed gateway. | |
# Yank them out of an array that gets returned by cidr2mask(). | |
tuple=(`cidr2mask $ip $cidrmask`) | |
netmask=${tuple[0]} | |
network=${tuple[1]} | |
bcast=${tuple[2]} | |
gateway=${tuple[3]} | |
# Show the user what we think the gateway should be and allow him | |
# to correct it (or mess it up). | |
while true; do | |
echo | |
echo -n "Please confirm or change the gateway address: [$gateway] " | |
read newgw | |
newgw=${newgw:-$gateway} | |
if validategw $newgw $network $bcast; then | |
gateway=$newgw | |
break | |
else | |
echo -e "\033[31mOut of range.\033[0m" | |
fi | |
done | |
# VMware will set the immutable flag on /etc/hosts when you customze a VM using a customization profile. | |
chattr -i /etc/hosts | |
cat > /etc/network/interfaces <<EOF | |
# This file describes the network interfaces available on your system | |
# and how to activate them. For more information, see interfaces(5). | |
# The loopback network interface | |
auto lo | |
iface lo inet loopback | |
# The primary network interface | |
auto eth0 | |
iface eth0 inet static | |
address $ip | |
netmask $netmask | |
network $network | |
broadcast $bcast | |
gateway $gateway | |
# dns-* options are implemented by the resolvconf package, if installed | |
dns-nameservers $ns1 $ns2 | |
dns-search $class.$domain | |
EOF | |
cat > /etc/hosts <<EOF | |
127.0.0.1 localhost | |
$ip $hostname.$class.$domain $hostname | |
# The following lines are desirable for IPv6 capable hosts | |
::1 ip6-localhost ip6-loopback | |
fe00::0 ip6-localnet | |
ff00::0 ip6-mcastprefix | |
ff02::1 ip6-allnodes | |
ff02::2 ip6-allrouters | |
EOF | |
cat > /etc/resolv.conf <<EOF | |
search $class.domain | |
options ndots:$ndots | |
nameserver $ns1 | |
nameserver $ns2 | |
EOF | |
echo "options ndots:$ndots" > /etc/resolvconf/resolv.conf.d/base | |
echo $hostname > /etc/hostname | |
hostname $hostname | |
if [ -e "/etc/udev/rules.d/70-persistent-net.rules" ]; then | |
rm -f /etc/udev/rules.d/70-persistent-net.rules | |
fi | |
( ifdown -a ; ifup -a ) > /dev/null 2>&1 & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment