r1213 + tagged RackTables 0.14.6
[racktables] / gateways / switchvlans / cisco.connector
CommitLineData
533aefb8
DO
1#!/bin/sh
2
fc11a02c 3[ $# = 7 ] || exit 1
533aefb8
DO
4
5ENDPOINT=$1
fc11a02c
DO
6HW=$2
7SW=$3
8COMMAND=$4
9FILE1=$5
10FILE2=$6
11FILE3=$7
533aefb8 12
697a123b 13prepare_connect_commands()
533aefb8 14{
697a123b
DO
15 [ $# = 1 ] || exit 2
16 local skip=yes cval found=no
533aefb8
DO
17 while read line; do
18 if [ "$skip" = "yes" -a "$line" = "# S-T-A-R-T" ]; then
19 skip=no
20 continue
21 fi
22 if [ "$skip" = "no" -a "$line" = "# S-T-O-P" ]; then
23 skip=yes
24 continue
25 fi
26 [ "$skip" = "yes" ] && continue
27 # Allow comments.
28 [ -z "${line###*}" ] && continue
29
30 # First endpoint string/regexp match is sufficient for us.
31 cval=`echo $line | cut -s -d' ' -f1`
32 if [ -z "${1##$cval}" ]; then
697a123b 33 found=yes
533aefb8
DO
34 # Don't be too smart at the moment, just be able to handle
35 # the known-good case ;-)
36
37 echo $line | cut -s -d' ' -f6 > $CMDS1
38 echo en >> $CMDS1
39 echo $line | cut -s -d' ' -f7 >> $CMDS1
40
41 # same for ports
42 echo $line | cut -s -d' ' -f6 > $CMDS2
43 echo en >> $CMDS2
44 echo $line | cut -s -d' ' -f7 >> $CMDS2
45 break
46 fi
47 done < `dirname $0`/cisco.secrets.php
697a123b
DO
48 [ "$found" = "yes" ] && return
49 echo "E!connector could not find credentials for $1" >> "$FILE2"
50 exit 3
51}
52
53prepare_fetch_commands()
54{
533aefb8
DO
55 printf 'term len 0\nshow vlan brief\nquit\n' >> $CMDS1
56 printf 'term len 0\nshow int status\nquit\n' >> $CMDS2
fc11a02c 57 printf 'term len 0\nshow mac-address-table dynamic\nquit\n' >> $CMDS3
533aefb8
DO
58}
59
7865c525 60prepare_push_commands()
533aefb8 61{
5b6704b5
DO
62 printf 'term len 0\nconf t\n' >> $CMDS1
63 while read portname vlanid; do
64 if [ -z "$portname" -o -z "$vlanid" ]; then
65 echo "E!could not parse input in connector" >> "$FILE2"
66 continue
67 fi
68 if [ "$vlanid" = "trunk" ]; then
69 echo "E!trunking is not allowed" >> "$FILE2"
70 continue
71 fi
72 printf "int $portname\nswi access vlan $vlanid\nexit\n" >> $CMDS1
73 echo "I!Port $portname@$ENDPOINT has been assigned to VLAN $vlanid" >> "$FILE2"
74 done < "$FILE1"
75 printf "end\nquit\n" >> $CMDS1
7865c525
DO
76}
77
78do_fetch()
79{
697a123b
DO
80 nc $ENDPOINT 23 < $CMDS1 > "$OUT1"
81 if fgrep -q '% Bad passwords' "$OUT1"; then
82 echo "E!password mismatch while trying to connect to $ENDPOINT" >> "$FILE2"
83 exit 4
84 fi
85 nc $ENDPOINT 23 < $CMDS2 > "$OUT2"
fc11a02c 86 nc $ENDPOINT 23 < $CMDS3 > "$OUT3"
d28ea105 87 cat "$OUT1" | fgrep ' active ' | sed -E 's/^([[:digit:]]+)[[:space:]]+(.+)[[:space:]]+ active (.*)/\1=\2/;s/[[:space:]]+$//' > $FILE1
0a73c02f 88 cat "$OUT2" | egrep '^(Et|Fa|Gi|Te)' | sed -E 's/ +(notconnect|connected) +/=\1,/;s/,(trunk|([0-9]+)) .*$/,\1/' > $FILE2
fc11a02c
DO
89 # FIXME
90# cat "$OUT3" > "$FILE3"
7865c525
DO
91}
92
93do_push()
94{
5b6704b5 95 nc $ENDPOINT 23 < $CMDS1 >/dev/null
533aefb8
DO
96}
97
697a123b
DO
98remove_tempfiles()
99{
100 [ -f "$CMDS1" ] && rm -f "$CMDS1"
101 [ -f "$CMDS2" ] && rm -f "$CMDS2"
fc11a02c 102 [ -f "$CMDS3" ] && rm -f "$CMDS3"
697a123b
DO
103 [ -f "$OUT1" ] && rm -f "$OUT1"
104 [ -f "$OUT2" ] && rm -f "$OUT2"
fc11a02c 105 [ -f "$OUT3" ] && rm -f "$OUT3"
697a123b 106}
533aefb8 107
697a123b
DO
108create_tempfiles()
109{
110 # This one is for VLAN list.
111 CMDS1=`mktemp /tmp/cisco.connector.XXXX`
fc11a02c 112 # And this one holds ports list...
697a123b 113 CMDS2=`mktemp /tmp/cisco.connector.XXXX`
fc11a02c
DO
114 # ...and one more for MAC address table
115 CMDS3=`mktemp /tmp/cisco.connector.XXXX`
116 # The following are buffers to hold the whole switch output
697a123b
DO
117 # before filtering.
118 OUT1=`mktemp /tmp/cisco.connector.XXXX`
119 OUT2=`mktemp /tmp/cisco.connector.XXXX`
fc11a02c
DO
120 OUT3=`mktemp /tmp/cisco.connector.XXXX`
121 [ -f "$CMDS1" -a -f "$CMDS2" -a -f "$CMDS3" -a -f "$OUT1" -a -f "$OUT2" -a -f "$OUT3" ] && return
697a123b
DO
122 echo "E!connector cannot create tempfiles" >> "$FILE2"
123 remove_tempfiles
124 exit 5
125}
533aefb8
DO
126
127case $COMMAND in
7865c525 128 fetch)
697a123b
DO
129 create_tempfiles
130 prepare_connect_commands $ENDPOINT
131 prepare_fetch_commands
7865c525 132 do_fetch
697a123b 133 remove_tempfiles
533aefb8 134 ;;
7865c525 135 push)
697a123b
DO
136 create_tempfiles
137 prepare_connect_commands $ENDPOINT
138 prepare_push_commands
7865c525 139 do_push
697a123b 140 remove_tempfiles
533aefb8
DO
141 ;;
142 *)
697a123b
DO
143 echo "E!unknown command for connector" >> "$FILE2"
144 exit 6
533aefb8
DO
145 ;;
146esac
147
533aefb8 148exit 0