summaryrefslogtreecommitdiff
path: root/lib/telnet.exp
blob: d0d05c1cf4fd11e89c5a423bc5a21c9938bd1e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
# 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.


# Connect to HOSTNAME using Telnet.  ARGS is a list of options.
# Currently the only supported option is "raw".  Sets the fileid field
# in the config array and returns -1 for error or the spawn id.
#
proc telnet_open { hostname args } {
    global verbose
    global connectmode
    global spawn_id
    global timeout
    global board_info

    set raw 0
    foreach arg $args {
	switch $arg {
	    "raw" { set raw 1 }
	}
    }

    set port 23
    if {[board_info $hostname exists name]} {
	set connhost [board_info $hostname name]
    } else {
	set connhost $hostname
    }

    if {[board_info $connhost exists hostname]} {
	set hostname [board_info $connhost hostname]
    }

    if {[file exists /usr/kerberos/bin/telnet]} {
	set telnet /usr/kerberos/bin/telnet
    } else {
	set telnet telnet
    }

    # Instead of unsetting it, let's return it. One connection at a
    # time, please.
    if {[board_info $connhost exists fileid]} {
	return [board_info $connhost fileid]
    }

    # Get the hostname and port number from the config array.
    if {[board_info $connhost exists netport]} {
	set type $hostname
	set hosttmp [split [board_info $connhost netport] ":"]
	set hostname [lindex $hosttmp 0]
	if { [llength $hosttmp] > 1 } {
	    set port [lindex $hosttmp 1]
	}
	unset hosttmp
    } else {
	set type target
    }

    if {[board_info $connhost exists shell_prompt]} {
        set shell_prompt [board_info $connhost shell_prompt]
    }
    if {![info exists shell_prompt]} {
	# If no prompt, then set it to something generic.
	set shell_prompt ".*> "
    }

    set tries 0
    set result -1
    set need_respawn 1
    verbose "Starting a telnet connection to $hostname:$port $shell_prompt" 2
    while { $result < 0 && $tries <= 3 } {
	if { $need_respawn } {
	    set need_respawn 0
	    spawn $telnet $hostname $port
	}
	expect {
	    "Trying " {
		exp_continue
	    }
	    -re "$shell_prompt.*$" {
		verbose "Got prompt\n"
		set result 0
	    }
	    -re "nt Name:|ogin:" {
		if {[board_info $connhost exists telnet_username]} {
		    exp_send "[board_info $connhost telnet_username]\n"
		    exp_continue
		}
		if {[board_info $connhost exists username]} {
		    exp_send "[board_info $connhost username]\n"
		    exp_continue
		}
		perror "telnet: need to login"
		break
	    }
	    "assword:" {
		if {[board_info $connhost exists telnet_password]} {
		    exp_send "[board_info $connhost telnet_password]\n"
		    exp_continue
		}
		if {[board_info $connhost exists password]} {
		    exp_send "[board_info $connhost password]\n"
		    exp_continue
		}
		perror "telnet: need a password"
		break
	    }
	    -re "advance.*y/n.*\\?" {
		exp_send "n\n"
		exp_continue
	    }
	    -re {([Aa]dvanced|[Ss]imple) or ([Ss]imple|[Aa]dvanced)} {
		exp_send "simple\n"
		exp_continue
	    }
	    "Connected to" {
		exp_continue
	    }
	    "unknown host" {
		exp_send "\003"
		perror "telnet: unknown host"
		break
	    }
	    "VxWorks Boot" {
		exp_send "@\n"
		sleep 20
		exp_continue
	    }
	    -re "Escape character is.*\\.\[\r\n\]" {
		if { $raw || [board_info $connhost exists dont_wait_for_prompt] } {
		    set result 0
		} else {
		    if {[board_info $connhost exists send_initial_cr]} {
			exp_send "\n"
		    }
		    exp_continue
		}
	    }
	    "has logged on from" {
		exp_continue
	    }
	    "You have no Kerberos tickets" {
		warning "telnet: no kerberos Tickets, please kinit"
		break
	    }
	    -re "Connection refused.*$" {
		catch "exp_send \"\003\"" foo
		sleep 5
		warning "telnet: connection refused."
	    }
	    -re "Sorry, this system is engaged.*" {
		exp_send "\003"
		warning "telnet: already connected."
	    }
	    "Connection closed by foreign host.*$" {
		warning "telnet: connection closed by foreign host."
		break
	    }
	    -re "\[\r\n\]+" {
		exp_continue
	    }
	    timeout {
		exp_send "\n"
	    }
	    eof {
		warning "telnet: got unexpected EOF from telnet."
		catch close
		catch wait
		set need_respawn 1
		sleep 5
	    }
	}
	incr tries
    }

    # We look for this here again cause it means something went wrong,
    # and it doesn't always show up in the expect in buffer till the
    # server times out.
    if {[info exists expect_out(buffer)]} {
	if {[regexp "assword:|ogin:" $expect_out(buffer)]} {
	    perror "telnet: need to supply a login and password."
	}
    }
    if { $result < 0 } {
	catch close
	catch wait
	set spawn_id -1
    }
    if { $spawn_id >= 0 } {
	verbose "setting board_info($connhost,fileid) to $spawn_id" 3
	set board_info($connhost,fileid) $spawn_id
    }

    return $spawn_id
}

# Put the Telnet connection to HOSTNAME into binary mode.
#
proc telnet_binary { hostname } {
    if {[board_info $hostname exists fileid]} {
	remote_send $hostname ""
	remote_expect $hostname 5 {
	    -re "telnet> *$" {}
	    default {}
	}
	remote_send $hostname "set binary\n"
	remote_expect $hostname 5 {
	    -re "Format is .*telnet> *$" {
		remote_send $hostname "toggle binary\n"
		exp_continue
	    }
	    -re "Negotiating network ascii.*telnet> *$" {
		remote_send $hostname "toggle binary\n"
		exp_continue
	    }
	    -re "Negotiating binary.*\[\r\n\].*$" { }
	    -re "binary.*unknown argument.*telnet> *$" {
		remote_send $hostname "mode character\n"
	    }
	    -re "Already operating in binary.*\[\r\n\].*$" { }
	    timeout {
		warning "Never got binary response from telnet."
	    }
	}
    }
}