Files
new_install/operation/configure_network.sh
lizhi-rocky 220e6bfe40 add Qin Bo's operating shell scripts in
rename directory 'python' to 'interface', for I think it is a better name for its included python scripts function.
2010-09-03 15:59:49 +08:00

256 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
#
# DESCRIPTION: configure network services
#
# SCRIPT NAME: configure_network.sh
#
# Input: argument
# [--help] show usage
# [-t|--type] \"manual\" or \"dynamic\"
# manual argument:
# [-h|--hostname] hostname
# [-d|--domainname] domainname
# [-i|--ip] ip address
# [-n|--netmask] netmask
# [-g|--gateway] gateway
# [-p|--pdns] priamaryDNS
# [-s|--sdns] secondaryDNS
# <-e|--device> network device. if no configure, default is eth0.
# dynamic argument:
# [-h|--hostname] hostname
# <-e|--device> network device. if no configure, default is eth0.
#
# Output:
# 1 argument error
# 2 ip/netmask/gateway address incorrect
#
# AUTHOR: Qin Bo
#
# EMAIL: bqin@linx-info.com
#
# DATE: 2010-08-23
#
# HISTORY:
# REVISOR DATE MODIFICATION
# Qin Bo 2010-08-23 create
#
#
#
source ./functions
usage ()
{
if [ "$1" == "-n" ];then
cmd="info"
ret=0
else
cmd="err"
ret=1
fi
$cmd "
This script will configure your network
Usage: $0 options:
options:
[--help] show this message
[-t|--type] \"manual\" or \"dynamic\"
manual argument:
[-h|--hostname] hostname
[-d|--domainname] domainname
[-i|--ip] ip address
[-n|--netmask] netmask
[-g|--gateway] gateway
[-p|--pdns] priamaryDNS
[-s|--sdns] secondaryDNS
<-e|--device> network device. if no configure, default is eth0.
dynamic argument:
[-h|--hostname] hostname
<-e|--device> network device. if no configure, default is eth0.
"
return $ret
}
check_ip ()
{
if [ $# -ne 1 ];then
err "$FUNCNAME function argument error, it must be 1"
return 1;
fi
local ip=$1
local ret=2
# "=~" use for match regular expression, only use in bash 3.0 or higher
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
# IFS is a separator for read builtin command, we save old IFS
OIFS=$IFS
IFS='.'
# set $ip to ip array
ip=($ip)
# restore IFS
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
ret=$?
fi
return $ret
}
parse_arg ()
{
tmp=$(getopt -o t:h:d:i:n:g:p:s:e:? --long type:,hostname:,ip:,netmask:,gateway:,pdns:,sdns:,device:,help -- "$@" 2>$dev_LOG)
if [ $? -ne 0 ];then
usage
return 1
fi
# set all argument to arg variable
eval set -- "$tmp"
while true ; do
case $1 in
-t|--type) type="$2" ;
info "network configure type: $type" ;
shift 2;;
-h|--hostname) hostname="$2";
info "hostname: $hostname" ;
shift 2 ;;
-d|--domainname) domainname="$2";
info "domainname: $domainname" ;
shift 2 ;;
-i|--ip) ip="$2";
info "ip: $ip" ;
shift 2 ;;
-n|--netmask) netmask="$2";
info "netmask: $netmask" ;
shift 2 ;;
-g|--gateway) gateway="$2";
info "gateway: $gateway" ;
shift 2 ;;
-p|--pdns) primaryDNS="$2";
info "primaryDNS: $primaryDNS" ;
shift 2 ;;
-s|--sdns) secondaryDNS="$2";
info "secondaryDNS: $secondaryDNS" ;
shift 2 ;;
# may be have mulit-network devices
-e|--device) device="$2";
info "device: $device" ;
shift 2 ;;
# shift the last "--", this dash is define by getopt (more information see man getopt)
--) shift ;break;;
--help) usage -n ;return 0;;
*) usage ;return 1;;
esac
done
if [ "$type" != "manual" -a "$type" != "dynamic" ];then
usage
return 1
fi
if [ -z "$hostname" ];then
err "you must specify hostname"
return 1
fi
# default device is eth0
if [ -z "$device" ];then
device="eth0"
fi
if [ "$type" == "manual" ];then
if [ -z "$ip" ];then
err "manual configure network, IP address can't be empty"
return 1
else
info "checking ip: $ip"
if ! check_ip "$ip"; then
return 2
fi
fi
if [ -z "$netmask" ];then
err "manual configure network, netmask address can't be empty"
return 1
else
info "checking netmask: $netmask"
if ! check_ip "$netmask"; then
return 2
fi
fi
if [ -z "$gateway" ];then
err "manual configure network, gateway address can't be empty"
return 1
else
info "checking gateway: $gateway"
if ! check_ip "$gateway"; then
return 2
fi
fi
fi
}
configure_network ()
{
if [ "$type" == "manual" ];then
# configure /etc/hosts
printf "%s %s.%s %s\n" $ip $hostname $domainname $hostname >>"$TARGET/etc/hosts"
# configure /etc/resolv.conf
printf "search %s\n" $domainname >>"$TARGET/etc/resolv.conf"
if [ -n "$primaryDNS" ];then
printf "nameserver %s\n" $primaryDNS >>"$TARGET/etc/resolv.conf"
fi
if [ -n "$secondaryDNS" ];then
printf "nameserver %s\n" $secondaryDNS >> "$TARGET/etc/resolv.conf"
fi
printf "domainname %s\n" $domainname >> "$TARGET/etc/resolv.conf"
# configure /etc/sysconfig/network-devices/ifcfg-eth0
broadcast=$(./ipcalc.pl -n $ip/$netmask| grep Broadcast|awk '{ print $2 }')
sed -i '/IPADDR/ c\IPADDR='$ip'
/NETMASK/ c\NETMASK='$netmask'
/DEVICE/ c\DEVICE='$device'
/GATEWAY/ c\GATEWAY='$gateway'
/BROADCAST/ c\BROADCAST='$broadcast'' "$TARGET/etc/sysconfig/network-devices/ifcfg-eth0"
sed -i '/HOSTNAME/ c\HOSTNAME='$hostname'' "$TARGET/etc/sysconfig/network"
else [ "$type" == "dynamic" ]
printf "%s\n" $hostname >> "$TARGET/etc/hosts"
sed -i '/BOOTPROTO/ c\BOOTPROTO=dhcp
/DEVICE/ c\DEVICE='$device'
/SERVICE/ c\SERVICE=dhcpcd
/IPADDR/ c\IPADDR=
/NETMASK/ c\NETMASK=
/GATEWAY/ c\GATEWAY=
/BROADCAST/ c\BROADCAST=' "$TARGET/etc/sysconfig/network-devices/ifcfg-eth0"
sed -i '/HOSTNAME/ c\HOSTNAME='$hostname'' "$TARGET/etc/sysconfig/network"
fi
if [ "$device" != "eth0" ];then
mv "$TARGET/etc/sysconfig/network-devices/ifcfg-eth0" "$TARGET/etc/sysconfig/network-devices/ifcfg-$device"
fi
}
main ()
{
parse_arg $@
erv
configure_network
}
main $*