Dual NIC clients
-
@Wayne-Workman
Thanks again.Yes, that code will only work on the specific network defined in $nwid and if the kernel names the interfaces ethX and probably only if the number of interfaces match that particular piece of hw…
Mighty nice of you to help me out here… Appreciate it.
Thanks.
-
@tag Well the way I write it, it’ll work with however many NICs a system has. We will need to use the new feature that @Tom-Elliott so kindly implemented maybe a month ago, the host’s
Host Init
field. Basically we will build an init for each of your subnets, then use groups to assign the right inits to the right computers - so that those computers in those subnets use the correct interface.Sounds like a lot - but I really don’t think it is. I think this is going to be very easy.
-
I’ve been working on this.
This is my first go-round with a custom init so I’m asking that @Sebastian-Roth and @Tom-Elliott and @george1421 to take a look, too.
I’ve not tested, as I don’t readily have available a machine with multiple interfaces, but I think I’ve got a universal init that you can pass a custom kernel argument to, which will ensure the correct interface is up, and others are down. So far, I’ve coded for three possible interfaces. I already had many of these functions already written in another project I’ve been working on.
in the init, I’ve edited the file
/usr/share/fog/lib/funcs.sh
to include these functions:cidr2mask() { #Expects CIDR notation (a single integer between 0 and 32) local i="" local mask="" local full_octets=$(($1/8)) local partial_octet=$(($1%8)) for ((i=0;i<4;i+=1)); do if [[ $i -lt $full_octets ]]; then mask+=255 elif [[ $i -eq $full_octets ]]; then mask+=$((256 - 2**(8-$partial_octet))) else mask+=0 fi test $i -lt 3 && mask+=. done echo $mask } getCidr() { #Expects an interface name to be passed. local cidr cidr=$(ip -f inet -o addr | grep $1 | awk -F'[ /]+' '/global/ {print $5}' | head -n2 | tail -n1) echo $cidr } mask2network() { #Expects IP address passed 1st, and Subnet Mask passed 2nd. OIFS=$IFS IFS='.' read -r i1 i2 i3 i4 <<< "$1" read -r m1 m2 m3 m4 <<< "$2" IFS=$OIFS printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))" } GetInterfaceInfo() { DIR="/" ip link show > $DIR/interfaces.txt interface1name="$(sed -n '3p' $DIR/interfaces.txt)" interface2name="$(sed -n '5p' $DIR/interfaces.txt)" interface3name="$(sed -n '7p' $DIR/interfaces.txt)" rm -f $DIR/interfaces.txt echo $interface1name | cut -d \: -f2 | cut -c2- > $DIR/interface1name.txt echo $interface2name | cut -d \: -f2 | cut -c2- > $DIR/interface2name.txt echo $interface3name | cut -d \: -f2 | cut -c2- > $DIR/interface3name.txt interface1name="$(cat $DIR/interface1name.txt)" interface2name="$(cat $DIR/interface2name.txt)" interface2name="$(cat $DIR/interface2name.txt)" rm -f $DIR/interface1name.txt rm -f $DIR/interface2name.txt rm -f $DIR/interface3name.txt #Bring up interfaces. echo “iface $interface1name inet dhcp” >>/etc/network/interfaces echo “iface $interface2name inet dhcp” >>/etc/network/interfaces echo “iface $interface3name inet dhcp” >>/etc/network/interfaces ip link set $interface1name up ip link set $interface2name up ip link set $interface3name up sleep 4 interface1ip="$(/sbin/ip addr show | grep $interface1name | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")" interface2ip="$(/sbin/ip addr show | grep $interface2name | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")" interface3ip="$(/sbin/ip addr show | grep $interface3name | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")" if [[ -z $interface1ip ]]; then interface1ip=127.0.0.1 fi if [[ -z $interface2ip ]]; then interface2ip=127.0.0.1 fi if [[ -z $interface3ip ]]; then interface3ip=127.0.0.1 fi interface1network=$(mask2network $interface1ip $(cidr2mask $(getCidr $interface1name))) interface2network=$(mask2network $interface2ip $(cidr2mask $(getCidr $interface2name))) interface3network=$(mask2network $interface3ip $(cidr2mask $(getCidr $interface3name))) } setCorrectInterface() { for arg in $(cat /proc/cmdline) do echo $arg | grep -q USE_NETWORK if [ $? == 0 ] then val=$(echo $arg | cut -d= -f2) desiredNetwork=$val fi done GetInterfaceInfo if [[ $interface1network==$desiredNetwork ]] then ip link set $interface1name up ip link set $interface2name down ip link set $interface3name down elif [[ $interface2network==$desiredNetwork ]] then ip link set $interface1name down ip link set $interface2name up ip link set $interface3name down else [[ $interface3network==$desiredNetwork ]] then ip link set $interface1name down ip link set $interface2name down ip link set $interface3name up fi }
And I’ve added a call to the main function in the main fog script file
/bin/fog
between the usb part and the task calling, around line 10 like this:#!/bin/bash . /usr/share/fog/lib/funcs.sh ### If USB Boot device we need a way to get the kernel args properly if [[ $boottype == usb && ! -z $web ]]; then mac=$(getMACAddresses) wget -q -O /tmp/hinfo.txt "http://${web}service/hostinfo.php?mac=$mac" [[ -f /tmp/hinfo.txt ]] && . /tmp/hinfo.txt fi setCorrectInterface if [[ -n $mode && $mode != +(*debug*) ]]; then case $mode in wipe) fog.wipe ;; checkdisk) fog.testdisk ;; photorec) fog.photorec ;; badblocks) fog.surfacetest ;; clamav) fog.av ;; autoreg) fog.auto.reg ;; manreg) fog.man.reg ;; inventory) fog.inventory ;; capone) fog.capone ;; winpassreset) fog.chntpw ;; quickimage) fog.quickimage ;; sysinfo) fog.sysinfo ;; "donate.full") fog.donatefull ;; *) handleError "Fatal Error: Unknown mode :: $mode ($0)\n Args Passed: $*" ;; esac else case $type in down) fog.download ;; up) fog.upload ;; *) [[ -z $type ]] && type="Null" handleError "Fatal Error: Unknown request type :: $type" ;; esac fi
With modifications to the init like this (and using fog trunk), You’d simply specify this custom init, and pass the kernel argument for the network you want to use. For example:
-
That would work nicely, seeing you can use different inits. I didn’t know that as it is not possible in 1.2.0.
The caveat is that I would have to redo the server as trunk requires a newer Ubuntu according to @Quazz.
-
@tag correct. No getting around that.