aboutsummaryrefslogtreecommitdiff
path: root/contrib/bluegnu2.0.3/lib/remote.exp
blob: a7fdf9494028bc2057d2d93d621b11d6b82fecbc (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
# Copyright (C) 92, 93, 94, 95, 1996 Free Software Foundation, Inc.

# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

# Please email any bugs, comments, and/or additions to this file to:
# bug-dejagnu@prep.ai.mit.edu

# This file was written by Rob Savoye. (rob@welcomehome.org)

# these just need to be initialized
# FIXME: This is deprecated (we should have no knowledge of global `shell_id').
# Remove at some point.
set shell_id   	0

#
# Open a connection to a remote host or target. This requires the target_info
# array be filled in with the proper info to work. The old variables are also
# still functional.
#
# type is either "host" or "target". The default is target if no type is supplied.
# It returns the spawn id of the process that is the connection.
#	
proc remote_open { args } {
    global target_info
    global connectmode
    global targetname
    global serialport
    global netport
    global reboot
    global shell_id
    global spawn_id

    if { [llength $args] == 0 } {
	set type "target"
    } else {
	set type $args
    }

    # set the current connection
    if [info exists target_info(${type},name)] {	
	if { $target_info(${type},name) != "" } {	
	    if { [info proc push_$type] != "" } {
		push_$type $target_info(${type},name)
	    }
	} else {
	    warning "Couldn't push target, name was NULL"
	}
    }

    if [info exists target_info(${type},connect)] {
	set connect_prog $target_info(${type},connect)
    } else {
	if [info exists connectmode] {
	    set connect_prog $connectmode
	} else {
	    perror "No connectmode specified"
	    set shell_id -1
	    return $shell_id
	}
    }

    # reboot the machine if we neeed to, typically by using an x10 controller.
    if $reboot {
	if { [info procs "reboot_hook"] != "" } {
	    reboot_hook
	}
    }

    set shell_id [$connect_prog $type]

    if [info exists target_info] {
	set target_info(${type},fileid) $shell_id
	if [info exists target_info(${type},name)] {
	    set target_info($target_info(${type},name),fileid) $shell_id
	}
    }
    return $shell_id
}

#
# Close the remote connection.
#	shell_id - This is the id number returned by the any of the connection
#	procedures, or an index into one of the arrays.
#
proc remote_close { arg } {
    # get the type of connection, host or target
    if [expr [string match "host" $arg] || [string match "target" $arg]] {
	set type $arg
	if [info exists target_info(${type},fileid)] {
	    set shell_id $target_info(${type},fileid)
	} else {
	    perror "No shell id for to close"
	}
    } else {
	set shell_id $arg
    }

    verbose "Closing the remote shell $shell_id" 2
    catch "close -i $shell_id"
    catch "wait -i $shell_id"

    return 0
}


# Most of these procedures try to establish the connection 3 times before
# returning. If $verbose is set to a value of 2 or greater, then error
# messages will appear for each attempt. If there is an error that
# can't be recovered from, it returns a -1. If the connection is
# established, it returns the shell's process number returned by the
# tcl command spawn.
# Hostname refers to the entry in /etc/hosts for this target. The
# procedure's name is the same as its unix counterpart.
# The final argument is the type of connection to establish, the default
# is the target. This can also be passed as the second arg or the third.

#
# Connect using telnet. This takes two arguments. The first one is the
# hostname, and the second is the optional port number. This sets
# the fileid field in the config array, and returns -1 for error, or the
# spawn id.
#
proc telnet { args } {
    global verbose
    global connectmode
    global shell_prompt
    global spawn_id
    global timeout
    global errno

    set hostname [lindex $args 0]

    # get the port number
    if { [llength $args] > 1 } {
	set port [lindex $args 1]
    } else {
        set port 23
    }

    # get the hostname and port number from the config array
    if [expr [string match "host" $hostname] || [string match "target" $hostname]] {
	set type $hostname
	set hosttmp [split $target_info($type,netport) ":"]
	set hostname [lindex $hosttmp 0]
	if { [llength $hosttmp] > 1 } {
	    set port [lindex $hosttmp 1]
	}
	unset hosttmp
	if [info exists target_info($type,prompt)] {
	    set shell_prompt $target_info($type,prompt)
	}
    } else {
	set type target
    }
    if ![info exists shell_prompt] {	# if no prompt, then set it to something generic
	set shell_prompt ".*> "
    }

    set tries 0
    set result -1
    verbose "Starting a telnet connection to $hostname:$port" 2
    spawn telnet $hostname $port
    exp_send "\r\n"
    while { $tries <= 3 } {
	catch expect {
	    "ogin:" {
		perror "telnet: need to login"
		break
	    }
	    "assword:" {
		perror "telnet: need a password"
		break
	    }
	    -re ".*$shell_prompt.*$" {
		verbose "Got prompt\n"
		set result 0
	    }
	    "Connected to" {
		exp_continue
	    }
	    -re "\[\r\n\]*" {
		exp_continue
	    }
	    "unknown host" {
		exp_send "\003"
		perror "telnet: unknown host"
		break
	    }
	    "Escape character is" {
		exp_send "\r\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.*$" {
		exp_send "\003"
		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
	    }
	    timeout {
		exp_send "\003"
		warning "telnet: timed out trying to connect."
	    }
	    eof {
		perror "telnet: got unexpected EOF from telnet."
		break
	    }
	}
	incr tries
    }
    # we look for this hear 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
#	perror "telnet: couldn't connect after $tries tries."
	set spawn_id -1
    }
    set target_info(target,fileid) $spawn_id
    if [info exists target_info(target,name)] {
	set target_info($target_info(target,name),fileid) $spawn_id
    }
    return $spawn_id
}

#
# Connect to hostname using rlogin. The global RLOGIN
# is the name of the actual rlogin program. This is for systems
# using rlogin to braindead targets that don't support kerboros.
# It returns either the spawn_id or a -1.
# The final argument is the type of connection to establish, the default
# is the target. This can also be passed as the second arg or the third.
#
proc rlogin { arg } {
    global spawn_id
    global target_info
    global RLOGIN
    global errno

    set tries 0
    set result -1

    # get the hostname and port number from the config array
    if [expr [string match "host" $arg] || [string match "target" $arg]] {
	set type $arg
	set hostname [lindex [split $target_info(${type},netport) ":"] 0]
	if [info exists target_info($type,prompt)] {
	    set shell_prompt $target_info($type,prompt)
	}
    } else {
	set hostname $arg
	set type target
    }
    if ![info exists shell_prompt] {	# if no prompt, then set it to something generic
	set shell_prompt ".*> "
    }

    # get the right version of rlogin
    if ![info exists RLOGIN] {
	set RLOGIN rlogin
    }

    # start connection and store the spawn_id
    verbose "Opening a $RLOGIN connection to $hostname" 2
    spawn $RLOGIN $hostname
    if { $spawn_id < 0 } {
	perror "invalid spawn id from rlogin"
	return
    }
    set target_info(${type},fileid) $spawn_id
    if [info exists target_info($type,name)] {
	set target_info($target_info($type,name),fileid) $spawn_id
    }

    # try to connect to the target. We give up after 3 attempts. At one point
    # we used to look for the prompt, but we may not know what it looks like.
    while { $tries <= 3 } {
	expect {
	    -re ".*$shell_prompt.*$" {
		verbose "Got prompt\n"
		set result 0
		break
	    }
	    -re "TERM = .*\\)\[ ]*$" {
		send "dumb\r\n"
		expect {
		    "Terminal type is*$" {
			verbose "rlogin: set the terminal to dumb" 2
		    }
		    default {
			warning "rlogin: couldn't set terminmal type"
		    }
		}
		set result 10
		break
	    }
	    "unknown host" {
		perror "rlogin: unknown host"
		break
	    }
	    "has logged on from" {
		exp_continue
	    }
	    "Terminal type is" {
		verbose "rlogin: connected, got terminal prompt" 2
		set result 0
		break
	    }
	    -re "Maximum number of users already logged in.*$" {
		warning "rlogin: maximum number of users already logged in"
	    }
	    -re "Sorry, shell is locked.*Connection closed.*$" {
		warning "rlogin: lready connected."
	    }
	    -re "Sorry, this system is engaged.*Connection closed.*$" {
		warning "rlogin: system engaged."
	    }
	    -re "Kerberos rcmd failed.*$" {
		warning "rlogin: Kerberos rcmd failed, please kinit"
		catch close
		catch wait
       		break
	    }
	    -re "trying normal rlogin.*$" {
		warning "rlogin: trying normal rlogin."
		catch close
		catch wait
		break
	    }
	    -re "unencrypted connection.*$" {
		warning "rlogin: unencrypted connection, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "isn't registered for Kerberos.*service.*$" {
		warning "rsh: isn't registered, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "You have no Kerberos tickets.*$" {
		warning "rlogin: No kerberos Tickets, please kinit"
		catch close
		catch wait
		break
	    }
	    timeout	{
		warning "rlogin: timed out trying to connect."
	    }
	    eof {
		perror "rlogin: got EOF while trying to connect."
		break
	    }
	}
	incr tries
    }

    # if the error was fatal, there's nothing to send to
    catch { send "\r\n" } tmp
    if [string match "*invalid spawn id*" $tmp] {
	perror "Couldn't rlogin to $hostname, fatal error."
	catch "close $spawn_id"
	set target_info(${type},fileid) $spawn_id
	if [info exists target_info(${type},name)] {
	    set target_info($target_info(${type},name),fileid) $spawn_id
	}
	return $spawn_id
    }
    expect {
	"\r\n*$" {
	    exp_continue
	}
	-re "\[- A-Za-z0-9\.\;\"\_\:\'\`\(\)\!\#\=\+\?\&\*]+.*$" {
	    # this is kinda gross, but if we get most any legit ascii
	    # text we figure we connected. Others tests later will
	    # determine if the connection actually works.
	    verbose "We got some text" 2
	}
    }

    # see if we maxed out on errors
    if { $result < 0 } {
	catch close
	catch wait
#	perror "rlogin: couldn't rlogin to $hostname, Too many errors"
	catch "close $spawn_id"
	set spawn_id -1
	set target_info(${type},fileid) $spawn_id
	if [info exists target_info(${type},name)] {
	    set target_info($target_info(${type},name),fileid) $spawn_id
	}
    } else {
	verbose "rlogin: connected to $hostname" 2
    }

    return $spawn_id
}

#
# Connect to hostname using rsh
#
proc rsh { arg } {
    global spawn_id
    global target_info
    global RSH
    global errno

    set tries 0
    set result -1

    # get the hostname and port number from the config array
    if [expr [string match "host" $arg] || [string match "target" $arg]] {
	set type $arg
	set hostname [lindex [split $target_info(${type},netport) ":"] 0]
	if [info exists target_info(${type},prompt)] {
	    set shell_prompt $target_info(${type},prompt)
	}
    } else {
	set hostname $arg
	set type target
    }
    if ![info exists shell_prompt] {	# if no prompt, then set it to something generic
	set shell_prompt ".*> "
    }

    if ![info exists RSH] {
	set RSH rsh
    }
    spawn $RSH $hostname
    if { $spawn_id < 0 } {
	perror "invalid spawn id from rsh"
	return
    }
    set target_info(${type},fileid) $spawn_id
    if [info exists target_info(${type},name)] {
	set target_info($target_info(${type},name),fileid) $spawn_id
    }
    if [info exists target_info(${type},prompt)] {
	set prompt $target_info(${type},prompt)
    }
    send "\r\n"
    while { $tries <= 3 } {
	expect {
	    -re ".*$shell_prompt.*$" {
		verbose "Got prompt\n"
		set result 0
		break
	    }
	    -re "TERM = .*$" {
		warning "Setting terminal type to vt100"
		set result 0
		send "vt100\n"
		break
	    }
	    "unknown host" {
		exp_send "\003"
		perror "telnet: unknown host"
		break
	    }
	    "has logged on from" {
		exp_continue
	    }
	    -re "isn't registered for Kerberos.*service.*$" {
		warning "rsh: isn't registered for Kerberos, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "Kerberos rcmd failed.*$" {
		warning "rsh: Kerberos rcmd failed, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "You have no Kerberos tickets.*$" {
		warning "rsh: No kerberos Tickets, please kinit"
		catch close
		catch wait
		break
	    }
	    "Terminal type is" {
		verbose "rsh: connected, got terminal prompt" 2
		set result 0
		break
	    }
	    -re "trying normal rlogin.*$" {
		warning "rsh: trying normal rlogin."
		catch close
		catch wait
		break
	    }
	    -re "unencrypted connection.*$" {
		warning "rsh: unencrypted connection, please kinit"
		catch close
		catch wait
		break
	    }
	    -re "Sorry, shell is locked.*Connection closed.*$" {
		warning "rsh: already connected."
	    }
	    timeout {
	       warning "rsh: timed out trying to connect."
	    }
	    eof {
		perror "rsh: got EOF while trying to connect."
		break
	    }
	}
	incr tries
    }

    if { $result < 0 } {
#	perror "rsh: couldn't connect after $tries tries."
	set spawn_id -1
    }
    set target_info(${type},fileid) $spawn_id
    if [info exists target_info(${type},name)] {
	set target_info($target_info(${type},name),fileid) $spawn_id
    }
    return $spawn_id
}

#
# Download an executable to a network neighbor
#
# DEST is assumed to already contain the nodename.
# Returns the status returned by the rcp command.
#
proc rcp_download { src dest } {
    set status [catch "exec rcp $src $dest" output]
    if { $status == 0 } {
	verbose "Copied $src to $dest" 2
    } else {
	verbose "Download to $dest failed, $output."
    }
    return $status
}

#
# This proc is deprecated.  Please use `execute_anywhere' instead.
#
# Execute a program on the remote system using rsh
#
# SYSTEM is the host name of the system to run the program on.
# CMD is the program to run (including path) and any arguments.
# The result is a list of two elements.
# First element: 0 for success, 1 for failure, -1 for comms failure.
# Second element: program output (success/failure) or error message (comms).
#
proc rsh_exec { system cmd } {
    verbose "Executing $system:$cmd" 3
    # If CMD sends any output to stderr, exec will think it failed.  More often
    # than not that will be true, but it doesn't catch the case where there is
    # no output but the exit code is non-zero.  The "2>&1" is done on the
    # remote system and is not a special flag for `exec'.
    set status [catch "exec rsh $system $cmd 2>&1 \\; echo XYZ$?ZYX" output]
    # `status' doesn't mean much here other than rsh worked ok.
    # What we want is whether $cmd ran ok.
    if { $status != 0 } {
	regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
	return [list -1 "rsh to $system failed for $cmd, $output"]
    }
    regexp "XYZ(\[0-9\]*)ZYX" $output junk status
    verbose "rsh_exec: status:$status text:$output" 4
    if { $status == "" } {
	return [list -1 "Couldn't parse rsh output, $output."]
    }
    regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
    # Delete one trailing \n because that is what `exec' will do and we want
    # to behave identical to it.
    regsub "\n$" $output "" output
    return [list [expr $status != 0] $output]
}

#
# Connect to using tip
# port - must be a name from /etc/remote, or "host" or "target".
# returns -1 if it failed, the spawn_id if it worked
#
proc tip { arg } {
    global verbose
    global shell_prompt
    global target_info
    global spawn_id

    set tries 0
    set result -1

    if [expr [string match "host" $arg] || [string match "target" $arg]] {
	set port $target_info(${type},target)
	if [info exists target_info(${type},prompt)] {
	    set shell_prompt $target_info(${type},prompt)
	}
    } else {
	set port $arg
    }
    if ![info exists shell_prompt] {	# if no prompt, then set it to something generic
	set shell_prompt ".*> "
    }

    spawn tip -v $port
    if { $spawn_id < 0 } {
	perror "invalid spawn id from tip"
	return -1
    }
    set target_info(target,fileid) $spawn_id
    set target_info($target_info(target,name),fileid) $spawn_id
    expect {
	-re ".*connected.*$" {
	    send "\r\n"
	    expect {
		-re ".*$shell_prompt.*$" {
		    verbose "Got prompt\n"
		    set result 0
		    incr tries
		}
		timeout {
		    warning "Never got prompt."
		    set result -1
		    incr tries
		    if $tries<=2 {
			exp_continue
		    }
		}
	    }
	}
	-re "all ports busy.*$" {
	    set result -1
	    perror "All ports busy."
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	-re "Connection Closed.*$" {
	    perror "Never connected."
	    set result -1
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	-re ".*: Permission denied.*link down.*$" {
	    perror "Link down."
	    set result -1
	    incr tries
	}
	timeout	{
	    perror "Timed out trying to connect."
	    set result -1
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	eof {
	    perror "Got unexpected EOF from tip."
	    set result -1
	    incr tries
	}
    }

    send "\n~s"
    expect {
	"~\[set\]*" {
	    verbose "Setting verbose mode" 1
	    send "verbose\n\n\n"
	}	
    }

    if { $result < 0 } {
	perror "Couldn't connect after $tries tries."
	set target_info(${type},fileid) -1
	set target_info($target_info(${type},name),fileid) -1
	return -1
    } else {
	set target_info(${type},fileid) $spawn_id
	set target_info($target_info(${type},name),fileid) $spawn_id
	return $spawn_id
    }
}

#
# Downloads using the ~put command under tip
#     arg - is a full path name to the file to download
#     returns 1 if an error occured, otherwise it returns
#	  the spawn_id.
#
proc tip_download { shell_id file } {
    global verbose
    global decimal
    global shell_prompt
    global expect_out

    set result 1
    if ![file exists $file] {
	perror "$file doesn't exist."
	return 1
    }

    send -i $shell_id "\n~p"
    expect {
	-i $shell_id "~\[put\]*" {
	    verbose "Downloading $file, please wait" 1
	    send -i $shell_id "$file\n"
	    set timeout 50
	    expect {
		-i $shell_id -re ".*$file.*$" {
		    exp_continue
		}
		-i $shell_id -re ".*lines transferred in.*minute.*seconds.*$shell_prompt.*$" {
		    verbose "Download $file successfully" 1
		    set result 0
		}
		-i $shell_id -re ".*Invalid command.*$shell_prompt$" {
		    warning "Got an Invalid command to the monitor"
		}
		-i $shell_id -re ".*$decimal\r" {
		    if [info exists expect_out(buffer)] {
			verbose "$expect_out(buffer)"
			exp_continue
		    }
		}
		-i $shell_id timeout {
		    perror "Timed out trying to download."
		    set result 1
		}
	    }
	}	
	timeout {
	    perror "Timed out waiting for response to put command."
	}
    }	
    set timeout 10
    return $result
}

#
# Connect to using kermit
#     args - first is the device name, ie. /dev/ttyb
#         second is the optional baud rate. If this is "host" or "target" the
#	  config array is used instead.
#     returns -1 if it failed, otherwise it returns
#         the spawn_id.
#
proc kermit { args } {
    global verbose
    global shell_prompt
    global spawn_id

    if { [llength $args] == 1 } {
	set baud 9600
    } else {
	set baud [lindex $args 1]
    }

    if [expr [string match "host" [lindex $args 0]] || [string match "target" [lindex $arg 0]]] {
	set device $target_info(${type},serial)
	if [info exists target_info(${type},baud)] {
	    set baud $target_info(${type},baud)
	}
    } else {
	set device [lindex $args 0]
    }

    set tries 0
    set result -1
    spawn kermit -l $device -b $baud
    if { $spawn_id < 0 } {
	perror "invalid spawn id from kermit"
	return -1
    }
    set target_info(${type},fileid) $spawn_id
    set target_info($target_info(${type},name),fileid) $spawn_id
    expect {
	-re ".*ermit.*>.*$" {
	    send "c\n"
	    expect {
		-re ".*Connecting to $port.*Type the escape character followed by C to.*$" {
		    verbose "Got prompt\n"
		    set result 0
		    incr tries
		}
		timeout {
		    warning "Never got prompt."
		    set result -1
		    incr tries
		    if { $tries <= 2 } {
			exp_continue
		    }
		}
	    }
	}
	-re "Connection Closed.*$" {
	    perror "Never connected."
	    set result -1
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	timeout			{
	    warning "Timed out trying to connect."
	    set result -1
	    incr tries
	    if { $tries<=2 } {
		exp_continue
	    }
	}
    }

    if { $result < 0 } {
	perror "Couldn't connect after $tries tries."
	set target_info(${type},fileid) -1
	set target_info($target_info(${type},name),fileid) -1
	return -1
    } else {
	set target_info(${type},fileid) $spawn_id
	set target_info($target_info(${type},name),fileid) $spawn_id
	return $spawn_id
    }
}

#
# exit the remote shell
#
# ??? This proc is deprecated.  Please use `remote_close' instead.
proc exit_remote_shell { shell_id } {
    return [remote_close $shell_id]
}

#
# Download a file using stdin. This will download a file
# regardless of whether rlogin, telnet, tip, or kermit was
# used to establish the connection.
#
proc download { args } {
    global spawn_id
    global verbose

    set file [lindex $args 0]

    if { [llength $args] > 1 } {
        set shellid [lindex $args 1]
    } else {
        set shellid $spawn_id
    }

    set lines 0
    set fd [open $file r]
    while { [gets $fd cur_line] >= 0 } {
        set errmess ""
        catch "send -i $shellid \"$cur_line\"" errmess
        if [string match "write\(spawn_id=\[0-9\]+\):" $errmess] {
            perror "sent \"$command\" got expect error \"$errmess\""
            catch "close $fd"
            return -1
        }
        verbose "." 2
        verbose "Sent $cur_line" 3
	incr lines
    }
    verbose "$lines lines downloaded"
    close $fd
    return 0
}