#!/bin/bash guess_localnet () { local found=0 for net in $( ip add show | grep 'inet ' | awk '{print $2}' ); do [[ "$net" =~ ^10\. ]] && found=1 && break [[ "$net" =~ ^192\.168\. ]] && found=1 && break [[ "$net" =~ ^172\.(1[6-9]|2[0-9]|3[12])\. ]] && found=1 && break done [ $found -eq 1 ] && echo $net } guess_wanip () { for net in $( ip add show | grep 'inet ' | awk '{print $2}' ); do [[ "$net" =~ ^127\.0\.0\.1 ]] && continue [[ "$net" =~ ^10\. ]] && continue [[ "$net" =~ ^192\.168\. ]] && continue [[ "$net" =~ ^172\.(1[6-9]|2[0-9]|3[12])\. ]] && continue break done echo ${net%/*} } guess_start_ip () { local netip=$1 # TODO: see below echo "${netip%.*}.100" } get_ip_by_count () { local startip=$1 local count=$2 # TODO: adds support for classless addresses # Now assums that local network is /24 network for simplicity local ipnet=${startip%.*} local iphost=${startip##*.} echo "$ipnet.$((iphost+count))" } gen_dhcpd_conf () { local startip=$1 local vmcount=$2 echo "group rainbow {" for i in $( seq 0 $vmcount ); do i2=$( printf '%02d\n' "$i" ) echo -e "\thost rainbow$i { hardware ethernet 52:54:00:c1:0d:$i2; fixed-address $( get_ip_by_count $startip $i ); }" if [ -n "$create_statedir" ]; then mkdir -p "$MAC_STATE_DIR/52:54:00:c1:0d:$i2" fi done echo "}" } gen_iptables_config_rdp () { local startip=$1 local startport=$2 local vmcount=$3 local wanip=$4 for i in $( seq 0 $vmcount ); do i2=$( printf '%02d\n' "$i" ) vmip=$( get_ip_by_count $startip $i ) vmport=$(( startport + i )) echo -e "-A PREROUTING -d $wanip -m tcp -p tcp --dport $vmport\t-j DNAT --to-destination $vmip:3389" if [ -n "$create_statedir" ]; then echo "$vmip" > "$MAC_STATE_DIR/52:54:00:c1:0d:$i2/ip" echo "$wanip:$vmport" > "$MAC_STATE_DIR/52:54:00:c1:0d:$i2/rdp" fi done } gen_iptables_config_ssh () { local startip=$1 local startport=$2 local vmcount=$3 local wanip=$4 for i in $( seq 0 $vmcount ); do i2=$( printf '%02d\n' "$i" ) vmip=$( get_ip_by_count $startip $i ) vmport=$(( startport + i )) echo -e "-A PREROUTING -d $wanip -m tcp -p tcp --dport $vmport\t-j DNAT --to-destination $vmip:22" if [ -n "$create_statedir" ]; then echo "$wanip:$vmport" > "$MAC_STATE_DIR/52:54:00:c1:0d:$i2/ssh" fi done } # common environment [ -f /etc/sysconfig/rainbow-nethelper ] && . /etc/sysconfig/rainbow-nethelper export MAC_STATE_DIR=${MAC_STATE_DIR:-/var/spool/rainbow-nethelper} # main input processing cycle g_localnet=$( guess_localnet ) g_wanip=$( guess_wanip ) save_dhcpd=/tmp/rainbow-nethelper.dhpcd save_iptables_rdp=/tmp/rainbow-nethelper.iptables-rdp save_iptables_ssh=/tmp/rainbow-nethelper.iptables-ssh echo "Welcome to Rainbow Networking Helper configuration wizard!" echo -n "Enter WAN IP address [$g_wanip]: " read wanip [ -z "$wanip" ] && wanip=$g_wanip echo -n "Specify cluster LAN network address [$g_localnet]: " read localnet [ -z "$localnet" ] && localnet=$g_localnet g_startip=$( guess_start_ip $localnet ) echo -n "Please define a start IP address for the Rainbow dynamic pool [$g_startip]: " read startip [ -z "$startip" ] && startip=$g_startip echo -n "What will be the size of Rainbow dynamic pool (max number of VMs) [100]: " read poolsize [ -z "$poolsize" ] && poolsize=100 poolsize=$(( poolsize - 1 )) echo -n "Please define a start port number to be used for RPD forwarding [33800]: " read rdpsport [ -z "$rdpsport" ] && rdpsport=33800 echo -n "Please define a start port number to be used for SSH forwarding [22200]: " read sshsport [ -z "$sshsport" ] && sshsport=22200 echo -n "Would you like to create Rainbow Networking Helper statedir (any previous statedir content will be overwritten) [Y/n]: " read statedir if [ "$statedir" = "Y" -o -z "$statedir" ]; then rm -rf $MAC_STATE_DIR export create_statedir=1 else cat <