Internal/OpenFlow/XORP: swconf.sh

File swconf.sh, 3.8 KB (added by akoshibe, 13 years ago)
Line 
1#!/bin/bash
2#swconf.sh v0.2 - a bash script to range-configure XorPlus-powered switches
3#fixed some bugs related to not having a switch to test it on...
4
5declare -a CMD #array of commands, primary context
6declare -a VARR #array of member VLANs, for trunks
7
8#DEV is usually /dev/ttyS0 on a machine with a serial port. Assume USB converter
9DEV=/dev/ttyUSB0
10
11#show help prompt and exit
12if [ "-h" = "$1" ]; then
13 echo "swconf.sh - a hacky bash script to range-configure XorPlus-powered switches"
14 echo "-h : show this prompt"
15 echo
16else
17#options: config mgmt. port, range ports - vlan, port modes (trunk or access), save, exit.
18#enter configure mode
19 echo "configure" >> $DEV
20
21 echo -n "> "
22 read -a CMD
23
24 while [ "${CMD[0]}" != "exit" ]; do
25 case "${CMD[0]}" in
26 "help" | "?" )
27 echo "iface [eth0|eth1] [IP addr] - Configure management interface address. Address is in CIDR form (e.g 172.16.0.30/16)"
28 echo "vlan [VLAN ID] - instantiate a VLAN interface of tag [VLAN ID]"
29 echo "ports [min] [max] - Configure VLAN and mode of ports between [min] and [max] inclusive"
30 echo "save - Commit changes to memory"
31 echo "exit - exit script"
32 echo "help - display this list (equivalent to '?')"
33 echo "clear - clear the screen"
34 ;;
35 "iface" ) #[iface] [if] [addr]
36 if [ -z "${CMD[1]}" ]; then
37 echo "iface [eth0|eth1] [IP addr/mask]"
38 else
39 echo "set interface management-ethernet ${CMD[1]} address ${CMD[2]}" >> $DEV
40 fi
41 ;;
42 "vlan" ) #instantiate a new vlan
43 if [ -z "${CMD[1]}" ]; then
44 echo "vlan [VLAN ID]"
45 else
46 echo "set vlans vlan-id ${CMD[1]}" >> $DEV
47 fi
48 ;;
49 "ports" ) #range configure ports
50 if [ -z "${CMD[1]}" ]; then
51 echo "ports [min] [max]"
52 else
53 echo -n "("${CMD[0]}")# " #change prompt to show new context
54 read OPT PAR1 PAR2
55 while [ "$OPT" != "exit" ]; do #stay in ports context until given 'exit'
56 case "$OPT" in
57 "delete")
58 for i in `seq ${CMD[1]} ${CMD[2]}`; do
59 sleep 0.2
60 echo "delete interface gigabit-ethernet ge-1/1/$i family ethernet-switching"
61 done >> $DEV
62 ;;
63 "vlan") #set vlan association
64 for i in `seq ${CMD[1]} ${CMD[2]}`; do
65 sleep 0.2
66 echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching native-vlan-id $PAR1"
67 done >> $DEV
68 ;;
69 "mode") #set port mode
70 if [ "$PAR1" = "access" ]; then
71 for i in `seq ${CMD[1]} ${CMD[2]}`; do
72 sleep 0.2
73 echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching port-mode access"
74 done >> $DEV
75 elif [ "$PAR1" = "trunk" ]; then
76 echo -n "trunk member vlans (X Y Z...): "
77 read -a VARR
78 for i in `seq ${CMD[1]} ${CMD[2]}`; do
79 sleep 0.2
80 for j in ${VARR[@]}; do
81 echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching vlan members $j"
82 done
83 echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching port-mode trunk"
84 done >> $DEV
85 else
86 echo "Unknown command"
87 fi
88 ;;
89 "range") #set new ports range
90 CMD[1]=$PAR1
91 CMD[2]=$PAR2
92 ;;
93 "help" | "?" )
94 echo "vlan [1..1020]: set native vlan for ports"
95 echo "mode [access|trunk]"
96 echo "delete: wipe out port settings"
97 echo "range [min] [max]: configure new range of ports"
98 echo "exit: exit from port context"
99 ;;
100 * ) echo "Unknown command" ;;
101 esac
102 echo -n "("${CMD[0]}")# "
103 read OPT PAR1 PAR2
104 done
105 fi
106 ;;
107 "save" )
108 echo "saving settings..."
109 echo "commit" >> $DEV
110 sleep 5 #save takes a while
111 echo "save running-to-startup" >> $DEV
112 ;;
113 "clear" ) clear ;;
114 * ) echo "Unknown command" ;;
115 esac
116 echo -n "> "
117 read -a CMD
118 done
119#escape configure mode
120 sleep 0.2
121 echo "exit" >> $DEV
122fi
123exit 0