correct issue with trc6500-webconfig package installation
[debian6500.git] / trc6500-master-file / script / connect_switch.tcl
1 #!/usr/bin/expect
2
3 # ============================================================================
4 #
5 # Copyright (C) THALES. All rights reserved
6 # Author: Laurent Mazet
7 #
8 # Script de connection au switchs
9 #
10 # 2013-10-10 V1.0 : Creation
11 #
12 # 2014-03-05 V1.1 : LMa ajout de passage de commande CISCO en parametre
13 #
14 # 2014-06-10 V1.2 : LMa ajout d'un fichier de commandes
15 #
16 # ============================================================================
17
18 # default value
19 set adminpassword "thales"
20 set cmd {}
21 set host ""
22 set loginpassword "thales"
23 set prompt "SW*"
24
25 proc usage {rc} {
26 puts "usage: connect_switch.tcl \[host\] \[-a adminpassword\] \[-c command\] \[-f file\] \[-h\] \[-l loginpassword\] \[-p prompt\]"
27 exit $rc
28 }
29
30 # process argument
31 set action ""
32 for {set i 0} {$i < [llength $argv]} {incr i} {
33 set arg [lindex $argv $i]
34 switch $arg {
35 -a { set action "-adminpassword" }
36 -c { set action "-cmd" }
37 -f { set action "-file" }
38 -h { usage 0 }
39 -l { set action "-loginpassword" }
40 -p { set action "-prompt" }
41 default {
42 switch $action {
43 -adminpassword { set adminpassword $arg }
44 -cmd { set cmd [lappend cmd $arg] }
45 -file {
46 if {[file isfile $arg] == 1} then {
47 set f [open $arg "r"]
48 set data [read $f]
49 close $f
50 foreach line [split $data "\n"] {
51 set cmd [lappend cmd $line]
52 }
53 } else {
54 puts "can't open file '$arg'"
55 exit 1
56 }
57 }
58 -loginpassword { set loginpassword $arg }
59 -prompt { set prompt $arg }
60 default { set host $arg }
61 }
62 set action ""
63 }
64 }
65 }
66 if {$action != ""} {
67 puts "missing arguments"
68 exit 1
69 }
70 if { $host == "" } {
71 usage 1
72 }
73
74 # start connection
75 spawn telnet $host
76
77 # login password
78 expect {
79 "Password:" { send "$loginpassword\r" }
80 timeout { send_user "connection to host failed\n"; exit 1 }
81 eof { send_user "connection to host failed\n"; exit 1 }
82 }
83
84 # admin password
85 expect {
86 "$prompt>" { send "enable\r" }
87 timeout { send_user "connection to host failed\n"; exit 1 }
88 "Password:" { send_user "invalid login password\n"; exit 1 }
89 }
90 expect "Password:" { send "$adminpassword\r" }
91 expect {
92 "$prompt#" { send "\r" }
93 "Password:" { send_user "invalid admin password\n"; exit 1 }
94 }
95
96 # commands or interactive mode
97 if {[llength $cmd] == 0 } {
98 expect "$prompt#" { interact }
99 } else {
100 expect "$prompt#" { send "terminal length 0\r" }
101 for {set i 0} {$i < [llength $cmd]} {incr i} {
102 expect {
103 "$prompt#" { send "[lindex $cmd $i]\r" }
104 "confirm" { send "\r"; continue }
105 }
106 }
107 expect {
108 "confirm" { send "\r"; expect "$prompt" { send "exit\r" } }
109 "$prompt" { send "exit\r" }
110 }
111 send_user "\n"
112 }
113
114 exit 0
115 # vim:set tabstop=4 shiftwidth=4 softtabstop=4: