1 # A library of commonly used functions written in expect.
2 # Copyright Brad Henry
<brad@samba.org
> 2006
3 # Released under the GNU GPL version
3 or later.
5 # This function maps a drive letter to a share point.
6 proc map_share
{ remote_prompt share_drive sharepoint username domain password
} {
7 set default_err_str
"Unknown error in function map_share"
8 set err_str $default_err_str
10 set cmd
"net use $share_drive $sharepoint $password /USER:$username@$domain\r\n"
14 "The command completed successfully." {
15 expect_prompt $remote_prompt
18 "The local device name is already in use." {
19 expect_prompt $remote_prompt
20 set err_str
"The device name $share_drive is already in use"
22 "The network name cannot be found." {
23 expect_prompt $remote_prompt
24 set err_str
"Sharepoint $sharepoint could not be found"
27 set err_str
"Function map_share timed out while mapping $share_drive to $sharepoint"
33 # This function unmaps a drive letter from a share point.
34 proc unmap_share
{ remote_prompt share_drive
} {
35 set default_err_str
"Unknown error in function unmap_share"
36 set err_str $default_err_str
38 set cmd
"net use $share_drive /DELETE\r\n"
42 "was deleted successfully." {
43 expect_prompt $remote_prompt
47 expect_prompt $remote_prompt
48 set err_str
"The network connection could not be found while unmapping $share_drive"
51 set err_str
"Function unmap_share timed out while unmapping $share_drive"
57 # This function uses xcopy to copy a
text file from one location
on the
58 # remote windows host to another.
59 proc xcopy_file
{ remote_prompt in_filename out_filename xcopy_options
} {
60 set default_err_str
"Unknown error in function xcopy_file"
61 set err_str $default_err_str
63 set cmd
"xcopy $in_filename $out_filename $xcopy_options\r\n"
67 "(F = file, D = directory)? " {
71 "1 File(s) copied\r\n\r\n" {
72 expect_prompt $remote_prompt
75 "0 File(s) copied\r\n\r\n" {
76 expect_prompt $remote_prompt
77 set err_str $default_err_str
80 set err_str
"Function xcopy_file has timed out while copying $in_filename"
84 "1 File(s) copied\r\n\r\n" {
85 expect_prompt $remote_prompt
88 "0 File(s) copied\r\n\r\n" {
89 expect_prompt $remote_prompt
90 set err_str $default_err_str
93 set err_str
"Function xcopy_file timed out while copying $in_filename"
99 # This function creates a temporary file
on the remote windows host.
100 # The file contents are populated by a
recursive directory listing of
101 # the windows
%HOMEDRIVE
%.
102 proc create_tmp_file
{ remote_prompt filename
} {
103 set default_err_str
"Unknown error in function create_tmp_file"
104 set err_str $default_err_str
106 set cmd
"dir %HOMEDRIVE%\\ /S > $filename\r\n"
113 set err_str
"Function create_tmp_file timed out while creating $filename"
119 # This function compares two files
on the remote windows host.
120 proc compare_files
{ remote_prompt file1 file2
} {
121 set default_err_str
"Unknown error in function compare_files"
122 set err_str $default_err_str
124 set cmd
"fc $file1 $file2\r\n"
127 "FC: no differences encountered\r\n\r\n\r\n" {
128 expect_prompt $remote_prompt
131 "\*\*\*\*\* $file1" {
132 expect_prompt $remote_prompt
133 set err_str
"Files $file1 and $file2 differ"
135 "\*\*\*\*\* $file2" {
136 expect_prompt $remote_prompt
137 set err_str
"Files $file1 and $file2 differ"
140 set err_str
"Function compare_files timed out while comparing files $file1 and $file2"
146 # This function deletes a file
on the remote windows host.
147 proc delete_file
{ remote_prompt filename
} {
148 set default_err_str
"Unknown error in function delete_file"
149 set err_str $default_err_str
151 set cmd
"del $filename\r\n"
155 expect_prompt $remote_prompt
156 set err_str $default_err_str
162 set err_str
"Function delete_file timed oout while deleting $filename"
168 # This function copies a
text file over telnet from the local unix host
169 # to the remote windows host.
170 proc copy_file
{ remote_prompt in_filename out_filename
} {
171 set default_err_str
"Unknown error in function copy_file"
172 set err_str $default_err_str
174 # The octal ASCII code
for Control
-Z is
032.
177 # Open local file and read contents.
178 set in_file
[open $in_filename r
]
179 set in_data
[read $in_file
]
181 # Initiate copy
on remote host.
182 set cmd
"copy con $out_filename\r\n"
185 # Separate $in_data into lines and send to remote host.
186 set out_data
[split $in_data
"\n"]
187 foreach out_line $out_data
{
189 # We might as well
do a unix
-> windows line conversion.
191 # Are we overwriting an existing file?
192 #
If so
, exit so we can handle it.
196 expect_prompt $remote_prompt
197 set err_str
"File exists"
203 set err_str
"Function copy_file timed out while copying $in_filename"
206 if { $err_str
!= "OK" } {
209 set err_str $default_err_str
213 # ^Z
\r to complete the transfer.
218 set err_str
[expect_prompt $remote_prompt
]
221 set err_str $default_err_str
224 expect_prompt $remote_prompt
225 set err_str
"Function copy_file timed out while finishing copy of $in_filename"
231 # This function waits
for the command prompt and reports an error
on
233 proc expect_prompt
{ remote_prompt
} {
234 set default_err_str
"Unknown error occurred while waiting for the command prompt"
235 set err_str $default_err_str
242 set err_str
"Timeout occurred while waiting for the command prompt"
248 # This function will create a telnet login
shell to $remote_host as $username.
249 #
If expected dialogue is not received
, return with a specific error
if one
250 # is recognized. Otherwise
return a generic error indicating the function
252 proc telnet_login
{ remote_prompt remote_host username password
} {
254 set default_err_str
"Unknown error in function telnet_login"
255 set err_str $default_err_str
257 set cmd
"telnet $remote_host\r"
263 "Connection refused" {
264 set err_str
"Connection refused"
267 set err_str
"No route to host"
270 set err_str
"Function telnet_login timed out while waiting for the login prompt"
273 if { $err_str
!= "OK" } {
274 #
Return because something unexpected happened.
278 set err_str $default_err_str
281 set cmd
"$username\r"
288 set err_str
"Function telnet_login timed out while waiting for the password prompt"
291 if { $err_str
!= "OK" } {
294 set err_str $default_err_str
297 set cmd
"$password\r"
304 set err_str
"Telnet login failed"
307 set err_str
"Function telnet_login timed out while waiting for the command prompt"
313 proc create_directory
{ remote_prompt sharepath
} {
315 set default_err_str
"Unknown error in function create_directory"
316 set err_str $default_err_str
318 set cmd
"mkdir $sharepath\r\n"
322 expect_prompt $remote_prompt
323 set err_str
"Directory already exists"
329 expect_prompt $remote_prompt
330 set err_str
"Timeout reached starting create_directory."
336 proc delete_directory
{ remote_prompt sharepath
} {
338 set default_err_str
"Unknown error in function delete_directory"
339 set err_str $default_err_str
341 set cmd
"rmdir /S /Q $sharepath\r\n"
344 "Access is denied." {
345 expect_prompt $remote_prompt
346 set err_str
"Directory access is denied"
352 expect_prompt $remote_prompt
353 set err_str
"Timeout reached in delete_directory"
359 proc create_share
{ remote_prompt username sharepath sharename
} {
361 set default_err_str
"Unknown error in function create_share"
362 set err_str $default_err_str
364 set cmd
"net share $sharename=$sharepath /GRANT:$username,FULL\r\n"
367 "was shared successfully." {
368 set err_str
[expect_prompt $remote_prompt
]
370 "NET HELPMSG 2118." {
371 expect_prompt $remote_prompt
372 set err_str
"The name has already been shared"
375 set err_str $default_err_str
378 expect_prompt $remote_prompt
379 set err_str
"Timeout reached in create_share"
385 proc delete_share
{ remote_prompt sharename
} {
387 set default_err_str
"Unknown error in function delete_share"
388 set err_str $default_err_str
390 set cmd
"net share $sharename /DELETE\r\n"
393 "was deleted successfully." {
394 set err_str
[expect_prompt $remote_prompt
]
397 expect_prompt $remote_prompt
398 set err_str
"The share does not exist"
401 set err_str $default_err_str
404 expect_prompt $remote_prompt
405 set err_str
"Timeout reached in delete_share"
411 proc delete_hosts_entry
{ remote_prompt hosts_file_path backup_hosts_filename
} {
413 set default_err_str
"Unknown error in function delete_hosts_entry"
414 set err_str $default_err_str
416 set cmd
"cd $hosts_file_path\r\n"
420 expect_prompt $remote_prompt
421 set err_str $default_err_str
427 expect_prompt $remote_prompt
428 set err_str
"Timeout reached in delete_hosts_entry"
431 if { $err_str
!= "OK" } {
434 set err_str $default_err_str
437 set cmd
"move /Y $backup_hosts_filename hosts\r\n"
441 set err_str
[expect_prompt $remote_prompt
]
443 "cannot find the file specified." {
444 expect_prompt $remote_prompt
445 set err_str
"File not found"
448 set err_str $default_err_str
451 expect_prompt $remote_prompt
452 set err_str
"Function delete_hosts_entry timed out while renaming $backup_hosts_filename"
458 proc create_hosts_entry
{ remote_prompt hosts_file_path hostname ip \
459 backup_hosts_filename
} {
461 set default_err_str
"Unknown error in function create_hosts_entry"
462 set err_str $default_err_str
464 set cmd
"cd $hosts_file_path\r\n"
468 expect_prompt $remote_prompt
469 set err_str $default_err_str
475 expect_prompt $remote_prompt
476 set err_str
"Timeout reached in create_hosts_entry"
479 if { $err_str
!= "OK" } {
482 set err_str $default_err_str
485 set cmd
"copy /Y hosts $backup_hosts_filename\r\n"
488 "1 file(s) copied." {
489 set err_str
[expect_prompt $remote_prompt
]
491 "cannot find the file specified." {
492 expect_prompt $remote_prompt
493 set err_str
"File not found."
496 set err_str $default_err_str
499 expect_prompt $remote_prompt
500 set err_str
"Function create_hosts_entry timed out while copying hosts file"
503 if { $err_str
!= "OK" } {
506 set err_str $default_err_str
509 set cmd
"echo $ip $hostname #smbtorture host. >> hosts\r\n"
516 expect_prompt $remote_prompt
517 set err_str
"Function create_hosts timed out while updating hosts file"