X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=simple-cdd%2Fadditional_scripts%2Fconnect_switch.tcl;fp=simple-cdd%2Fadditional_scripts%2Fconnect_switch.tcl;h=5f2299e30134996c02c5c119c298519f22353318;hb=b3f3562f417f2983a7e0d0c2d5b54bb3bd9e94ab;hp=0000000000000000000000000000000000000000;hpb=40346f7c7585854238262f7b8ca4b5a0e5c5f0b4;p=debian6500.git diff --git a/simple-cdd/additional_scripts/connect_switch.tcl b/simple-cdd/additional_scripts/connect_switch.tcl new file mode 100755 index 0000000..5f2299e --- /dev/null +++ b/simple-cdd/additional_scripts/connect_switch.tcl @@ -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.tcl \[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: