IPv6: Update edithosts.sh accept parameters

This commit is contained in:
Sheng Yang 2013-01-24 20:09:37 -08:00
parent 6169c1d724
commit 9300a02ba4
3 changed files with 76 additions and 55 deletions

View File

@ -26,12 +26,37 @@
# $5 : nameserver on default nic # $5 : nameserver on default nic
# $6 : comma separated static routes # $6 : comma separated static routes
mac=$1 usage() {
ip=$2 printf "Usage: %s: -m <MAC address> -4 <IPv4 address> -h <hostname> -d <default router> -n <name server address> -s <Routes> \n" $(basename $0) >&2
host=$3 }
dflt=$4
dns=$5 mac=
routes=$6 ipv4=
host=
dflt=
dns=
routes=
while getopts 'm:4:h:d:n:s:' OPTION
do
case $OPTION in
m) mac="$OPTARG"
;;
4) ipv4="$OPTARG"
;;
h) host="$OPTARG"
;;
d) dflt="$OPTARG"
;;
n) dns="$OPTARG"
;;
s) routes="$OPTARG"
;;
?) usage
exit 2
;;
esac
done
DHCP_HOSTS=/etc/dhcphosts.txt DHCP_HOSTS=/etc/dhcphosts.txt
DHCP_OPTS=/etc/dhcpopts.txt DHCP_OPTS=/etc/dhcpopts.txt
@ -71,25 +96,25 @@ logger -t cloud "edithosts: update $1 $2 $3 to hosts"
#delete any previous entries from the dhcp hosts file #delete any previous entries from the dhcp hosts file
sed -i /$mac/d $DHCP_HOSTS sed -i /$mac/d $DHCP_HOSTS
sed -i /$ip,/d $DHCP_HOSTS sed -i /$ipv4,/d $DHCP_HOSTS
sed -i /$host,/d $DHCP_HOSTS sed -i /$host,/d $DHCP_HOSTS
#put in the new entry #put in the new entry
echo "$mac,$ip,$host,infinite" >>$DHCP_HOSTS echo "$mac,$ipv4,$host,infinite" >>$DHCP_HOSTS
#delete leases to supplied mac and ip addresses #delete leases to supplied mac and ip addresses
sed -i /$mac/d $DHCP_LEASES sed -i /$mac/d $DHCP_LEASES
sed -i /"$ip "/d $DHCP_LEASES sed -i /"$ipv4 "/d $DHCP_LEASES
sed -i /"$host "/d $DHCP_LEASES sed -i /"$host "/d $DHCP_LEASES
#put in the new entry #put in the new entry
echo "0 $mac $ip $host *" >> $DHCP_LEASES echo "0 $mac $ipv4 $host *" >> $DHCP_LEASES
#edit hosts file as well #edit hosts file as well
sed -i /"$ip "/d $HOSTS sed -i /"$ipv4 "/d $HOSTS
sed -i /" $host$"/d $HOSTS sed -i /" $host$"/d $HOSTS
echo "$ip $host" >> $HOSTS echo "$ipv4 $host" >> $HOSTS
if [ "$dflt" != "" ] if [ "$dflt" != "" ]
then then
@ -97,26 +122,26 @@ then
sed -i /dhcp-optsfile/d /etc/dnsmasq.conf sed -i /dhcp-optsfile/d /etc/dnsmasq.conf
echo "dhcp-optsfile=$DHCP_OPTS" >> /etc/dnsmasq.conf echo "dhcp-optsfile=$DHCP_OPTS" >> /etc/dnsmasq.conf
tag=$(echo $ip | tr '.' '_') tag=$(echo $ipv4 | tr '.' '_')
sed -i /$tag/d $DHCP_OPTS sed -i /$tag/d $DHCP_OPTS
if [ "$dflt" != "0.0.0.0" ] if [ "$dflt" != "0.0.0.0" ]
then then
logger -t cloud "$0: setting default router for $ip to $dflt" logger -t cloud "$0: setting default router for $ipv4 to $dflt"
echo "$tag,3,$dflt" >> $DHCP_OPTS echo "$tag,3,$dflt" >> $DHCP_OPTS
else else
logger -t cloud "$0: unset default router for $ip" logger -t cloud "$0: unset default router for $ipv4"
echo "$tag,3," >> $DHCP_OPTS echo "$tag,3," >> $DHCP_OPTS
fi fi
if [ "$dns" != "" ] if [ "$dns" != "" ]
then then
logger -t cloud "$0: setting dns server for $ip to $dns" logger -t cloud "$0: setting dns server for $ipv4 to $dns"
echo "$tag,6,$dns" >> $DHCP_OPTS echo "$tag,6,$dns" >> $DHCP_OPTS
fi fi
[ "$routes" != "" ] && echo "$tag,121,$routes" >> $DHCP_OPTS [ "$routes" != "" ] && echo "$tag,121,$routes" >> $DHCP_OPTS
#delete entry we just put in because we need a tag #delete entry we just put in because we need a tag
sed -i /$mac/d $DHCP_HOSTS sed -i /$ipv4/d $DHCP_HOSTS
#put it back with a tag #put it back with a tag
echo "$mac,set:$tag,$ip,$host,infinite" >>$DHCP_HOSTS echo "$mac,set:$tag,$ipv4,$host,infinite" >>$DHCP_HOSTS
fi fi
# make dnsmasq re-read files # make dnsmasq re-read files

View File

@ -1629,20 +1629,20 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
} }
// ssh -p 3922 -o StrictHostKeyChecking=no -i $cert root@$domr "/root/edithosts.sh $mac $ip $vm $dfltrt $ns $staticrt" >/dev/null // ssh -p 3922 -o StrictHostKeyChecking=no -i $cert root@$domr "/root/edithosts.sh $mac $ip $vm $dfltrt $ns $staticrt" >/dev/null
String args = " " + cmd.getVmMac(); String args = " -m " + cmd.getVmMac();
args += " " + cmd.getVmIpAddress(); args += " -4 " + cmd.getVmIpAddress();
args += " " + cmd.getVmName(); args += " -h " + cmd.getVmName();
if (cmd.getDefaultRouter() != null) { if (cmd.getDefaultRouter() != null) {
args += " " + cmd.getDefaultRouter(); args += " -d " + cmd.getDefaultRouter();
} }
if (cmd.getDefaultDns() != null) { if (cmd.getDefaultDns() != null) {
args += " " + cmd.getDefaultDns(); args += " -n " + cmd.getDefaultDns();
} }
if (cmd.getStaticRoutes() != null) { if (cmd.getStaticRoutes() != null) {
args += " " + cmd.getStaticRoutes(); args += " -s " + cmd.getStaticRoutes();
} }
if (s_logger.isDebugEnabled()) { if (s_logger.isDebugEnabled()) {

View File

@ -22,24 +22,12 @@
# @VERSION@ # @VERSION@
usage() { usage() {
printf "Usage: %s: -r <domr-ip> -m <vm mac> -v <vm ip> -n <vm name>\n" $(basename $0) >&2 printf "Usage: %s: -r <domr-ip> -m <vm mac> -v <vm ip> -n <vm name> -s <static route> -d <default router> -N <dns>\n" $(basename $0) >&2
exit 2 exit 2
} }
cert="/root/.ssh/id_rsa.cloud" cert="/root/.ssh/id_rsa.cloud"
add_dhcp_entry() {
local domr=$1
local mac=$2
local ip=$3
local vm=$4
local dfltrt=$5
local ns=$6
local staticrt=$7
ssh -p 3922 -o StrictHostKeyChecking=no -i $cert root@$domr "/root/edithosts.sh $mac $ip $vm $dfltrt $ns $staticrt" >/dev/null
return $?
}
domrIp= domrIp=
vmMac= vmMac=
vmIp= vmIp=
@ -48,29 +36,37 @@ staticrt=
dfltrt= dfltrt=
dns= dns=
opts=
while getopts 'r:m:v:n:d:s:N:' OPTION while getopts 'r:m:v:n:d:s:N:' OPTION
do do
case $OPTION in case $OPTION in
r) domrIp="$OPTARG" r) domrIp="$OPTARG"
;; ;;
v) vmIp="$OPTARG" v) vmIp="$OPTARG"
;; opts="$opts -4 $vmIp"
m) vmMac="$OPTARG" ;;
;; m) vmMac="$OPTARG"
n) vmName="$OPTARG" opts="$opts -m $vmMac"
;; ;;
s) staticrt="$OPTARG" n) vmName="$OPTARG"
;; opts="$opts -h $vmName"
d) dfltrt="$OPTARG" ;;
;; s) staticrt="$OPTARG"
N) dns="$OPTARG" opts="$opts -s $staticrt"
;; ;;
?) usage d) dfltrt="$OPTARG"
exit 1 opts="$opts -d $dfltrt"
;; ;;
N) dns="$OPTARG"
opts="$opts -n $dns"
;;
?) usage
exit 1
;;
esac esac
done done
add_dhcp_entry $domrIp $vmMac $vmIp $vmName $dfltrt $dns $staticrt ssh -p 3922 -o StrictHostKeyChecking=no -i $cert root@$domrIp "/root/edithosts.sh $opts " >/dev/null
exit $? exit $?