#============================================================================# # vHost - one-step solution for all virtual hosting needs # # V01network - network virtualization module # # # # Copyright(c) Chaogic Systems, LLC. http://chaogic.com # # Author: Jake Fan # # # # This is Free Software; permission to use, copy, modify, and distribute # # this software and its documentation for any purpose - with or without fee # # - is hereby granted, provided that the above copyright information and # # this permission notice appear in all copies and documentations. This # # software is provided "as is" without express or implied warranty. For # # more details, see GNU General Public License as published by the Free # # Software Foundation. # # # #============================================================================# $X eq "configuration" && do { ################################################ ### EDITABLE CONFIGURATIONS ################################################## ### network interface card which handles virtual host ip aliasing. when left # blank, it will be reversely calculated using the main host ip, in which # case the main host name needs to resolve to an active ip on the system. # $NIC = ""; $NIC = ""; ### ip alias configuration file or directory. for redhat linux and similar # systems, set this to the network configuration directory which contains # configuration files for all network interfaces. for other systems, set # this to a system startup script which will be used to maintain ip alias # configurations. # $nicCf = "/etc/sysconfig/network-scripts"; #redhat-style # $nicCf = "/etc/rc.local"; #others $nicCf = "/etc/sysconfig/network-scripts"; ### command "ifconfig". # $ifconfig = "/sbin/ifconfig"; $ifconfig = "/sbin/ifconfig"; ### DO NOT TOUCH ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!! ### SanityChecking ########################################################### my $if; my $id = ":" if $UNIX =~ /^Linux$/i; @NIC = `$ifconfig -a $NERR`; /^([\w$id]+)/ && ($if = $1), /^\s+inet\s\D*(\d+\.\d+\.\d+\.\d+)/ && ($NIC{$1} ||= $if) for @NIC; $NIC = $NIC || (split /:/, $NIC{$HIP})[0]; $ERR = 1, Error "$MOD: \$NIC = '$NIC'" if !grep /^\Q$NIC\E(:|$)/, values %NIC; $ERR = 1, Error "$MOD: \$nicCf = '$nicCf'" if !-e $nicCf; $ERR = 1, Error "$MOD: \$ifconfig = '$ifconfig'" if $ifconfig !~ /^\// || !-x (split /[\s|&;]+/, $ifconfig)[0]; @IFCF = Cat "$nicCf/ifcfg-$NIC" if -d $nicCf; }; $X eq "initialization" && do { ############################################### if (-f $nicCf && $ONCFG) { Flock $nicCf, ">>", 2 if !grep /^\s*$FOOT/, Cat $nicCf; BackupCf $nicCf; print FILE -s FILE && "\n" || "", "$FOOT\n"; Flock 0; } }; $X eq "addhost" && do { ###################################################### my ($ok, $id); my $nm = " netmask 0xffffffff" if $FreeBSD; if ($UNIX !~ /^Linux$/i && -f $nicCf && !$NIC{$I}) { Flock $nicCf, "+>>", 2; print FILE "$ifconfig $NIC alias $I$nm\n" if !grep /^\s*\Q$ifconfig\E\s+\S+\s+alias\s+\Q$I\E$nm\s*$/, @FILE; Flock 0; `$ifconfig $NIC alias $I$nm $NERR`; } if ($UNIX =~ /^Linux$/i && -f $nicCf && !$NIC{$I}) { $id = 0; $id++ while grep /^\Q$NIC\E:$id$/, values %NIC; Flock $nicCf, "+>>", 2; print FILE "$ifconfig $NIC:$id $I\n" if !grep /^\s*\Q$ifconfig\E\s+\S+\s+\Q$I\E\s*$/, @FILE; Flock 0; `$ifconfig $NIC:$id $I $NERR`; } if ($UNIX =~ /^Linux$/i && -d $nicCf && !$NIC{$I}) { $ok = $ok || Cat ($_) =~ /^\s*IPADDR[\s"'=]*\Q$I\E[\s"']*$/im, $ok && last for <$nicCf/ifcfg-*>; $id = 0; $id++ while !$ok && -e "$nicCf/ifcfg-$NIC:$id"; Flock "$nicCf/ifcfg-$NIC:$id", ">", 0 if !$ok; s/^(\s*DEVICE[\s"'=]*)[\w:]+/$1$NIC:$id/i, s/^(\s*IPADDR[\s"'=]*)[\d.]+/$1$I/i for @IFCF; print FILE "$FOOT\n"; print FILE @IFCF; Flock 0; `chmod 0755 $nicCf/ifcfg-$NIC:$id $NERR`; `$ifconfig $NIC:$id $I $NERR`; } }; $X eq "delhost" && do { ###################################################### my ($ok, $id); my $nm = " netmask 0xffffffff" if $FreeBSD; if ($UNIX !~ /^Linux$/i && -f $nicCf && !grep CheckQ ($QHOST{$_}) && $_ ne $H && $HOST{$_} eq $HOST{$H}, keys %HOST) { Flock $nicCf, "+<", 0; $ok = $ok || /^\s*$FOOT/, $ok && s/^\s*\Q$ifconfig\E\s+\S+\s+alias\s+\Q$HOST{$H}\E$nm\s*$//s && ($id = 1) for @FILE; print FILE @FILE if $id; Flock 0; `$ifconfig $NIC{$HOST{$H}} -alias $HOST{$H} $NERR` if $id; } if ($UNIX =~ /^Linux$/i && -f $nicCf && !grep CheckQ ($QHOST{$_}) && $_ ne $H && $HOST{$_} eq $HOST{$H}, keys %HOST) { Flock $nicCf, "+<", 0; $ok = $ok || /^\s*$FOOT/, $ok && s/^\s*\Q$ifconfig\E\s+\S+\s+\Q$HOST{$H}\E\s*$//s && ($id = 1) for @FILE; print FILE @FILE if $id; Flock 0; `$ifconfig $NIC{$HOST{$H}} down $NERR` if $id; } if ($UNIX =~ /^Linux$/i && -d $nicCf && !grep CheckQ ($QHOST{$_}) && $_ ne $H && $HOST{$_} eq $HOST{$H}, keys %HOST) { Cat ($_) =~ /^\s*$FOOT.*^\s*IPADDR[\s"'=]*\Q$HOST{$H}\E[\s"']*$/ims && ($id = 1) && `rm $_ $NERR` for <$nicCf/ifcfg-*:*>; `$ifconfig $NIC{$HOST{$H}} down $NERR` if $id; } }; $X eq "finalization" && do { ################################################# }; 1;