1 # Copyright
(C
) 2016-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.
19 #
Connect using ssh
(1).
21 set ssh_initialized
"no"
22 set ssh_useropts
" -o ControlPersist=yes -o ControlMaster=auto -o ControlPath=\"/tmp/ssh-%r@%h:%p\""
24 # Default to the ssh and scp in the user
's path.
28 # Download SRCFILE to DESTFILE on DESTHOST.
30 proc ssh_download {desthost srcfile destfile} {
31 global SSH SCP ssh_initialized timeout
39 if {[board_info $desthost exists scp_prog]} {
40 set SCP [board_info $desthost scp_prog]
43 if {[board_info $desthost exists ssh_prog]} {
44 set SSH [board_info $desthost ssh_prog]
47 # The default user name is the person running the tests
48 if {[board_info $desthost exists username]} {
49 set ssh_user "[board_info $desthost username]@"
52 if {[board_info $desthost exists ssh_opts]} {
53 append ssh_useropts " [board_info $desthost ssh_opts]"
56 # The default SSH port is 22
57 if {[board_info $desthost exists port]} {
58 set ssh_port "[board_info $desthost port]"
63 if {[board_info $desthost exists name]} {
64 set name [board_info $desthost name]
67 if {[board_info $desthost exists hostname]} {
68 set hostname [board_info $desthost hostname]
70 set hostname $desthost
73 append ssh_useropts " -o ControlPersist=yes -o ControlMaster=auto -o ControlPath=/tmp/ssh-%r@%h:%p"
75 set ret [local_exec "$SCP -P $ssh_port $ssh_useropts $srcfile $ssh_user$hostname:$destfile" "" "" $timeout]
76 set status [lindex $ret 0]
77 set output [lindex $ret 1]
79 set ssh_initialized "yes"
80 verbose "Copied $srcfile to $desthost:$destfile" 2
83 verbose "Download via ssh to $desthost failed."
88 proc ssh_upload {desthost srcfile destfile} {
91 if {[board_info $desthost exists scp_prog]} {
92 set SCP [board_info $desthost scp_prog]
95 if {[board_info $desthost exists username]} {
96 set ssh_user "[board_info $desthost username]@"
101 if {[board_info $desthost exists name]} {
102 set desthost [board_info $desthost name]
105 if {[board_info $desthost exists hostname]} {
106 set desthost [board_info $desthost hostname]
109 set status [catch "exec $SCP $ssh_user$desthost:$srcfile $destfile" output]
110 if { $status == 0 } {
111 verbose "Copied $desthost:$srcfile to $destfile" 2
114 verbose "Upload from $desthost failed, $output."
119 # Execute CMD on BOARDNAME.
121 proc ssh_exec { boardname program pargs inp outp } {
131 verbose "Executing on $boardname: $program $pargs"
133 if {![board_info $boardname exists ssh_prog]} {
136 set SSH [board_info $boardname ssh_prog]
139 if {[board_info $boardname exists username]} {
140 set ssh_user "[board_info $boardname username]@"
145 if {[board_info $boardname exists ssh_useropts]} {
146 append ssh_useropts " [board_info $boardname ssh_opts]"
149 if {[board_info $boardname exists name]} {
150 set boardname [board_info $boardname name]
153 if {[board_info $boardname exists hostname]} {
154 set hostname [board_info $boardname hostname]
156 set hostname $boardname
159 if {[board_info $boardname exists port]} {
160 append ssh_useropts " -p [board_info $boardname port]"
163 append ssh_useropts " -o ControlPersist=yes -o ControlMaster=auto -o ControlPath=\"/tmp/ssh-%r@%h:%p\""
165 # If CMD sends any output to stderr, exec will think it failed.
166 # More often than not that will be true, but it doesn't catch the
167 # case where there is no output but the exit code is non
-zero.
172 # We use
&& here
, as otherwise the echo always works
, which makes it look
173 # like execution succeeded when in reality it failed.
174 set ret
[local_exec
"$SSH $ssh_useropts $ssh_user$hostname sh -c '$program $pargs 2>&1 && echo XYZ\\\${?}ZYX \\; rm -f $program'" $inp $outp $timeout]
175 set status [lindex $ret
0]
176 set output
[lindex $ret
1]
178 verbose
"$SSH status is $status, output is $output"
180 # `
status' doesn't mean much here other than ssh worked ok.
181 # What we want is whether $
program ran ok.
Return $
status
182 #
if the
program timed out
, status will be
1 indicating that
183 # ssh ran and failed.
If ssh fails
, we will
get FAIL rather
184 # than UNRESOLVED
- this will help the problem be noticed.
185 if { $
status != 0 } {
186 regsub
"XYZ(\[0-9\]*)ZYX\n?" $output "" output
187 return [list $
status "$SSH to $boardname failed for $program, $output"]
189 if { [regexp
"XYZ(\[0-9\]*)ZYX" $output junk status] == 0 } {
192 verbose
"ssh_exec: status:$status text:$output" 4
193 if { $
status eq
"" } {
194 return [list
-1 "Couldn't parse $SSH output, $output."]
196 regsub
"XYZ(\[0-9\]*)ZYX\n?" $output "" output
197 return [list
[expr
{$
status != 0}] $output
]
200 proc ssh_close
{ desthost
} {
201 global SSH ssh_initialized
203 verbose
"Closing the SSH connection to $desthost"
212 if {[board_info $desthost
exists username
]} {
213 set ssh_useropts
"-l [board_info $desthost username]"
214 set ssh_user
"[board_info $desthost username]@"
220 if {[board_info $desthost
exists hostname
]} {
221 set hostname
[board_info $desthost hostname
]
224 if {[board_info $desthost
exists ssh_opts
]} {
225 append ssh_useropts
" [board_info $desthost ssh_opts]"
228 if {[board_info $desthost
exists port
]} {
229 set ssh_port
" -p [board_info $desthost port]"
234 set args "$ssh_user$hostname $ssh_port"
236 # Kill the remote server
237 set status [catch
"exec ssh $ssh_port -o ControlPath=/tmp/ssh-%r@%h:%p -O exit $args"]
238 set ssh_initialized
"no"