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