r1169 The first ever working switchvlans gateway for the real world, readonly, of...
[racktables] / gateways / switchvlans / cisco.connector
CommitLineData
533aefb8
DO
1#!/bin/sh
2
3[ $# = 4 ] || exit 2
4
5ENDPOINT=$1
6COMMAND=$2
7VLANINFO=$3
8PORTINFO=$4
9
10prepare_load_commands()
11{
12 [ $# = 1 ] || exit 1
13 local skip=yes cval
14 while read line; do
15 if [ "$skip" = "yes" -a "$line" = "# S-T-A-R-T" ]; then
16 skip=no
17 continue
18 fi
19 if [ "$skip" = "no" -a "$line" = "# S-T-O-P" ]; then
20 skip=yes
21 continue
22 fi
23 [ "$skip" = "yes" ] && continue
24 # Allow comments.
25 [ -z "${line###*}" ] && continue
26
27 # First endpoint string/regexp match is sufficient for us.
28 cval=`echo $line | cut -s -d' ' -f1`
29 if [ -z "${1##$cval}" ]; then
30 # Don't be too smart at the moment, just be able to handle
31 # the known-good case ;-)
32
33 echo $line | cut -s -d' ' -f6 > $CMDS1
34 echo en >> $CMDS1
35 echo $line | cut -s -d' ' -f7 >> $CMDS1
36
37 # same for ports
38 echo $line | cut -s -d' ' -f6 > $CMDS2
39 echo en >> $CMDS2
40 echo $line | cut -s -d' ' -f7 >> $CMDS2
41 break
42 fi
43 done < `dirname $0`/cisco.secrets.php
44 printf 'term len 0\nshow vlan brief\nquit\n' >> $CMDS1
45 printf 'term len 0\nshow int status\nquit\n' >> $CMDS2
46}
47
48do_load()
49{
50 nc $ENDPOINT 23 < $CMDS1 | fgrep active | cut -d' ' -f1 > $VLANINFO
51 nc $ENDPOINT 23 < $CMDS2 | egrep '^(Et|Fa|Gi|Te)' | sed -E 's/ +(notconnect|connected) +/=/;s/=(trunk|([0-9]+)) .*$/=\1/' > $PORTINFO
52}
53
54# This one is for VLAN list.
55CMDS1=`mktemp /tmp/cisco.connector.XXXX`
56[ -f "$CMDS1" ] || exit 2
57
58# And this one holds ports list.
59CMDS2=`mktemp /tmp/cisco.connector.XXXX`
60if ! [ -f "$CMDS2" ]; then
61 rm -f "$CMDS1"
62 exit 2
63fi
64
65case $COMMAND in
66 load)
67 prepare_load_commands $ENDPOINT
68 do_load
69 ;;
70 save)
71 rm -f "$CMDS1" "$CMDS2"
72 exit 1
73 ;;
74 *)
75 rm -f "$CMDS1" "$CMDS2"
76 exit 2
77 ;;
78esac
79
80rm -f "$CMDS1" "$CMDS2"
81exit 0