Skip to content

Instantly share code, notes, and snippets.

@upa
Last active April 27, 2017 12:05
Show Gist options
  • Save upa/e5ee89cff9229bb3c6a5daed6b58e3f8 to your computer and use it in GitHub Desktop.
Save upa/e5ee89cff9229bb3c6a5daed6b58e3f8 to your computer and use it in GitHub Desktop.
A Login Shell (sciprt) to enable users to select a network namespace where the users want to login
#!/bin/bash
ip=/bin/ip
ifc=/sbin/ifconfig
intf=$1
configdir=ns-configs
if [ "$intf" == "" ]; then
echo $0 [raw interface]
exit 1
fi
if [ ! -e /sys/class/net/$intf ]; then
echo interface $intf does not exist
exit 1
fi
echo detroy all existing netns
for netns in `$ip netns list`; do
$ip netns del $netns
done
for x in `seq 3`; do echo -n .; sleep 1; done
echo
# create netns
for config in `ls $configdir`; do
source $configdir/$config
vlan_intf=$intf.$VLANID
netns=$NAME
ipns="$ip netns exec $netns "
echo create netns \"$netns\"
$ip netns add $netns
$ip link add link $intf name $vlan_intf type vlan id $VLANID
$ip link set dev $vlan_intf netns $netns
$ipns $ip link set dev $vlan_intf up
$ipns $ip link set dev lo up
$ipns $ifc $vlan_intf $ADDRESS
$ipns $ip route add to default via $GATEWAY
done
#!/bin/bash
ip=/bin/ip
netnslist=`ls /var/run/netns`
echo
echo
echo "Select routing instance you want to Login:"
echo
printf " = No. =\t= Instance Name =\n"
printf " %4d\t%s\n" 0 default
cnt=1
for netns in $netnslist; do
printf " %4d\t%s\n" $cnt $netns
cnt=$(( cnt + 1))
done
echo
echo -n "type a number of a routing instance : "
matched=0
while read ri_num; do
if [ "$ri_num" == "0" ]; then
# default is selected
matched=1
netns=default
break
fi
cnt=1
for netns in $netnslist; do
if [ "$cnt" == "$ri_num" ]; then
matched=1
break
fi
cnt=$(( cnt + 1))
done
if [ $matched -eq 1 ]; then
break
fi
echo
echo routing instance No.$ri_num does not exist.
echo
echo -n "type a number of a routing instance : "
done
if [ $matched -ne 1 ]; then
echo
exit 0
fi
echo
echo Login to routing instance \"$netns\"
echo
if [ $ri_num -eq 0 ]; then
# default is selected
/bin/bash
else
# login into selected netns
sudo $ip netns exec $netns /bin/bash
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment