518535a6076dcf1ef29123bad5f696bd6cc6feae
[racktables] / gateways / deviceconfig / main
1 #!/bin/sh
2
3 # This gateway retrieves the output of "show run" command (or its
4 # equivalent) and prints it onto stdout. When run, it accepts the
5 # only command on stdin:
6 #
7 # * retrieve <endpoint> <hardware> <software>: print text and terminate
8
9 endpoint=
10 hw=
11 sw=
12 MYDIR=`dirname $0`
13
14 decode_error()
15 {
16 case "$1" in
17 0)
18 echo -n 'success'
19 ;;
20 1)
21 echo -n 'internal error 1'
22 ;;
23 2)
24 echo -n 'internal error 2'
25 ;;
26 3)
27 echo -n 'password not found'
28 ;;
29 4)
30 echo -n 'invalid password'
31 ;;
32 5)
33 echo -n 'cannot create temporary files'
34 ;;
35 6)
36 echo -n 'invalid command'
37 ;;
38 7)
39 echo -n 'unknown host OS'
40 ;;
41 *)
42 echo -n 'unknown error'
43 ;;
44 esac
45 }
46
47 do_work()
48 {
49 endpoint=`echo $args | cut -s -d' ' -f1`
50 hw=`echo $args | cut -s -d' ' -f2`
51 sw=`echo $args | cut -s -d' ' -f3`
52 # sanity checks
53 if [ -z "$endpoint" -o -z "$hw" -o -z "$sw" ]; then
54 echo 'ERR!too few arguments to connect'
55 return
56 fi
57 case "$sw" in
58 Cisco+IOS+12.0|Cisco+IOS+12.1|Cisco+IOS+12.2)
59 handler=cisco
60 ;;
61 *)
62 echo "ERR!Don't know how to handle $sw on $endpoint"
63 return
64 ;;
65 esac
66
67 CONFTEXT=`mktemp /tmp/racktables.XXXX`
68 if ! [ -f "$CONFTEXT" ]; then
69 echo 'ERR!could not create tmpfile'
70 return
71 fi
72
73 "$MYDIR/$handler.connector" $endpoint "$CONFTEXT"
74 ret=$?
75 if [ $ret = 0 ]; then
76 echo "OK!connected to $endpoint"
77 cat "$CONFTEXT"
78 else
79 echo -n "ERR!Cannot connect to $endpoint ("
80 decode_error $ret
81 echo ')'
82 fi
83 }
84
85 while read cmd args; do
86 case $cmd in
87 retrieve)
88 do_work $args
89 rm -f "$CONFTEXT"
90 exit 0
91 ;;
92 *)
93 echo "ERR!unknown command $cmd"
94 esac
95 done
96
97 exit 0