r3305 gwRetrieveDeviceConfig(): initial implementation of gateway wrapper for "device...
[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> <outputfile> <hardware> <software>
8 # (save config text into provided file)
9
10 MYDIR=`dirname $0`
11
12 decode_error()
13 {
14 case "$1" in
15 0)
16 echo -n 'success'
17 ;;
18 1)
19 echo -n 'internal error 1'
20 ;;
21 2)
22 echo -n 'internal error 2'
23 ;;
24 3)
25 echo -n 'password not found'
26 ;;
27 4)
28 echo -n 'invalid password'
29 ;;
30 5)
31 echo -n 'cannot create temporary files'
32 ;;
33 6)
34 echo -n 'invalid command'
35 ;;
36 7)
37 echo -n 'unknown host OS'
38 ;;
39 *)
40 echo -n 'unknown error'
41 ;;
42 esac
43 }
44
45 do_work()
46 {
47 local endpoint=`echo $args | cut -s -d' ' -f1`
48 local conftext=`echo $args | cut -s -d' ' -f2`
49 local hw=`echo $args | cut -s -d' ' -f3`
50 local sw=`echo $args | cut -s -d' ' -f4`
51 # sanity checks
52 if [ -z "$endpoint" -o -z "$conftext" -o -z "$hw" -o -z "$sw" ]; then
53 echo 'ERR!too few arguments to connect'
54 return
55 fi
56 case "$sw" in
57 Cisco+IOS+12.0|Cisco+IOS+12.1|Cisco+IOS+12.2)
58 local handler=cisco
59 ;;
60 *)
61 echo "ERR!Don't know how to handle $sw on $endpoint"
62 return
63 ;;
64 esac
65
66 "$MYDIR/$handler.connector" $endpoint "$conftext"
67 local ret=$?
68 if [ $ret = 0 ]; then
69 echo "OK!connected to $endpoint"
70 else
71 echo -n "ERR!Cannot connect to $endpoint ("
72 decode_error $ret
73 echo ')'
74 fi
75 }
76
77 while read cmd args; do
78 case $cmd in
79 retrieve)
80 do_work $args
81 ;;
82 *)
83 echo "ERR!unknown command $cmd"
84 esac
85 done
86
87 exit 0