Add tests for mixed C/C++ unit test library usage
[dejagnu.git] / lib / telnet.exp
blob4ffa9260358b7dd01f33b291141d2f78d45ae01d
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, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 # Connect to HOSTNAME using Telnet. ARGS is a list of options.
21 # Currently the only supported option is "raw". Sets the fileid field
22 # in the config array and returns -1 for error or the spawn id.
24 proc telnet_open { hostname args } {
25 global verbose
26 global spawn_id
27 global timeout
28 global board_info
30 set raw 0
31 foreach arg $args {
32 switch -- $arg {
33 "raw" { set raw 1 }
37 set port 23
38 if {[board_info $hostname exists name]} {
39 set connhost [board_info $hostname name]
40 } else {
41 set connhost $hostname
44 if {[board_info $connhost exists hostname]} {
45 set hostname [board_info $connhost hostname]
48 if {[file exists /usr/kerberos/bin/telnet]} {
49 set telnet /usr/kerberos/bin/telnet
50 } else {
51 set telnet telnet
54 # Instead of unsetting it, let's return it. One connection at a
55 # time, please.
56 if {[board_info $connhost exists fileid]} {
57 return [board_info $connhost fileid]
60 # Get the hostname and port number from the config array.
61 if {[board_info $connhost exists netport]} {
62 set type $hostname
63 set hosttmp [split [board_info $connhost netport] ":"]
64 set hostname [lindex $hosttmp 0]
65 if { [llength $hosttmp] > 1 } {
66 set port [lindex $hosttmp 1]
68 unset hosttmp
69 } else {
70 set type target
73 if {[board_info $connhost exists shell_prompt]} {
74 set shell_prompt [board_info $connhost shell_prompt]
76 if {![info exists shell_prompt]} {
77 # If no prompt, then set it to something generic.
78 set shell_prompt ".*> "
81 set tries 0
82 set result -1
83 set need_respawn 1
84 verbose "Starting a telnet connection to $hostname:$port $shell_prompt" 2
85 while { $result < 0 && $tries <= 3 } {
86 if { $need_respawn } {
87 set need_respawn 0
88 spawn $telnet $hostname $port
90 expect {
91 "Trying " {
92 exp_continue
94 -re "$shell_prompt.*$" {
95 verbose "Got prompt\n"
96 set result 0
98 -re "nt Name:|ogin:" {
99 if {[board_info $connhost exists telnet_username]} {
100 exp_send "[board_info $connhost telnet_username]\n"
101 exp_continue
103 if {[board_info $connhost exists username]} {
104 exp_send "[board_info $connhost username]\n"
105 exp_continue
107 perror "telnet: need to login"
108 break
110 "assword:" {
111 if {[board_info $connhost exists telnet_password]} {
112 exp_send "[board_info $connhost telnet_password]\n"
113 exp_continue
115 if {[board_info $connhost exists password]} {
116 exp_send "[board_info $connhost password]\n"
117 exp_continue
119 perror "telnet: need a password"
120 break
122 -re {advance.*y/n.*\?} {
123 exp_send "n\n"
124 exp_continue
126 -re {([Aa]dvanced|[Ss]imple) or ([Ss]imple|[Aa]dvanced)} {
127 exp_send "simple\n"
128 exp_continue
130 "Connected to" {
131 exp_continue
133 "unknown host" {
134 exp_send "\003"
135 perror "telnet: unknown host"
136 break
138 "VxWorks Boot" {
139 exp_send "@\n"
140 sleep 20
141 exp_continue
143 -re {Escape character is.*\.[\r\n]} {
144 if { $raw || [board_info $connhost exists dont_wait_for_prompt] } {
145 set result 0
146 } else {
147 if {[board_info $connhost exists send_initial_cr]} {
148 exp_send "\n"
150 exp_continue
153 "has logged on from" {
154 exp_continue
156 "You have no Kerberos tickets" {
157 warning "telnet: no kerberos Tickets, please kinit"
158 break
160 -re "Connection refused.*$" {
161 catch "exp_send \"\003\"" foo
162 sleep 5
163 warning "telnet: connection refused."
165 -re "Sorry, this system is engaged.*" {
166 exp_send "\003"
167 warning "telnet: already connected."
169 "Connection closed by foreign host.*$" {
170 warning "telnet: connection closed by foreign host."
171 break
173 -re {[\r\n]+} {
174 exp_continue
176 timeout {
177 exp_send "\n"
179 eof {
180 warning "telnet: got unexpected EOF from telnet."
181 catch close
182 catch wait
183 set need_respawn 1
184 sleep 5
187 incr tries
190 # We look for this here again cause it means something went wrong,
191 # and it doesn't always show up in the expect in buffer till the
192 # server times out.
193 if {[info exists expect_out(buffer)]} {
194 if {[regexp "assword:|ogin:" $expect_out(buffer)]} {
195 perror "telnet: need to supply a login and password."
198 if { $result < 0 } {
199 catch close
200 catch wait
201 set spawn_id -1
203 if { $spawn_id >= 0 } {
204 verbose "setting board_info($connhost,fileid) to $spawn_id" 3
205 set board_info($connhost,fileid) $spawn_id
208 return $spawn_id
211 # Put the Telnet connection to HOSTNAME into binary mode.
213 proc telnet_binary { hostname } {
214 if {[board_info $hostname exists fileid]} {
215 remote_send $hostname "\x1d"
216 remote_expect $hostname 5 {
217 -re "telnet> *$" {}
218 default {}
220 remote_send $hostname "set binary\n"
221 remote_expect $hostname 5 {
222 -re "Format is .*telnet> *$" {
223 remote_send $hostname "toggle binary\n"
224 exp_continue
226 -re "Negotiating network ascii.*telnet> *$" {
227 remote_send $hostname "toggle binary\n"
228 exp_continue
230 -re {Negotiating binary.*[\r\n].*$} { }
231 -re "binary.*unknown argument.*telnet> *$" {
232 remote_send $hostname "mode character\n"
234 -re {Already operating in binary.*[\r\n].*$} { }
235 timeout {
236 warning "Never got binary response from telnet."