Есть задача подцепить на маршрутзатор два (а может и более) модемов samsung swc-200 usb. При этом 1 - yota, второй - comstar. Получилась примерно такая обёртка для запуска из udev:
#!/bin/sh
. /usr/local/etc/wimax_connect
echo $lock
#if there is lock file wait until it will be released
while true;
do
if [ ! -f $lock ]
then
break
fi
sleep 10
done
#set lock file
touch $lock
echo lock set
#commandline arguments from udev
#bus number of new modem
busnum=$1
#device number of new modem
devnum=$2
#echo $busnum $devnum > /tmp/wimax.debug
#save current main routing table
ip route show | sed 's/.*/ip route add & table $bkp_rt/' | sh
echo rt backuped
#get new modem's hardware address
/sbin/madwimax -v -l $if_info --exact-device=$busnum/$devnum &
mw_pid=$!
sleep 10
kill $mw_pid
echo modem info wrote
mac=$(grep MAC: $if_info | head -n1 | sed 's/^[^ ]* //')
rm -f $if_info
echo -n $mac > $lock
echo modem\'s mac - $mac
#get commandlines from modem_db
cmdline="$(grep $mac $modem_db | head -n1 | sed 's/^[a-fA-F0-9:]* table[0-9]* //')"
echo $cmdline
rt_table="$(grep $mac $modem_db | head -n1 | sed 's/^[^ ]* \(table[0-9]*\) .*$/\1/')"
echo $rt_table
#append current device bus/dev numbers to commandline
cmdline="$cmdline --exact-device=$busnum/$devnum"
echo $cmdline
#clear old records about new modem if exists
[ -f $modem_cur ] && grep -v $mac $modem_cur > $modem_cur.new
echo $mac $rt_table $cmdline
echo $mac $rt_table $cmdline >> $modem_cur.new
mv $modem_cur.new $modem_cur
#clear main routing table before start madwimax
ip route flush table main
#start madwimax with $cmdline and save pid
echo "nohup madwimax $cmdline &"
sleep 5
nohup madwimax $cmdline 2>&1 > /dev/null &
echo $! > "$pid"mw_"$rt_table".pid
#after start madwimax dhclient-exit-hook will handle recived by dhcp routes
dhcp-enter-hook:
#this hook saves original main routing table and sets lock if not set
source /usr/local/etc/wimax.connect
export mac=$(ip link show | grep $interface -A1 | tail -n1 | \
sed 's/^.*ether \(.*\) brd.*/\1/')
echo $mac - entered dhclient enter hook >> /tmp/dhcp-enter.debug
function mw_set_lock() {
echo $mac > $lock
ip route flush table $bkp_rt
ip route show | sed 's/.*/ip route add & table $bkp_rt/' | sh
ip route flush table main
}
if grep -q $mac $modem_db;
then
if [ x$reason=="xBOUND" -o x$reason=="xRENEW" -o x$reason=="xREBIND" \
-o x$reason=="xREBOOT" ]
then
if [ ! -f $lock ]
then
mw_set_lock
else
echo entering lock waiting loop >> /tmp/dhcp-enter.debug
echo $mac >> /tmp/dhcp-enter.debug
while [ -z $(cat $lock) -o $(cat $lock) != $mac ]
do
echo . $(cat $lock) >> /tmp/dhcp-enter.debug
sleep 10
done
mw_set_lock
echo exiting lock waiting loop >> /tmp/dhcp-enter.debug
fi
fi
fi
echo $mac - exited dhclient enter hook >> /tmp/dhcp-enter.debug
#this hook handles recived by dhcp routes and removes wimax_connect lock file
source /usr/local/etc/wimax.connect
#get mac address of interface
export mac=$(ip link show | grep $interface -A1 | tail -n1 | \
sed 's/^.*ether \(.*\) brd.*/\1/')
echo $mac - entered dhclient exit hook >> /tmp/dhclient-exit.debug
#if interface is modem listed in modem_db - move routing tables
if grep -q $mac $modem_db;
then
#get name of routing table for the modem
export $rt_table=$(grep $mac $modem_db | head -n1 | \
sed 's/^[^ ]* \(table[0-9]*\) .*$/\1/')
export $mark=$(echo $rt_table | sed 's/table\([0-9]*\)/\1/')
echo $mark, $rt_table exit hook >> /tmp/dhclient-exit.debug
if [ x$reason=="xBOUND" -o x$reason=="xRENEW" -o x$reason=="xREBIND" \
-o x$reason=="xREBOOT" ]
then
#copy dhcp routes to modem's routing table
ip route flush table $rt_table
ip route show | sed 's/.*/ip route add & table $rt_table/' | sh
#restore main routing table
ip route flush table main
ip route show table $bkp_rt | sed 's/.*/ip route add &/' | sh
ip route flush table $bkp_rt
#set default routing rules if not set
ip rule show | grep -q "fwmark 0x$mark lookup $rt_table" || \
ip rule add fwmark $mark table $rt_table
/usr/local/bin/monitor.sh $interface $mac $table $mark
rm -f $lock
fi
fi