r1185 support IOS 12.0 as well
[racktables] / gateways / switchvlans / cisco.connector
CommitLineData
533aefb8
DO
1#!/bin/sh
2
3[ $# = 4 ] || exit 2
4
5ENDPOINT=$1
6COMMAND=$2
7865c525
DO
7FILE1=$3
8FILE2=$4
533aefb8 9
7865c525 10prepare_fetch_commands()
533aefb8
DO
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
5b6704b5 48# FIXME: this is almost a dup!
7865c525 49prepare_push_commands()
533aefb8 50{
5b6704b5
DO
51 [ $# = 1 ] || exit 1
52 local skip=yes cval
53 while read line; do
54 if [ "$skip" = "yes" -a "$line" = "# S-T-A-R-T" ]; then
55 skip=no
56 continue
57 fi
58 if [ "$skip" = "no" -a "$line" = "# S-T-O-P" ]; then
59 skip=yes
60 continue
61 fi
62 [ "$skip" = "yes" ] && continue
63 # Allow comments.
64 [ -z "${line###*}" ] && continue
65
66 # First endpoint string/regexp match is sufficient for us.
67 cval=`echo $line | cut -s -d' ' -f1`
68 if [ -z "${1##$cval}" ]; then
69 # Don't be too smart at the moment, just be able to handle
70 # the known-good case ;-)
71
72 echo $line | cut -s -d' ' -f6 > $CMDS1
73 echo en >> $CMDS1
74 echo $line | cut -s -d' ' -f7 >> $CMDS1
75
76 break
77 fi
78 done < `dirname $0`/cisco.secrets.php
79
80
81
82
83 printf 'term len 0\nconf t\n' >> $CMDS1
84 while read portname vlanid; do
85 if [ -z "$portname" -o -z "$vlanid" ]; then
86 echo "E!could not parse input in connector" >> "$FILE2"
87 continue
88 fi
89 if [ "$vlanid" = "trunk" ]; then
90 echo "E!trunking is not allowed" >> "$FILE2"
91 continue
92 fi
93 printf "int $portname\nswi access vlan $vlanid\nexit\n" >> $CMDS1
94 echo "I!Port $portname@$ENDPOINT has been assigned to VLAN $vlanid" >> "$FILE2"
95 done < "$FILE1"
96 printf "end\nquit\n" >> $CMDS1
7865c525
DO
97}
98
99do_fetch()
100{
101 nc $ENDPOINT 23 < $CMDS1 | fgrep active | cut -d' ' -f1 > $FILE1
102 nc $ENDPOINT 23 < $CMDS2 | egrep '^(Et|Fa|Gi|Te)' | sed -E 's/ +(notconnect|connected) +/=/;s/=(trunk|([0-9]+)) .*$/=\1/' > $FILE2
103}
104
105do_push()
106{
5b6704b5
DO
107 nc $ENDPOINT 23 < $CMDS1 >/dev/null
108# echo "E!this is a stub" >> "$FILE2"
109# echo "W!this is another stub" >> "$FILE2"
110# echo "I!have a good time!" >> "$FILE2"
533aefb8
DO
111}
112
113# This one is for VLAN list.
114CMDS1=`mktemp /tmp/cisco.connector.XXXX`
115[ -f "$CMDS1" ] || exit 2
116
117# And this one holds ports list.
118CMDS2=`mktemp /tmp/cisco.connector.XXXX`
119if ! [ -f "$CMDS2" ]; then
120 rm -f "$CMDS1"
121 exit 2
122fi
123
124case $COMMAND in
7865c525
DO
125 fetch)
126 prepare_fetch_commands $ENDPOINT
127 do_fetch
533aefb8 128 ;;
7865c525
DO
129 push)
130 prepare_push_commands $ENDPOINT
131 do_push
533aefb8
DO
132 ;;
133 *)
134 rm -f "$CMDS1" "$CMDS2"
135 exit 2
136 ;;
137esac
138
139rm -f "$CMDS1" "$CMDS2"
140exit 0