r1194 + dropping useless file, cause svn:ignore is an internal property
[racktables] / gateways / switchvlans / cisco.connector
1 #!/bin/sh
2
3 [ $# = 4 ] || exit 1
4
5 ENDPOINT=$1
6 COMMAND=$2
7 FILE1=$3
8 FILE2=$4
9
10 prepare_connect_commands()
11 {
12 [ $# = 1 ] || exit 2
13 local skip=yes cval found=no
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 found=yes
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
45 [ "$found" = "yes" ] && return
46 echo "E!connector could not find credentials for $1" >> "$FILE2"
47 exit 3
48 }
49
50 prepare_fetch_commands()
51 {
52 printf 'term len 0\nshow vlan brief\nquit\n' >> $CMDS1
53 printf 'term len 0\nshow int status\nquit\n' >> $CMDS2
54 }
55
56 prepare_push_commands()
57 {
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
72 }
73
74 do_fetch()
75 {
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"
82 cat "$OUT1" | fgrep ' active ' | sed -E 's/^([[:digit:]]+)[[:space:]]+(.+)[[:space:]]+ active (.*)/\1=\2/;s/[[:space:]]+$//' > $FILE1
83 cat "$OUT2" | egrep '^(Et|Fa|Gi|Te)' | sed -E 's/ +(notconnect|connected) +/=/;s/=(trunk|([0-9]+)) .*$/=\1/' > $FILE2
84 }
85
86 do_push()
87 {
88 nc $ENDPOINT 23 < $CMDS1 >/dev/null
89 }
90
91 remove_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 }
98
99 create_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 }
114
115 case $COMMAND in
116 fetch)
117 create_tempfiles
118 prepare_connect_commands $ENDPOINT
119 prepare_fetch_commands
120 do_fetch
121 remove_tempfiles
122 ;;
123 push)
124 create_tempfiles
125 prepare_connect_commands $ENDPOINT
126 prepare_push_commands
127 do_push
128 remove_tempfiles
129 ;;
130 *)
131 echo "E!unknown command for connector" >> "$FILE2"
132 exit 6
133 ;;
134 esac
135
136 exit 0