remove extention on some scripts and correct services
[debian6500.git] / master / opt / script / connect_switch
diff --git a/master/opt/script/connect_switch b/master/opt/script/connect_switch
new file mode 100755 (executable)
index 0000000..67f7678
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/expect
+
+# ============================================================================
+#
+# Copyright (C) THALES. All rights reserved
+# Author: Laurent Mazet
+# 
+# Script de connection au switchs
+#
+# 2013-10-10 V1.0 : Creation
+#
+# 2014-03-05 V1.1 : LMa ajout de passage de commande CISCO en parametre
+#
+# 2014-06-10 V1.2 : LMa ajout d'un fichier de commandes
+# 
+# ============================================================================
+
+# default value
+set adminpassword "thales"
+set cmd {}
+set host ""
+set loginpassword "thales"
+set prompt "SW*"
+
+proc usage {rc} {
+    puts "usage: connect_switch \[host\] \[-a adminpassword\] \[-c command\] \[-f file\] \[-h\] \[-l loginpassword\] \[-p prompt\]"
+    exit $rc
+}
+
+# process argument
+set action ""
+for {set i 0} {$i < [llength $argv]} {incr i} {
+    set arg [lindex $argv $i]
+    switch $arg {
+        -a { set action "-adminpassword" }
+        -c { set action "-cmd" }
+        -f { set action "-file" }
+        -h { usage 0 }
+        -l { set action "-loginpassword" }
+        -p { set action "-prompt" }
+        default {
+            switch $action {
+                -adminpassword { set adminpassword $arg }
+                -cmd { set cmd [lappend cmd $arg] }
+                -file {
+                    if {[file isfile $arg] == 1} then {
+                        set f [open $arg "r"]
+                        set data [read $f]
+                        close $f
+                        foreach line [split $data "\n"] {
+                            set cmd [lappend cmd $line]
+                        }
+                    } else {
+                        puts "can't open file '$arg'"
+                        exit 1
+                    }
+                }
+                -loginpassword { set loginpassword $arg }
+                -prompt { set prompt $arg }
+                default { set host $arg }
+            }
+        set action ""
+        }
+    }
+}
+if {$action != ""} {
+    puts "missing arguments"
+    exit 1
+}
+if { $host == "" } {
+    usage 1
+}
+
+# start connection
+spawn telnet $host
+
+# login password
+expect {
+    "Password:" { send "$loginpassword\r" }
+    timeout { send_user "connection to host failed\n"; exit 1 }
+    eof { send_user "connection to host failed\n"; exit 1 }
+}
+
+# admin password
+expect {
+    "$prompt>" { send "enable\r" }
+    timeout { send_user "connection to host failed\n"; exit 1 }
+    "Password:" { send_user "invalid login password\n"; exit 1 }
+}
+expect "Password:" { send "$adminpassword\r" }
+expect {
+    "$prompt#" { send "\r" }
+    "Password:" { send_user "invalid admin password\n"; exit 1 }
+}
+
+# commands or interactive mode
+if {[llength $cmd] == 0 } {
+    expect "$prompt#" { interact }
+} else {
+    expect "$prompt#" { send "terminal length 0\r" }
+    for {set i 0} {$i < [llength $cmd]} {incr i} {
+        expect {
+            "$prompt#" { send "[lindex $cmd $i]\r" }
+            "confirm" { send "\r"; continue }
+        }
+    }
+    expect {
+        "confirm" { send "\r"; expect "$prompt" { send "exit\r" } }
+        "$prompt" { send "exit\r" }
+    }
+    send_user "\n"
+}
+
+exit 0
+# vim:set tabstop=4 shiftwidth=4 softtabstop=4: