1,760
edits
Changes
→Shell Script for adding custom routes on your gateway to other's VLSs
* ip route show
== Manually Configure routes on each VM in each virtual network VLS ==You only need to create the custom routes on the VM in your VLS, but make sure that other students have proper routes on their VMs as well.=== VLS 1 VM2 to VM4 ===On Virtual Network VLS 1's VM (192.168.1.2 .. 192.168.1.2544)* route add -net nmcli con modify ens224 +ipv4.routes "192.168.2.0 netmask 255.255.255.0 gw /16 192.168.1.1100"* nmcli con down ens224* nmcli con up ens224* ip route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.1show
=== VLS 2 VM2 to VM4 ===On Virtual Network VLS 2's VM (192.168.2.2 .. 192.168.12.2544)* route add -net nmcli con modify ens224 +ipv4.routes "192.168.1.0 netmask 255.255.255.0 gw /16 192.168.2.1100"* nmcli con down ens224* nmcli con up ens224* ip route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.2.1show
=== VLS 3 VM2 to VM4 ===On Virtual Network VLS 3's VM (192.168.3.2 .. 192.168.3.2544)* route add -net nmcli con modify ens224 +ipv4.routes "192.168.1.0 netmask 255.255.255.0 gw /16 192.168.3.1100"* nmcli con down ens224* nmcli con up ens224* ip route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.3.1show
= Automate the process using BASH script =
The above example shows that 12 route commands will be 2 custom routes on each VLS are needed to create provides the necessary routes just for 3 virtual networks, each with a proper routing to reach 2 other VLS on the your gateway and one VM(VM1). It will be difficult quite time consuming to manage all the routes if the number of virtual networks VLS is much greater(how about 40 VLSs?). The following script will be more practical if your need to set up all the necessary routes for a large number of virtual networksVLSs. Both script scripts use the shell environment variables "MY_NETWORK_NOMY_VLS_NO" and "OTHER_NETWORKSOTHER_VLS_NOS" to allow customization for different values of private network addressVLSs.
== Shell Script for adding the proper custom routes on your gateway to other's private network VLSs ==* set the shell variable MY_NETWORK_NO MY_VLS_NO to your assigned network ID (we I use 99 2 here is as an example, please change 99 2 to your assigned network ID)
<pre>
export MY_NETWORK_NOMY_VLS_NO=992
</pre>
* set edit the shell variable OTHER_NETWORKS OTHER_VLS_NOS to the list of private network VLS you want to create have a routeto their VLS network. The following example assign the list of network numbers 32, 33, 34, 35, 40, 41, and 99 1 to 43 that you want to create a custom route on your gateway:.
<pre>
</pre>
The following Bash script to add 7 42 new route custom routes to your gateway hostVM's routing table:
<pre>
#! /bin/bash
if [ -z "$MY_NETWORK_NOMY_VLS_NO" ]
then
echo "Please run the following command to assign your network number" >&2
echo "to the shell varible MY_NETWORK_NO MY_VLS_NO and run this script again." >&2
echo "Please replace xx with your actual network number." >&2
echo " export MY_NETWORK_NOMY_VLS_NO=xx" >&2
exit 1
fi
X=${MY_NETWORK_NOMY_VLS_NO}
echo "Your Network Number is $X"
echo -n "Press ENTER to continue ... "
read dummy
if [ "$X" -ne "$y" ]
then
echo "Adding route to 192.168.${y}.0 network."
fi
done
nmcli con down ens192nmcli con up ens192ip route show # enable IP forwarding on the gateway, add "net.ipv4.ip_forward = 1" to /etc/sysctl.conf
echo "1" > /proc/sys/net/ipv4/ip_forward
</pre>