Created
November 18, 2010 23:46
-
-
Save fleger/705900 to your computer and use it in GitHub Desktop.
Utility to create skeleton files for new NetKit labs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# bash >= 4.0 required | |
[ $# == 0 ] && cat << EOF | |
Usage: $0 machine[:interface1=hub1[,ip1][:interface2=hub2[,ip2]] ...] ... | |
Utility to create skeleton files for new NetKit labs. | |
Options | |
machine: name of a virtual machine | |
interface: interface number | |
hub: collision domain associated to the interface | |
ip: IP address associated to the interface | |
Examples | |
Three machines a, b, c on three different hubs h0, h1, h2 connected by a router r: | |
$0 a:0=h0,192.168.0.2 b:0=h1,192.168.1.2 c:0=h2,192.168.2.2 r:0=h0,192.168.0.1:1=h1,192.168.1.1:2=h2,192.168.2.1 | |
EOF | |
DEBUG="/dev/null" | |
for m; do | |
echo "Parsing $m" >> "$DEBUG" | |
declare -A hubs=() | |
declare -A ips=() | |
if [[ "$m" =~ ^([^:]+):(.+)$ ]]; then | |
name="${BASH_REMATCH[1]}" | |
options="${BASH_REMATCH[2]}" | |
echo "$name has options $options" >> "$DEBUG" | |
OLD_IFS="$IFS" | |
IFS=":" | |
for couple in $options; do | |
echo "Couple $couple" >> "$DEBUG" | |
if [[ "$couple" =~ ^([0-9]+)=(.+) ]]; then | |
interface=${BASH_REMATCH[1]} | |
suboptions=${BASH_REMATCH[2]} | |
echo "Interface $interface has suboptions $suboptions" >> "$DEBUG" | |
if [[ "$suboptions" =~ ^(.+),(.+)$ ]]; then | |
hubs[$interface]=${BASH_REMATCH[1]} | |
ips[$interface]=${BASH_REMATCH[2]} | |
echo "Interface $interface on hub ${hubs[$interface]} at ${ips[$interface]}" >> "$DEBUG" | |
else | |
hubs[$interface]=$suboptions | |
echo "Interface $interface on hub $suboptions" >> "$DEBUG" | |
fi | |
fi | |
done | |
IFS="$OLD_IFS" | |
else | |
echo "Name only" >> "$DEBUG" | |
name="$m" | |
fi | |
mkdir -p "$name" && | |
echo -e "#! /bin/sh\n# $name startup script\n" >> "$name.startup" | |
for interface in "${!hubs[@]}"; do | |
echo -e "$name[$interface]=${hubs[$interface]}" >> "lab.conf" | |
done | |
for interface in "${!ips[@]}"; do | |
echo -e "ifconfig eth$interface ${ips[$interface]}" >> "$name.startup" | |
done | |
echo -e "# Uncomment the following line to set up a default gateway" >> "$name.startup" | |
echo -e "# route add -net default gw 42.42.42.42" >> "$name.startup" | |
echo -e "$name[mem]=64\n" >> "lab.conf" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment