removing ifconfig for RHEL7 support

This commit is contained in:
Serghey Rodin 2014-12-12 01:47:56 +02:00
commit 9de2d15c62
5 changed files with 107 additions and 49 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# info: add system ip address # info: add system ip address
# options: IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP] # options: IP NETMASK [INTERFACE] [USER] [STATUS] [NAME] [NAT_IP]
# #
# The function adds ip address into a system. It also creates rc scripts. You # The function adds ip address into a system. It also creates rc scripts. You
# can specify ip name which will be used as root domain for temporary aliases. # can specify ip name which will be used as root domain for temporary aliases.
@ -16,7 +16,7 @@
# Argument defenition # Argument defenition
ip=${1// /} ip=${1// /}
mask=$2 netmask=$2
interface="${3-eth0}" interface="${3-eth0}"
user="${4-admin}" user="${4-admin}"
ip_status="${5-shared}" ip_status="${5-shared}"
@ -34,8 +34,8 @@ source $VESTA/conf/vesta.conf
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
check_args '2' "$#" 'IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]' check_args '2' "$#" 'IP NETMASK [INTERFACE] [USER] [STATUS] [NAME] [NAT_IP]'
validate_format 'ip' 'mask' 'interface' 'user' 'ip_status' validate_format 'ip' 'netmask' 'interface' 'user' 'ip_status'
is_ip_free is_ip_free
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user" is_object_unsuspended 'user' 'USER' "$user"
@ -50,11 +50,18 @@ fi
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
get_ip_iface
sys_ip_check=$(/sbin/ifconfig | grep "addr:$ip ") # Converting netmask to CIDR format and calculating broadcast address
cidr=$(convert_netmask $netmask)
broadcast=$(get_broadcast $ip $netmask)
iface=$(get_ip_iface)
sys_ip_check=$(/sbin/ip addr | grep "$ip/$cidr")
if [ -z "$sys_ip_check" ]; then if [ -z "$sys_ip_check" ]; then
# Adding sys ip
/sbin/ifconfig "$iface" "$ip" netmask "$mask" # Adding system ip
/sbin/ip addr add $ip/$cidr broadcast $broadcast \
dev $interface label $iface
# Adding RHEL/CentOS/Fedora startup script # Adding RHEL/CentOS/Fedora startup script
if [ -e "/etc/redhat-release" ]; then if [ -e "/etc/redhat-release" ]; then
@ -63,7 +70,7 @@ if [ -z "$sys_ip_check" ]; then
sys_ip="$sys_ip\nBOOTPROTO=static" sys_ip="$sys_ip\nBOOTPROTO=static"
sys_ip="$sys_ip\nONBOOT=yes" sys_ip="$sys_ip\nONBOOT=yes"
sys_ip="$sys_ip\nIPADDR=$ip" sys_ip="$sys_ip\nIPADDR=$ip"
sys_ip="$sys_ip\nNETMASK=$mask" sys_ip="$sys_ip\nNETMASK=$netmask"
echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$iface echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$iface
fi fi
@ -73,7 +80,7 @@ if [ -z "$sys_ip_check" ]; then
sys_ip="$sys_ip\nauto $iface" sys_ip="$sys_ip\nauto $iface"
sys_ip="$sys_ip\niface $iface inet static" sys_ip="$sys_ip\niface $iface inet static"
sys_ip="$sys_ip\naddress $ip" sys_ip="$sys_ip\naddress $ip"
sys_ip="$sys_ip\nnetmask $mask" sys_ip="$sys_ip\nnetmask $netmask"
echo -e $sys_ip >> /etc/network/interfaces echo -e $sys_ip >> /etc/network/interfaces
fi fi
fi fi
@ -85,7 +92,7 @@ NAME='$ip_name'
U_SYS_USERS='' U_SYS_USERS=''
U_WEB_DOMAINS='0' U_WEB_DOMAINS='0'
INTERFACE='$interface' INTERFACE='$interface'
NETMASK='$mask' NETMASK='$netmask'
NAT='$nat_ip' NAT='$nat_ip'
TIME='$TIME' TIME='$TIME'
DATE='$DATE'" > $VESTA/data/ips/$ip DATE='$DATE'" > $VESTA/data/ips/$ip

View file

@ -35,33 +35,40 @@ is_ip_key_empty '$U_SYS_USERS'
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Get ip owner # Import ip variables
user="$(get_ip_value '$OWNER')" source $VESTA/data/ips/$ip
ip_status="$(get_ip_value '$STATUS')" cidr=$(convert_netmask $NETMASK)
# Deleting interface # Checking main ip on the interface
interface=$(/sbin/ifconfig | grep -B1 "dr:$ip " | head -n1 | cut -f1 -d \ ) interface=$(/sbin/ip addr | grep "$ip/$cidr" | awk '{print $NF}')
if [ ! -z "$interface" ] && [ -z "$(echo $interface |cut -s -f2 -d :)" ]; then if [ ! -z "$interface" ] && [ -z "$(echo $interface |cut -s -f2 -d :)" ]; then
echo "Error: can't delete main IP address" echo "Error: can't delete main IP address"
log_event "$E_FORBIDEN" "$EVENT" log_event "$E_FORBIDEN" "$EVENT"
exit $E_FORBIDEN exit $E_FORBIDEN
fi fi
# Deleting system ip
if [ ! -z "$interface" ]; then if [ ! -z "$interface" ]; then
/sbin/ifconfig $interface down /sbin/ip addr del $ip/$cidr dev $INTERFACE
if [ "$?" -ne 0 ]; then
# Deleting startup conf on RHEL/CentOS/Fedora echo "Error: can't delete system ip"
if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then log_event "$E_FORBIDEN" "$EVENT"
rm -f /etc/sysconfig/network-scripts/ifcfg-$interface exit $E_FORBIDEN
fi fi
fi
# Deleting startup conf on Debian/Ubuntu # Deleting startup conf on RHEL/CentOS/Fedora
if [ -e "/etc/network/interfaces" ]; then if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:) rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
if [ ! -z "$ip_str" ]; then fi
first_str=$((ip_str - 3))
last_str=$((ip_str + 1)) # Deleting startup conf on Debian/Ubuntu
sed -i "$first_str,$last_str d" /etc/network/interfaces if [ -e "/etc/network/interfaces" ]; then
fi ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:)
if [ ! -z "$ip_str" ]; then
first_str=$((ip_str - 3))
last_str=$((ip_str + 1))
sed -i "$first_str,$last_str d" /etc/network/interfaces
fi fi
fi fi
@ -92,7 +99,6 @@ if [ ! -z "$PROXY_SYSTEM" ]; then
new_ips=$(echo "$rpaf_str" | sed "s/$ip//") new_ips=$(echo "$rpaf_str" | sed "s/$ip//")
sed -i "s/$ips/$new_ips/g" $rpaf_conf sed -i "s/$ips/$new_ips/g" $rpaf_conf
fi fi
fi fi
@ -101,24 +107,20 @@ fi
#----------------------------------------------------------# #----------------------------------------------------------#
# Updating user conf # Updating user conf
if [ ! -z "$user" ]; then if [ ! -z "$OWNER" ]; then
decrease_user_value "$user" '$IP_OWNED' decrease_user_value "$OWNER" '$IP_OWNED'
fi fi
if [ "$user" = 'admin' ]; then if [ "$OWNER" = 'admin' ]; then
if [ "$ip_status" = 'shared' ]; then if [ "$STATUS" = 'shared' ]; then
for user in $(ls $VESTA/data/users/); do for user in $(ls $VESTA/data/users/); do
decrease_user_value "$user" '$IP_AVAIL' decrease_user_value "$user" '$IP_AVAIL'
done done
else
decrease_user_value 'admin' '$IP_AVAIL'
fi fi
else else
decrease_user_value "$user" '$IP_AVAIL' decrease_user_value "$OWNER" '$IP_AVAIL'
decrease_user_value 'admin' '$IP_AVAIL'
fi fi
# Adding task to the vesta pipe # Adding task to the vesta pipe
$BIN/v-restart-web $BIN/v-restart-web
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then

View file

@ -18,6 +18,7 @@ ip_status=${2-shared}
# Includes # Includes
source /etc/profile.d/vesta.sh source /etc/profile.d/vesta.sh
source $VESTA/func/main.sh source $VESTA/func/main.sh
source $VESTA/func/ip.sh
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
@ -35,8 +36,8 @@ is_object_valid 'user' 'USER' "$user" "$user"
#----------------------------------------------------------# #----------------------------------------------------------#
# Get list of ip addresses # Get list of ip addresses
ip_list=$(/sbin/ifconfig | grep 'inet addr:' | cut -f 2 -d : | \ ip_list=$(/sbin/ip addr |grep "inet "|grep -v "host lo" |awk '{print $2}')
cut -f 1 -d ' '| grep -v 127.0.0.1 | grep -v "^0.0.0.") ip_list=$(echo "$ip_list"|cut -f 1 -d /)
ip_num=$(echo "$ip_list" | wc -l) ip_num=$(echo "$ip_list" | wc -l)
# WorkAround for DHCP IP address # WorkAround for DHCP IP address
@ -88,10 +89,11 @@ fi
# Compare ips # Compare ips
for ip in $ip_list; do for ip in $ip_list; do
if [ ! -e "$VESTA/data/ips/$ip" ]; then if [ ! -e "$VESTA/data/ips/$ip" ]; then
iface=$(/sbin/ifconfig |grep -B1 -w $ip |head -n1 |cut -f1 -d ' ') interface=$(/sbin/ip addr |grep $ip |awk '{print $NF}')
interface=$(echo "$iface" | cut -f 1 -d :) interface=$(echo $interface |cut -f 1 -d :)
mask=$(/sbin/ifconfig |grep -w $ip |awk -F "Mask:" '{print $2}') netmask=$(/sbin/ip addr |grep $ip |awk '{print $2}' |cut -f 2 -d /)
$BIN/v-add-sys-ip $ip $mask $interface netmask=$(convert_cidr $netmask)
$BIN/v-add-sys-ip $ip $netmask $interface
fi fi
done done

View file

@ -54,14 +54,14 @@ is_ip_free() {
# Get full interface name # Get full interface name
get_ip_iface() { get_ip_iface() {
i=$(/sbin/ifconfig -a |grep -w "$interface"|cut -f1 -d ' '|\ i=$(/sbin/ip addr | grep -w $interface |\
tail -n 1|cut -f 2 -d :) awk '{print $NF}' | tail -n 1 | cut -f 2 -d :)
if [ "$i" = "$interface" ]; then if [ "$i" = "$interface" ]; then
n=0 n=0
else else
n=$((i + 1)) n=$((i + 1))
fi fi
iface="$interface:$n" echo "$interface:$n"
} }
@ -201,3 +201,50 @@ get_user_ip(){
fi fi
echo "$ip" echo "$ip"
} }
# Convert CIDR to netmask
convert_cidr() {
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 \
$(((255 << (8 - ($1 % 8))) & 255 )) 0 0 0
if [[ $1 -gt 1 ]]; then
shift $1
else
shift
fi
echo ${1-0}.${2-0}.${3-0}.${4-0}
}
# Convert netmask to CIDR
convert_netmask() {
nbits=0
IFS=.
for dec in $1 ; do
case $dec in
255) let nbits+=8;;
254) let nbits+=7;;
252) let nbits+=6;;
248) let nbits+=5;;
240) let nbits+=4;;
224) let nbits+=3;;
192) let nbits+=2;;
128) let nbits+=1;;
0);;
esac
done
echo "$nbits"
}
# Calculate broadcast address
get_broadcast() {
OLD_IFS=$IFS
IFS=.
typeset -a I=($1)
typeset -a N=($2)
IFS=$OLD_IFS
echo "$((${I[0]} |\
(255 ^ ${N[0]}))).$((${I[1]} |\
(255 ^ ${N[1]}))).$((${I[2]} |\
(255 ^ ${N[2]}))).$((${I[3]} |\
(255 ^ ${N[3]})))"
}

View file

@ -905,11 +905,11 @@ validate_format(){
key) validate_format_username "$arg" "$arg_name" ;; key) validate_format_username "$arg" "$arg_name" ;;
lname) validate_format_name_s "$arg" "$arg_name" ;; lname) validate_format_name_s "$arg" "$arg_name" ;;
malias) validate_format_username "$arg" "$arg_name" ;; malias) validate_format_username "$arg" "$arg_name" ;;
mask) validate_format_ip "$arg" ;;
max_db) validate_format_int "$arg" 'max db';; max_db) validate_format_int "$arg" 'max db';;
min) validate_format_mhdmw "$arg" $arg_name ;; min) validate_format_mhdmw "$arg" $arg_name ;;
month) validate_format_mhdmw "$arg" $arg_name ;; month) validate_format_mhdmw "$arg" $arg_name ;;
nat_ip) validate_format_ip "$arg" ;; nat_ip) validate_format_ip "$arg" ;;
netmask) validate_format_ip "$arg" ;;
newid) validate_format_int "$arg" 'id' ;; newid) validate_format_int "$arg" 'id' ;;
ns1) validate_format_domain "$arg" 'name_server';; ns1) validate_format_domain "$arg" 'name_server';;
ns2) validate_format_domain "$arg" 'name_server';; ns2) validate_format_domain "$arg" 'name_server';;