Work around bug in AIX 7.1 awk in report card tool
[dejagnu.git] / lib / telnet.exp
blob551c94c577f060e53c0f9725862b73e03bc13339
1 # Copyright (C) 1992-2019, 2020 Free Software Foundation, Inc.
3 # This file is part of DejaGnu.
5 # DejaGnu is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # DejaGnu is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with DejaGnu.  If not, see <http://www.gnu.org/licenses/>.
19 # Connect to HOSTNAME using Telnet.  ARGS is a list of options.
20 # Currently the only supported option is "raw".  Sets the fileid field
21 # in the config array and returns -1 for error or the spawn id.
23 proc telnet_open { hostname args } {
24     global verbose
25     global spawn_id
26     global timeout
27     global board_info
29     set raw 0
30     foreach arg $args {
31         switch -- $arg {
32             "raw" { set raw 1 }
33         }
34     }
36     set port 23
37     if {[board_info $hostname exists name]} {
38         set connhost [board_info $hostname name]
39     } else {
40         set connhost $hostname
41     }
43     if {[board_info $connhost exists hostname]} {
44         set hostname [board_info $connhost hostname]
45     }
47     if {[file exists /usr/kerberos/bin/telnet]} {
48         set telnet /usr/kerberos/bin/telnet
49     } else {
50         set telnet telnet
51     }
53     # Instead of unsetting it, let's return it. One connection at a
54     # time, please.
55     if {[board_info $connhost exists fileid]} {
56         return [board_info $connhost fileid]
57     }
59     # Get the hostname and port number from the config array.
60     if {[board_info $connhost exists netport]} {
61         set type $hostname
62         set hosttmp [split [board_info $connhost netport] ":"]
63         set hostname [lindex $hosttmp 0]
64         if { [llength $hosttmp] > 1 } {
65             set port [lindex $hosttmp 1]
66         }
67         unset hosttmp
68     } else {
69         set type target
70     }
72     if {[board_info $connhost exists shell_prompt]} {
73         set shell_prompt [board_info $connhost shell_prompt]
74     }
75     if {![info exists shell_prompt]} {
76         # If no prompt, then set it to something generic.
77         set shell_prompt ".*> "
78     }
80     set tries 0
81     set result -1
82     set need_respawn 1
83     verbose "Starting a telnet connection to $hostname:$port $shell_prompt" 2
84     while { $result < 0 && $tries <= 3 } {
85         if { $need_respawn } {
86             set need_respawn 0
87             spawn $telnet $hostname $port
88         }
89         expect {
90             "Trying " {
91                 exp_continue
92             }
93             -re "$shell_prompt.*$" {
94                 verbose "Got prompt\n"
95                 set result 0
96             }
97             -re "nt Name:|ogin:" {
98                 if {[board_info $connhost exists telnet_username]} {
99                     exp_send "[board_info $connhost telnet_username]\n"
100                     exp_continue
101                 }
102                 if {[board_info $connhost exists username]} {
103                     exp_send "[board_info $connhost username]\n"
104                     exp_continue
105                 }
106                 perror "telnet: need to login"
107                 break
108             }
109             "assword:" {
110                 if {[board_info $connhost exists telnet_password]} {
111                     exp_send "[board_info $connhost telnet_password]\n"
112                     exp_continue
113                 }
114                 if {[board_info $connhost exists password]} {
115                     exp_send "[board_info $connhost password]\n"
116                     exp_continue
117                 }
118                 perror "telnet: need a password"
119                 break
120             }
121             -re {advance.*y/n.*\?} {
122                 exp_send "n\n"
123                 exp_continue
124             }
125             -re {([Aa]dvanced|[Ss]imple) or ([Ss]imple|[Aa]dvanced)} {
126                 exp_send "simple\n"
127                 exp_continue
128             }
129             "Connected to" {
130                 exp_continue
131             }
132             "unknown host" {
133                 exp_send "\003"
134                 perror "telnet: unknown host"
135                 break
136             }
137             "VxWorks Boot" {
138                 exp_send "@\n"
139                 sleep 20
140                 exp_continue
141             }
142             -re {Escape character is.*\.[\r\n]} {
143                 if { $raw || [board_info $connhost exists dont_wait_for_prompt] } {
144                     set result 0
145                 } else {
146                     if {[board_info $connhost exists send_initial_cr]} {
147                         exp_send "\n"
148                     }
149                     exp_continue
150                 }
151             }
152             "has logged on from" {
153                 exp_continue
154             }
155             "You have no Kerberos tickets" {
156                 warning "telnet: no kerberos Tickets, please kinit"
157                 break
158             }
159             -re "Connection refused.*$" {
160                 catch "exp_send \"\003\"" foo
161                 sleep 5
162                 warning "telnet: connection refused."
163             }
164             -re "Sorry, this system is engaged.*" {
165                 exp_send "\003"
166                 warning "telnet: already connected."
167             }
168             "Connection closed by foreign host.*$" {
169                 warning "telnet: connection closed by foreign host."
170                 break
171             }
172             -re {[\r\n]+} {
173                 exp_continue
174             }
175             timeout {
176                 exp_send "\n"
177             }
178             eof {
179                 warning "telnet: got unexpected EOF from telnet."
180                 catch close
181                 catch wait
182                 set need_respawn 1
183                 sleep 5
184             }
185         }
186         incr tries
187     }
189     # We look for this here again cause it means something went wrong,
190     # and it doesn't always show up in the expect in buffer till the
191     # server times out.
192     if {[info exists expect_out(buffer)]} {
193         if {[regexp "assword:|ogin:" $expect_out(buffer)]} {
194             perror "telnet: need to supply a login and password."
195         }
196     }
197     if { $result < 0 } {
198         catch close
199         catch wait
200         set spawn_id -1
201     }
202     if { $spawn_id >= 0 } {
203         verbose "setting board_info($connhost,fileid) to $spawn_id" 3
204         set board_info($connhost,fileid) $spawn_id
205     }
207     return $spawn_id
210 # Put the Telnet connection to HOSTNAME into binary mode.
212 proc telnet_binary { hostname } {
213     if {[board_info $hostname exists fileid]} {
214         remote_send $hostname "\x1d"
215         remote_expect $hostname 5 {
216             -re "telnet> *$" {}
217             default {}
218         }
219         remote_send $hostname "set binary\n"
220         remote_expect $hostname 5 {
221             -re "Format is .*telnet> *$" {
222                 remote_send $hostname "toggle binary\n"
223                 exp_continue
224             }
225             -re "Negotiating network ascii.*telnet> *$" {
226                 remote_send $hostname "toggle binary\n"
227                 exp_continue
228             }
229             -re {Negotiating binary.*[\r\n].*$} { }
230             -re "binary.*unknown argument.*telnet> *$" {
231                 remote_send $hostname "mode character\n"
232             }
233             -re {Already operating in binary.*[\r\n].*$} { }
234             timeout {
235                 warning "Never got binary response from telnet."
236             }
237         }
238     }