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 } {
37 if {[board_info $hostname exists name]} {
38 set connhost [board_info $hostname name]
40 set connhost $hostname
43 if {[board_info $connhost exists hostname]} {
44 set hostname [board_info $connhost hostname]
47 if {[file exists /usr/kerberos/bin/telnet]} {
48 set telnet /usr/kerberos/bin/telnet
53 # Instead of unsetting it, let's return it. One connection at a
55 if {[board_info $connhost exists fileid]} {
56 return [board_info $connhost fileid]
59 # Get the hostname and port number from the config array.
60 if {[board_info $connhost exists netport]} {
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]
72 if {[board_info $connhost exists shell_prompt]} {
73 set shell_prompt [board_info $connhost shell_prompt]
75 if {![info exists shell_prompt]} {
76 # If no prompt, then set it to something generic.
77 set shell_prompt ".*> "
83 verbose "Starting a telnet connection to $hostname:$port $shell_prompt" 2
84 while { $result < 0 && $tries <= 3 } {
85 if { $need_respawn } {
87 spawn $telnet $hostname $port
93 -re "$shell_prompt.*$" {
94 verbose "Got prompt\n"
97 -re "nt Name:|ogin:" {
98 if {[board_info $connhost exists telnet_username]} {
99 exp_send "[board_info $connhost telnet_username]\n"
102 if {[board_info $connhost exists username]} {
103 exp_send "[board_info $connhost username]\n"
106 perror "telnet: need to login"
110 if {[board_info $connhost exists telnet_password]} {
111 exp_send "[board_info $connhost telnet_password]\n"
114 if {[board_info $connhost exists password]} {
115 exp_send "[board_info $connhost password]\n"
118 perror "telnet: need a password"
121 -re {advance.*y/n.*\?} {
125 -re {([Aa]dvanced|[Ss]imple) or ([Ss]imple|[Aa]dvanced)} {
134 perror "telnet: unknown host"
142 -re {Escape character is.*\.[\r\n]} {
143 if { $raw || [board_info $connhost exists dont_wait_for_prompt] } {
146 if {[board_info $connhost exists send_initial_cr]} {
152 "has logged on from" {
155 "You have no Kerberos tickets" {
156 warning "telnet: no kerberos Tickets, please kinit"
159 -re "Connection refused.*$" {
160 catch "exp_send \"\003\"" foo
162 warning "telnet: connection refused."
164 -re "Sorry, this system is engaged.*" {
166 warning "telnet: already connected."
168 "Connection closed by foreign host.*$" {
169 warning "telnet: connection closed by foreign host."
179 warning "telnet: got unexpected EOF from telnet."
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
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."
202 if { $spawn_id >= 0 } {
203 verbose "setting board_info($connhost,fileid) to $spawn_id" 3
204 set board_info($connhost,fileid) $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 {
219 remote_send $hostname "set binary\n"
220 remote_expect $hostname 5 {
221 -re "Format is .*telnet> *$" {
222 remote_send $hostname "toggle binary\n"
225 -re "Negotiating network ascii.*telnet> *$" {
226 remote_send $hostname "toggle binary\n"
229 -re {Negotiating binary.*[\r\n].*$} { }
230 -re "binary.*unknown argument.*telnet> *$" {
231 remote_send $hostname "mode character\n"
233 -re {Already operating in binary.*[\r\n].*$} { }
235 warning "Never got binary response from telnet."