r1752 + don't check non-existent VLAN IDs
[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
533aefb8 40 # same for ports
9e76e210
DO
41 cat "$CMDS1" > "$CMDS2"
42 # ...and MAC addresses
43 cat "$CMDS1" > "$CMDS3"
533aefb8
DO
44 break
45 fi
46 done < `dirname $0`/cisco.secrets.php
697a123b
DO
47 [ "$found" = "yes" ] && return
48 echo "E!connector could not find credentials for $1" >> "$FILE2"
49 exit 3
50}
51
52prepare_fetch_commands()
53{
533aefb8
DO
54 printf 'term len 0\nshow vlan brief\nquit\n' >> $CMDS1
55 printf 'term len 0\nshow int status\nquit\n' >> $CMDS2
fc11a02c 56 printf 'term len 0\nshow mac-address-table dynamic\nquit\n' >> $CMDS3
533aefb8
DO
57}
58
7865c525 59prepare_push_commands()
533aefb8 60{
5b6704b5
DO
61 printf 'term len 0\nconf t\n' >> $CMDS1
62 while read portname vlanid; do
63 if [ -z "$portname" -o -z "$vlanid" ]; then
64 echo "E!could not parse input in connector" >> "$FILE2"
65 continue
66 fi
67 if [ "$vlanid" = "trunk" ]; then
68 echo "E!trunking is not allowed" >> "$FILE2"
69 continue
70 fi
71 printf "int $portname\nswi access vlan $vlanid\nexit\n" >> $CMDS1
72 echo "I!Port $portname@$ENDPOINT has been assigned to VLAN $vlanid" >> "$FILE2"
73 done < "$FILE1"
be6ae134 74 printf "end\nwri\nquit\n" >> $CMDS1
7865c525
DO
75}
76
77do_fetch()
78{
697a123b
DO
79 nc $ENDPOINT 23 < $CMDS1 > "$OUT1"
80 if fgrep -q '% Bad passwords' "$OUT1"; then
81 echo "E!password mismatch while trying to connect to $ENDPOINT" >> "$FILE2"
82 exit 4
83 fi
84 nc $ENDPOINT 23 < $CMDS2 > "$OUT2"
fc11a02c 85 nc $ENDPOINT 23 < $CMDS3 > "$OUT3"
c882b87d 86 cat "$OUT1" | fgrep ' active ' | sed -E 's/^([[:digit:]]+)[[:space:]]+(.+)[[:space:]]+active (.*)/\1=\2/;s/[[:space:]]+$//' > $FILE1
d4da71b6 87 cat "$OUT2" | egrep '^(Et|Fa|Gi|Te)' | sed -E 's/^([A-Za-z/0-9]+).+(connected|notconnect|disabled|monitoring|suspended) +/\1=\2,/;s/,(trunk|([0-9]+)) .*$/,\1/;s/=(monitoring|suspended),/=connected,/' > $FILE2
fc11a02c 88 # FIXME
9e76e210
DO
89 # Here we need to distinguish between different platforms and IOS version,
90 # cause they produce output in different formats.
91 if [ "$SW" = "Cisco+IOS+12.0" ]; then
92 cat "$OUT3" | tr -d '\r' | fgrep Dynamic | sed -E 's/ +Dynamic +([0-9]+) +(.+)/=\1@\2/;s/FastEthernet/Fa/;s/GigabitEthernet/Gi/' > "$FILE3"
2a078c28
DO
93 elif [ "$SW" = "Cisco+IOS+12.2" ]; then
94 case "$HW" in
9a8ce4df 95 Cisco+Catalyst+35*|Cisco+Catalyst+37*|Cisco+Catalyst+29*|Cisco+Catalyst+49*)
2a078c28
DO
96 cat "$OUT3" | tr -d '\r' | fgrep DYNAMIC | \
97 sed -E 's/ +([0-9]+) +(.+) DYNAMIC +(.+)/\2=\1@\3/;s/FastEthernet/Fa/;s/GigabitEthernet/Gi/' > "$FILE3"
98 ;;
99 esac
9e76e210 100 fi
7865c525
DO
101}
102
103do_push()
104{
5b6704b5 105 nc $ENDPOINT 23 < $CMDS1 >/dev/null
533aefb8
DO
106}
107
697a123b
DO
108remove_tempfiles()
109{
110 [ -f "$CMDS1" ] && rm -f "$CMDS1"
111 [ -f "$CMDS2" ] && rm -f "$CMDS2"
fc11a02c 112 [ -f "$CMDS3" ] && rm -f "$CMDS3"
697a123b
DO
113 [ -f "$OUT1" ] && rm -f "$OUT1"
114 [ -f "$OUT2" ] && rm -f "$OUT2"
fc11a02c 115 [ -f "$OUT3" ] && rm -f "$OUT3"
697a123b 116}
533aefb8 117
697a123b
DO
118create_tempfiles()
119{
120 # This one is for VLAN list.
121 CMDS1=`mktemp /tmp/cisco.connector.XXXX`
fc11a02c 122 # And this one holds ports list...
697a123b 123 CMDS2=`mktemp /tmp/cisco.connector.XXXX`
fc11a02c
DO
124 # ...and one more for MAC address table
125 CMDS3=`mktemp /tmp/cisco.connector.XXXX`
126 # The following are buffers to hold the whole switch output
697a123b
DO
127 # before filtering.
128 OUT1=`mktemp /tmp/cisco.connector.XXXX`
129 OUT2=`mktemp /tmp/cisco.connector.XXXX`
fc11a02c
DO
130 OUT3=`mktemp /tmp/cisco.connector.XXXX`
131 [ -f "$CMDS1" -a -f "$CMDS2" -a -f "$CMDS3" -a -f "$OUT1" -a -f "$OUT2" -a -f "$OUT3" ] && return
697a123b
DO
132 echo "E!connector cannot create tempfiles" >> "$FILE2"
133 remove_tempfiles
134 exit 5
135}
533aefb8
DO
136
137case $COMMAND in
7865c525 138 fetch)
697a123b
DO
139 create_tempfiles
140 prepare_connect_commands $ENDPOINT
141 prepare_fetch_commands
7865c525 142 do_fetch
697a123b 143 remove_tempfiles
533aefb8 144 ;;
7865c525 145 push)
697a123b
DO
146 create_tempfiles
147 prepare_connect_commands $ENDPOINT
148 prepare_push_commands
7865c525 149 do_push
697a123b 150 remove_tempfiles
533aefb8
DO
151 ;;
152 *)
697a123b
DO
153 echo "E!unknown command for connector" >> "$FILE2"
154 exit 6
533aefb8
DO
155 ;;
156esac
157
533aefb8 158exit 0