r3338 deviceconfig: implement "deploy" command and do some code cleanup
authorDenis Ovsienko <infrastation@yandex.ru>
Wed, 10 Mar 2010 10:37:25 +0000 (10:37 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Wed, 10 Mar 2010 10:37:25 +0000 (10:37 +0000)
gateways/deviceconfig/fdry5.connector
gateways/deviceconfig/ios12.connector
gateways/deviceconfig/main

index cb3e306bc1ca6db7621fbe1bf9c4465af8e6a99d..96c2d0e2d07d69b74b2158c9be7382553b4d76e4 100755 (executable)
@@ -1,9 +1,10 @@
 #!/bin/sh
 
-[ $# = 2 ] || exit 1
+[ $# = 3 ] || exit 1
 
 ENDPOINT=$1
-OUT1=$2
+COMMAND=$2
+WORKFILE=$3
 
 prepare_connect_commands()
 {
@@ -27,13 +28,13 @@ prepare_connect_commands()
                if [ -z "${1##$cval}" ]; then
                        found=yes
                        username=`echo $line | cut -s -d' ' -f5`
-                       [ "$username" != "-" ] && echo $username > $CMDS1
+                       [ "$username" != "-" ] && echo $username > "$SESSION"
                        # access password
                        access_password=`echo $line | cut -s -d' ' -f6`
-                       [ "$access_password" != "-" ] && echo "$access_password" >> $CMDS1
-                       printf "en\r\n" >> $CMDS1
+                       [ "$access_password" != "-" ] && echo "$access_password" >> "$SESSION"
+                       printf "en\r\n" >> "$SESSION"
                        enable_password=`echo $line | cut -s -d' ' -f7`
-                       [ "$enable_password" != "-" ] && echo $enable_password >> $CMDS1
+                       [ "$enable_password" != "-" ] && echo $enable_password >> "$SESSION"
                        break
                fi
        done < "$MYDIR/switch.secrets.php"
@@ -41,14 +42,22 @@ prepare_connect_commands()
        exit 3
 }
 
-CMDS1=`mktemp /tmp/fdry5.connector.XXXX`
-[ -f "$CMDS1" ] || exit 5
+SESSION=`mktemp /tmp/fdry5.connector.XXXX`
+[ -f "$SESSION" ] || exit 5
 prepare_connect_commands $ENDPOINT
-printf 'skip-page-display\r\nshow running-config\r\nexit\r\nexit\r\n' >> $CMDS1
-cat $CMDS1 | nc -i 1 $ENDPOINT 23 > "$OUT1"
-if fgrep -q '% Bad passwords' "$OUT1"; then
-       rm -f "$CMDS1"
-       exit 4
-fi
-rm -f "$CMDS1"
+case $COMMAND in
+retrieve)
+       printf 'skip-page-display\r\nshow running-config\r\n' >> "$SESSION"
+       outfile="$WORKFILE"
+       ;;
+deploy)
+       cat "$WORKFILE" >> "$SESSION"
+       outfile=/dev/null
+       ;;
+*)
+       outfile=/dev/null
+esac
+printf 'exit\r\nexit\r\n' >> "$SESSION"
+nc -i 1 $ENDPOINT 23 < "$SESSION" > "$outfile"
+rm -f "$SESSION"
 exit 0
index c381bc35bd1d7b2ca7aeda98d96eaf2f6c5de6e1..064679737fec79875767d6c815b2b18aac0f02da 100755 (executable)
@@ -1,9 +1,10 @@
 #!/bin/sh
 
-[ $# = 2 ] || exit 1
+[ $# = 3 ] || exit 1
 
 ENDPOINT=$1
-OUT1=$2
+COMMAND=$2
+WORKFILE=$3
 
 prepare_connect_commands()
 {
@@ -27,13 +28,13 @@ prepare_connect_commands()
                if [ -z "${1##$cval}" ]; then
                        found=yes
                        username=`echo $line | cut -s -d' ' -f5`
-                       [ "$username" != "-" ] && echo $username > $CMDS1
+                       [ "$username" != "-" ] && echo $username > $SESSION
                        # access password
-                       echo $line | cut -s -d' ' -f6 >> $CMDS1
+                       echo $line | cut -s -d' ' -f6 >> $SESSION
                        enable_password=`echo $line | cut -s -d' ' -f7`
                        [ "$enable_password" != "-" ] && {
-                               echo en >> $CMDS1
-                               echo $enable_password >> $CMDS1
+                               echo en >> $SESSION
+                               echo $enable_password >> $SESSION
                        }
                        break
                fi
@@ -42,14 +43,23 @@ prepare_connect_commands()
        exit 3
 }
 
-CMDS1=`mktemp /tmp/cisco.connector.XXXX`
-[ -f "$CMDS1" ] || exit 5
+SESSION=`mktemp /tmp/ios12.connector.XXXX`
+[ -f "$SESSION" ] || exit 5
 prepare_connect_commands $ENDPOINT
-printf 'term len 0\nshow run\n! END OF CONFIG\nshow vlan brief\n! END OF VLAN LIST\nquit\n' >> $CMDS1
-nc $ENDPOINT 23 < $CMDS1 > "$OUT1"
-if fgrep -q '% Bad passwords' "$OUT1"; then
-       rm -f "$CMDS1"
-       exit 4
-fi
-rm -f "$CMDS1"
+case $COMMAND in
+retrieve)
+       printf 'term len 0\nshow run\n! END OF CONFIG\nshow vlan brief\n! END OF VLAN LIST\n' >> "$SESSION"
+       outfile="$WORKFILE"
+       ;;
+deploy)
+       cat "$WORKFILE" >> "$SESSION"
+       outfile=/dev/null
+       ;;
+*)
+       outfile=/dev/null
+       ;;
+esac
+printf 'quit\n' >> "$SESSION"
+nc $ENDPOINT 23 < "$SESSION" > "$outfile"
+rm -f "$SESSION"
 exit 0
index ec45f4d53d452c692be13c785d535031cdf02a6f..7f2b403acdee17f28f442a0070c95790e145c470 100755 (executable)
@@ -2,10 +2,13 @@
 
 # This gateway retrieves the output of "show run" command (or its
 # equivalent) and prints it onto stdout. When run, it accepts the
-# only command on stdin:
+# following commands on stdin:
 #
 # * retrieve <endpoint> <handler> <outputfile>
 #   (save remote config text into provided local file)
+#
+# * deploy <endpoint> <handler> <inputfile>
+#   (execute given text in privileged mode)
 
 MYDIR=`dirname $0`
 
@@ -44,11 +47,12 @@ decode_error()
 
 do_work()
 {
-       local endpoint=`echo $args | cut -s -d' ' -f1`
-       local handler=`echo $args | cut -s -d' ' -f2`
-       local conftext=`echo $args | cut -s -d' ' -f3`
+       local command=$1
+       local endpoint=$2
+       local handler=$3
+       local conftext=$4
        # sanity checks
-       if [ -z "$endpoint" -o -z "$conftext" -o -z "$handler" ]; then
+       if [ -z "$endpoint" -o -z "$conftext" -o -z "$handler" -o -z "$command" ]; then
                echo 'ERR!too few arguments to connect'
                return
        fi
@@ -56,10 +60,10 @@ do_work()
                echo "ERR!Handler '$handler' is not available"
                exit 1
        }
-       "$MYDIR/$handler.connector" $endpoint "$conftext"
+       "$MYDIR/$handler.connector" $endpoint $command "$conftext"
        local ret=$?
        if [ $ret = 0 ]; then
-               echo "OK!connected to $endpoint"
+               echo "OK!request '$command' complete for $endpoint"
        else
                echo -n "ERR!Cannot connect to $endpoint ("
                decode_error $ret
@@ -69,8 +73,8 @@ do_work()
 
 while read cmd args; do
        case $cmd in
-               retrieve)
-                       do_work $args
+               retrieve|deploy)
+                       do_work $cmd $args
                        ;;
                *)
                        echo "ERR!unknown command $cmd"