blob: a7ba95d722550f359e703cd1e47b1bdf65239e7c (
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
|
# A library of commonly used functions written in expect.
# Copyright Brad Henry <brad@samba.org> 2006
# Released under the GNU GPL v2 or later.
# This function maps a drive letter to a share point.
proc map_share { remote_prompt share_drive sharepoint username domain password } {
set default_err_str "Unknown error in function map_share"
set err_str $default_err_str
set cmd "net use $share_drive $sharepoint $password /USER:$username@$domain\r\n"
send $cmd
expect {
"The command completed successfully." {
expect_prompt $remote_prompt
set err_str "OK"
} \
"The local device name is already in use." {
expect_prompt $remote_prompt
set err_str "The device name $share_drive is already in use"
} \
"The network name cannot be found." {
expect_prompt $remote_prompt
set err_str "Sharepoint $sharepoint could not be found"
} \
timeout {
set err_str "Function map_share timed out while mapping $share_drive to $sharepoint"
}
}
return $err_str
}
# This function unmaps a drive letter from a share point.
proc unmap_share { remote_prompt share_drive } {
set default_err_str "Unknown error in function unmap_share"
set err_str $default_err_str
set cmd "net use $share_drive /DELETE\r\n"
send $cmd
expect {
"was deleted successfully." {
expect_prompt $remote_prompt
set err_str "OK"
} \
"NET HELPMSG 2250" {
expect_prompt $remote_prompt
set err_str "The network connection could not be found while unmapping $share_drive"
} \
timeout {
set err_str "Function unmap_share timed out while unmapping $share_drive"
}
}
return $err_str
}
# This function uses xcopy to copy a text file from one location on the
# remote windows host to another.
proc xcopy_file { remote_prompt in_filename out_filename xcopy_options } {
set default_err_str "Unknown error in function xcopy_file"
set err_str $default_err_str
set cmd "xcopy $in_filename $out_filename $xcopy_options\r\n"
send $cmd
expect {
"(F = file, D = directory)? " {
set cmd "F\r\n"
send $cmd
expect {
"1 File(s) copied\r\n\r\n" {
expect_prompt $remote_prompt
set err_str "OK"
} \
"0 File(s) copied\r\n\r\n" {
expect_prompt $remote_prompt
set err_str $default_err_str
} \
timeout {
set err_str "Function xcopy_file has timed out while copying $in_filename"
}
}
} \
"1 File(s) copied\r\n\r\n" {
expect_prompt $remote_prompt
set err_str "OK"
} \
"0 File(s) copied\r\n\r\n" {
expect_prompt $remote_prompt
set err_str $default_err_str
} \
timeout {
set err_str "Function xcopy_file timed out while copying $in_filename"
}
}
return $err_str
}
# This function creates a temporary file on the remote windows host.
# The file contents are populated by a recursive directory listing of
# the windows %HOMEDRIVE%.
proc create_tmp_file { remote_prompt filename } {
set default_err_str "Unknown error in function create_tmp_file"
set err_str $default_err_str
set cmd "dir %HOMEDRIVE%\\ /S > $filename\r\n"
send $cmd
expect {
$remote_prompt {
set err_str "OK"
} \
timeout {
set err_str "Function create_tmp_file timed out while creating $filename"
}
}
return $err_str
}
# This function compares two files on the remote windows host.
proc compare_files { remote_prompt file1 file2 } {
set default_err_str "Unknown error in function compare_files"
set err_str $default_err_str
set cmd "fc $file1 $file2\r\n"
send $cmd
expect {
"FC: no differences encountered\r\n\r\n\r\n" {
expect_prompt $remote_prompt
set err_str "OK"
} \
"\*\*\*\*\* $file1" {
expect_prompt $remote_prompt
set err_str "Files $file1 and $file2 differ"
} \
"\*\*\*\*\* $file2" {
expect_prompt $remote_prompt
set err_str "Files $file1 and $file2 differ"
} \
timeout {
set err_str "Function compare_files timed out while comparing files $file1 and $file2"
}
}
return $err_str
}
# This function deletes a file on the remote windows host.
proc delete_file { remote_prompt filename } {
set default_err_str "Unknown error in function delete_file"
set err_str $default_err_str
set cmd "del $filename\r\n"
send $cmd
expect {
"Could Not" {
expect_prompt $remote_prompt
set err_str $default_err_str
} \
$remote_prompt {
set err_str "OK"
} \
timeout {
set err_str "Function delete_file timed oout while deleting $filename"
}
}
return $err_str
}
# This function copies a text file over telnet from the local unix host
# to the remote windows host.
proc copy_file { remote_prompt in_filename out_filename } {
set default_err_str "Unknown error in function copy_file"
set err_str $default_err_str
# The octal ASCII code for Control-Z is 032.
set CTRLZ \032
# Open local file and read contents.
set in_file [open $in_filename r]
set in_data [read $in_file]
# Initiate copy on remote host.
set cmd "copy con $out_filename\r\n"
send $cmd
# Separate $in_data into lines and send to remote host.
set out_data [split $in_data "\n"]
foreach out_line $out_data {
send $out_line
# We might as well do a unix -> windows line conversion.
send "\r\n"
# Are we overwriting an existing file?
# If so, exit so we can handle it.
expect {
"(Yes/No/All)" {
send "NO\r\n"
expect_prompt $remote_prompt
set err_str "File exists"
} \
$out_line {
set err_str "OK"
} \
timeout {
set err_str "Function copy_file timed out while copying $in_filename"
}
}
if { $err_str != "OK" } {
return $err_str
} else {
set err_str $default_err_str
}
}
# ^Z\r to complete the transfer.
send $CTRLZ
send "\r"
expect {
"file(s) copied." {
set err_str [expect_prompt $remote_prompt]
} \
$remote_prompt {
set err_str $default_err_str
} \
timeout {
expect_prompt $remote_prompt
set err_str "Function copy_file timed out while finishing copy of $in_filename"
}
}
return $err_str
}
# This function waits for the command prompt and reports an error on
# timeout.
proc expect_prompt { remote_prompt } {
set default_err_str "Unknown error occurred while waiting for the command prompt"
set err_str $default_err_str
expect {
$remote_prompt {
set err_str "OK"
} \
timeout {
set err_str "Timeout occurred while waiting for the command prompt"
}
}
return $err_str
}
# This function will create a telnet login shell to $remote_host as $username.
# If expected dialogue is not recieved, return with a specific error if one
# is recognized. Otherwise return a generic error indicating the function
# name.
proc telnet_login { remote_prompt remote_host username password } {
set default_err_str "Unknown error in function telnet_login"
set err_str $default_err_str
set cmd "telnet $remote_host\r"
send $cmd
expect {
"login: " {
set err_str "OK"
} \
"Connection refused" {
set err_str "Connection refused"
} \
"No route to host" {
set err_str "No route to host"
} \
timeout {
set err_str "Function telnet_login timed out while waiting for the login prompt"
}
}
if { $err_str != "OK" } {
# Return because something unexpected happened.
return $err_str
} else {
# Reset err_str
set err_str $default_err_str
}
set cmd "$username\r"
send $cmd
expect {
"password: " {
set err_str "OK"
} \
timeout {
set err_str "Function telnet_login timed out while waiting for the password prompt"
}
}
if { $err_str != "OK" } {
return $err_str
} else {
set err_str $default_err_str
}
set cmd "$password\r"
send $cmd
expect {
$remote_prompt {
set err_str "OK"
} \
"Login Failed" {
set err_str "Telnet login failed"
} \
timeout {
set err_str "Function telnet_login timed out while waiting for the command prompt"
}
}
return $err_str
}
proc create_directory { remote_prompt sharepath } {
set default_err_str "Unknown error in function create_directory"
set err_str $default_err_str
set cmd "mkdir $sharepath\r\n"
send $cmd
expect {
"already exists" {
expect_prompt $remote_prompt
set err_str "Directory already exists"
} \
$remote_prompt {
set err_str "OK"
} \
timeout {
expect_prompt $remote_prompt
set err_str "Timeout reached starting create_directory."
}
}
return $err_str
}
proc delete_directory { remote_prompt sharepath } {
set default_err_str "Unknown error in function delete_directory"
set err_str $default_err_str
set cmd "rmdir /S /Q $sharepath\r\n"
send $cmd
expect {
"Access is denied." {
expect_prompt $remote_prompt
set err_str "Directory access is denied"
} \
$remote_prompt {
set err_str "OK"
} \
timeout {
expect_prompt $remote_prompt
set err_str "Timeout reached in delete_directory"
}
}
return $err_str
}
proc create_share { remote_prompt username sharepath sharename } {
set default_err_str "Unknown error in function create_share"
set err_str $default_err_str
set cmd "net share $sharename=$sharepath /GRANT:$username,FULL\r\n"
send $cmd
expect {
"was shared successfully." {
set err_str [expect_prompt $remote_prompt]
} \
"NET HELPMSG 2118." {
expect_prompt $remote_prompt
set err_str "The name has already been shared"
} \
$remote_prompt {
set err_str $default_err_str
} \
timeout {
expect_prompt $remote_prompt
set err_str "Timeout reached in create_share"
}
}
return $err_str
}
proc delete_share { remote_prompt sharename } {
set default_err_str "Unknown error in function delete_share"
set err_str $default_err_str
set cmd "net share $sharename /DELETE\r\n"
send $cmd
expect {
"was deleted successfully." {
set err_str [expect_prompt $remote_prompt]
} \
"does not exist." {
expect_prompt $remote_prompt
set err_str "The share does not exist"
} \
$remote_prompt {
set err_str $default_err_str
} \
timeout {
expect_prompt $remote_prompt
set err_str "Timeout reached in delete_share"
}
}
return $err_str
}
proc delete_hosts_entry { remote_prompt hosts_file_path backup_hosts_filename } {
set default_err_str "Unknown error in function delete_hosts_entry"
set err_str $default_err_str
set cmd "cd $hosts_file_path\r\n"
send $cmd
expect {
"." {
expect_prompt $remote_prompt
set err_str $default_err_str
} \
$remote_prompt {
set err_str "OK"
} \
timeout {
expect_prompt $remote_prompt
set err_str "Timeout reached in delete_hosts_entry"
}
}
if { $err_str != "OK" } {
return $err_str
} else {
set err_str $default_err_str
}
set cmd "move /Y $backup_hosts_filename hosts\r\n"
send $cmd
expect {
"1 file(s) moved." {
set err_str [expect_prompt $remote_prompt]
} \
"cannot find the file specified." {
expect_prompt $remote_prompt
set err_str "File not found"
} \
$remote_prompt {
set err_str $default_err_str
} \
timeout {
expect_prompt $remote_prompt
set err_str "Function delete_hosts_entry timed out while renaming $backup_hosts_filename"
}
}
return $err_str
}
proc create_hosts_entry { remote_prompt hosts_file_path hostname ip \
backup_hosts_filename } {
set default_err_str "Unknown error in function create_hosts_entry"
set err_str $default_err_str
set cmd "cd $hosts_file_path\r\n"
send $cmd
expect {
"." {
expect_prompt $remote_prompt
set err_str $default_err_str
} \
$remote_prompt {
set err_str "OK"
} \
timeout {
expect_prompt $remote_prompt
set err_str "Timeout reached in create_hosts_entry"
}
}
if { $err_str != "OK" } {
return $err_str
} else {
set err_str $default_err_str
}
set cmd "copy /Y hosts $backup_hosts_filename\r\n"
send $cmd
expect {
"1 file(s) copied." {
set err_str [expect_prompt $remote_prompt]
} \
"cannot find the file specified." {
expect_prompt $remote_prompt
set err_str "File not found."
} \
$remote_prompt {
set err_str $default_err_str
} \
timeout {
expect_prompt $remote_prompt
set err_str "Function create_hosts_entry timed out while copying hosts file"
}
}
if { $err_str != "OK" } {
return $err_str
} else {
set err_str $default_err_str
}
set cmd "echo $ip $hostname #smbtorture host. >> hosts\r\n"
send $cmd
expect {
$remote_prompt {
set err_str "OK"
} \
timeout {
expect_prompt $remote_prompt
set err_str "Function create_hosts timed out while updating hosts file"
}
}
return $err_str
}
|