-
Notifications
You must be signed in to change notification settings - Fork 0
/
EVA-FTP-Client.ps1
714 lines (685 loc) · 31.6 KB
/
EVA-FTP-Client.ps1
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
#######################################################################################
# #
# PowerShell script to communicate with the FTP server in AVM's bootloader #
# #
#######################################################################################
# #
# Copyright (C) 2010-2016 P.Hämmerlein ([email protected]) #
# #
# 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 under #
# http://www.gnu.org/licenses/gpl-2.0.html #
# for more details. #
# #
#######################################################################################
Param([Parameter(Mandatory = $False, Position = 0, HelpMessage = 'the IP address, where EVA is awaiting our compliments (holding the apple behind her back), defaults to 192.168.178.1')][string]$Address = "192.168.178.1",
[Parameter(Mandatory = $False, Position = 1, HelpMessage = 'an optional script block, which will be executed in the context of an active FTP session')][ScriptBlock]$ScriptBlock,
[Parameter(Mandatory = $False, Position = 2, HelpMessage = 'specify this switch, if you request write access to MTD2, which is usually the boot-loader partition and should not get overwritten without explicit intention')][switch]$WriteBootloader
)
$Global:LogoutBeforeClose = $False
#######################################################################################
# #
# check the reply of the server for the expected error code #
# #
#######################################################################################
function ParseAnswer {
Param([Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the answer from device')][System.Object]$answer,
[Parameter(Mandatory = $True, Position = 1, HelpMessage = 'the message ID to look for')][String]$expected
)
if ($answer.GetType().ToString() -eq "System.Object[]") {
for ($i = 0; $i -lt $answer.Count; $i++) {
$line = $answer[$i]
if ($line.GetType().ToString() -eq "System.String" -and $line.Length -gt 0 -and $line.StartsWith($expected)) {
return $True
}
}
}
else {
if ($answer.GetType().ToString() -eq "System.String" -and $answer.Length -gt 0 -and $answer.StartsWith($expected)) {
return $True
}
}
return $False
}
#######################################################################################
# #
# read response text from EVA (with a short delay before the 1st read starts), until #
# no more data is available and return the read lines as string array #
# #
#######################################################################################
function ReadAnswer {
if ($DebugPreference -eq "Inquire") { $DebugPreference = "Continue" }
# a large buffer to hold potential answers ... they should never reach 4 KB in one single read from the FTP control channel
$inputBuffer = New-Object System.Byte[] 4096
# the whole conversation is ASCII based here
$encoding = New-Object System.Text.ASCIIEncoding
[String]$lines = [String]::Empty;
# short hold, it seems that too early access attempts are unsuccessfully
Start-Sleep -Milliseconds 100
$debug = $False
while ($Global:EVAStream.DataAvailable) {
$bytesread = $Global:EVAStream.Read($inputBuffer, 0, 4096)
$lines = [String]::Concat($lines, $encoding.GetString($inputBuffer, 0, $bytesread))
if ($bytesread -gt 0) { $debug = $True }
}
$lines -split "`r`n"
if ($debug) {
Write-Debug "Response:`n$lines`n================"
}
}
#######################################################################################
# #
# write a command to the control channel #
# #
#######################################################################################
function SendCommand {
Param([ValidateNotNullOrEmpty()][Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the command string to be sent to the server')][string]$command
)
if ($DebugPreference -eq "Inquire") { $DebugPreference = "Continue" }
# simple as possible, write and flush - could be done without the function call
$Global:EVAWriter.WriteLine($command)
$Global:EVAWriter.Flush()
Write-Debug "Sent`n$command`n================"
if ($command.ToUpper() -eq "QUIT") {
$Global:LogoutBeforeClose = $False
}
}
#######################################################################################
# #
# read a single value from the urlader environment #
# #
#######################################################################################
function GetEnvironmentValue {
Param([ValidateNotNullOrEmpty()][Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the requested variable')][String]$name
)
SendCommand "GETENV $name"
$answer = ReadAnswer
if (ParseAnswer $answer "200") {
$answer | Where { $_ } | ForEach { if ($_ -match "^(?'name'$name) *(?'value'.*)$") { Write-Output $Matches["value"] } }
}
}
#######################################################################################
# #
# set/unset a single value in the urlader environment #
# #
#######################################################################################
function SetEnvironmentValue {
Param([ValidateNotNullOrEmpty()][Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the variable name')][String]$name,
[Parameter(Mandatory = $False, Position = 1, HelpMessage = 'the value to set, a missing value leads to an UNSETENV command issued')][String]$value
)
if ($value -eq $null -or $value.Length -eq 0) { $cmd = "UNSETENV $name" }
else { $cmd = "SETENV $name $value" }
SendCommand $cmd
$answer = ReadAnswer
return $(ParseAnswer $answer "200")
}
#######################################################################################
# #
# reboot the device #
# #
#######################################################################################
function RebootTheDevice {
SendCommand "REBOOT"
$answer = ReadAnswer
return $(ParseAnswer $answer "221")
}
#######################################################################################
# #
# switch to the alternative partition set, no reboot command is issued #
# #
#######################################################################################
function SwitchSystem {
$current = GetEnvironmentValue "linux_fs_start"
# default value, if it's not set
if ($current -eq $null -or $current.Length -eq 0) { $current = "0" }
Write-Verbose "current setting - linux_fs_start=$current"
# switch to the alternative value
if ($current -ne "1") { $new = "1" } else { $new = "0" }
Write-Verbose "new setting - linux_fs_start=$new"
# set the new value
if (SetEnvironmentValue "linux_fs_start" $new) {
Write-Verbose "new value set successfully"
return $True
}
else {
Write-Output "Error setting new value for 'linux_fs_start' to '$new'."
Write-Output $Error
return $False
}
}
#######################################################################################
# #
# get the whole environment as file from the device #
# #
#######################################################################################
function GetEnvironmentFile {
Param([Parameter(Mandatory = $False, Position = 0, HelpMessage = 'the name of file to read, defaults to "env"')][String]$name = "env"
)
# set binary transfer mode
SendCommand "TYPE I"
$answer = ReadAnswer
if (-not (ParseAnswer $answer "200")) {
$ex = New-Object System.IO.IOException "Error setting binary transfer mode."
Throw $ex
}
# set media type to SDRAM, retrieving is only functioning in this mode
SendCommand "MEDIA SDRAM"
$answer = ReadAnswer
if (-not (ParseAnswer $answer "200")) {
$ex = New-Object System.IO.IOException "Error selecting media type."
Throw $ex
}
try {
$file = New-TemporaryFile
$fname = $file.FullName
}
catch {
$fname = [System.IO.Path]::GetTempFileName()
}
if (ReadFile $fname $name) {
if (-not ($file)) {
$file = Get-Item -Path $fname
}
Get-Content $file
}
Remove-Item $file
}
#######################################################################################
# #
# upload a file to the flash of the device #
# #
#######################################################################################
function UploadFlashFile {
Param([ValidateNotNullOrEmpty()][Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the file, which has to be uploaded')][String]$filename,
[ValidateNotNullOrEmpty()][Parameter(Mandatory = $True, Position = 1, HelpMessage = 'the target partition name (e.g. MTDx)')][String]$target
)
# set binary transfer mode
SendCommand "TYPE I"
$answer = ReadAnswer
if (-not (ParseAnswer $answer "200")) {
$ex = New-Object System.IO.IOException "Error setting binary transfer mode."
Throw $ex
}
# set media type to flash, we want to write to this memory type
SendCommand "MEDIA FLSH"
$answer = ReadAnswer
if (-not (ParseAnswer $answer "200")) {
$ex = New-Object System.IO.IOException "Error selecting media type."
Throw $ex
}
if (-not (WriteFile $filename $target)) {
$ex = New-Object System.IO.IOException "Error uploading image file."
Throw $ex
}
return $True
}
#######################################################################################
# #
# start the device from an in-memory image #
# #
#######################################################################################
function BootDeviceFromImage {
Param([Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the file containing the image to be loaded')][String]$filename,
[Parameter(Mandatory = $False, Position = 1, HelpMessage = 'limit memory size to 128 MB for the uploaded image')][bool]$limitedMemory = $True
)
if ($DebugPreference -eq "Inquire") { $DebugPreference = "Continue" }
if (-not (Test-Path $filename)) {
$ex = New-Object System.Management.Automation.MethodInvocationException "The specified file cannot be found or accessed."
Throw $ex
}
$fileattr = Get-Item "$filename"
$filesize = $fileattr.Length
if ($filesize -eq 0) {
$ex = New-Object System.Management.Automation.MethodInvocationException "The specified file is empty."
Throw $ex
}
$memsize = GetEnvironmentValue "memsize"
$originalmemory = $memsize / 1024 * 1024
# check, if memsize is a multiple of 32 MB, else it's already changed by an earlier attempt
$rem = $memsize % (1024 * 1024 * 32)
if ($rem -ne 0) {
$ex = New-Object System.Management.Automation.MethodInvocationException "The memory size was already reduced by an earlier upload, restart the device first."
Throw $ex
}
if ($limitedMemory) {
# set memory size to 128 MB
$memsize = 1024 * 1024 * 128
}
else {
$memsize = $memsize / 1024 * 1024
}
# compute the needed size values (as strings)
Write-Debug $("Memory size found : 0x{0:x8} ({1} MB)" -f $originalmemory,($originalmemory / 1024 / 1024))
Write-Debug $("Memory size used : 0x{0:x8} ({1} MB)" -f $memsize,($memsize / 1024 / 1024))
Write-Debug $("Image size found : 0x{0:x8}" -f $filesize)
$newsize = $memsize - $filesize
$newmemsize = "0x{0:x8}" -f $newsize
Write-Debug "Set memory size to : $newmemsize"
$firstbyte = 0x80000000
$startaddr = "0x{0:x8}" -f [System.Int32]$($newsize + $firstbyte)
$endaddr = "0x{0:x8}" -f [System.Int32]$($newsize + $firstbyte + $filesize)
Write-Debug "Set MTD RAM device to: $startaddr,$endaddr"
# set the new environment values
if (-not (SetEnvironmentValue "memsize" "$newmemsize")) {
$ex = New-Object System.IO.IOException "Setting the new memory size failed."
Throw $ex
}
if (-not (SetEnvironmentValue "kernel_args_tmp" "mtdram1=$startaddr,$endaddr")) {
$ex = New-Object System.IO.IOException "Setting the temporary kernel parameters failed."
Throw $ex
}
# set binary transfer mode
SendCommand "TYPE I"
$answer = ReadAnswer
if (-not (ParseAnswer $answer "200")) {
$ex = New-Object System.IO.IOException "Error setting binary transfer mode."
Throw $ex
}
# set media type to SDRAM, we'll start from memory
SendCommand "MEDIA SDRAM"
$answer = ReadAnswer
if (-not (ParseAnswer $answer "200")) {
$ex = New-Object System.IO.IOException "Error selecting media type."
Throw $ex
}
if (-not (WriteFile $filename "$startaddr $endaddr")) {
# try to reset environment settings
$memory = "0x{0:x8}" -f $originalmemory
SetEnvironmentValue "memsize" $memory
SetEnvironmentValue "kernel_args_tmp"
# throw error from WriteFile
$ex = New-Object System.IO.IOException "Error uploading image file."
Throw $ex
}
return $True
}
#######################################################################################
# #
# write a file to the FTP server, the needed preparation have to be done before this #
# function is called (set transfer mode, select target media type) #
# #
#######################################################################################
function WriteFile {
Param([Parameter(Mandatory = $True, Position = 0, HelpMessage = 'the file name, where the data will be read')][String]$filename,
[Parameter(Mandatory = $True, Position = 1, HelpMessage = 'the parameters for the "STOR" command')][String]$target,
[Parameter(Mandatory = $False, Position = 2, HelpMessage = 'the command to be used for passive data transfers, defaults to "P@SW"')][String]$passive_cmd = "P@SW"
)
# do not write to MTD2, if it's not explicitly enabled
if ($target.ToUpper().CompareTo("MTD2") -eq 0 -and -not $WriteBootloader) {
$ex = New-Object System.IO.IOException "Write access to bootloader partition is locked."
Throw $ex
}
if ($DebugPreference -eq "Inquire") { $DebugPreference = "Continue" }
Write-Debug "Uploading file '$filename' to '$target' ..."
# ensure file is readable
try {
$path = Resolve-Path $filename
$file = [System.IO.File]::Open($path, "Open", "Read")
}
catch {
$ex = New-Object System.IO.IOException "Unable to locate or open input file."
throw $ex
}
# set passive mode
SendCommand $passive_cmd
$answer = ReadAnswer
if (-not (ParseAnswer $answer "227")) {
$ex = New-Object System.IO.IOException "Error setting passive transfer mode."
Throw $ex
}
# parse passive mode answer: 227 Entering Passive Mode (192,168,178,1,12,10) into named capture groups
if ($answer[0] -match "^227 Entering Passive Mode \((?'a1'[0-9]{1,3}),(?'a2'[0-9]{1,3}),(?'a3'[0-9]{1,3}),(?'a4'[0-9]{1,3}),(?'p1'[0-9]{1,3}),(?'p2'[0-9]{1,3})\).*$") {
$data_addr = [System.Net.IPAddress]::Parse($($Matches["a1"]+'.'+$Matches["a2"]+'.'+$Matches["a3"]+'.'+$Matches["a4"]))
$data_port = ( [System.Int32]::Parse($Matches["p1"]) * 256 ) + [System.Int32]::Parse($Matches["p2"])
}
# open connection and stream
try {
$connection = New-Object System.Net.Sockets.TcpClient $data_addr, $data_port
$stream = $connection.GetStream()
}
catch {
if ($stream) {
$stream.Close()
}
if ($connection) {
$connection.Close()
}
return $False
}
try {
SendCommand "STOR $target"
$answer = [String]::Empty
while ($answer.Length -eq 0) {
$answer = ReadAnswer
}
if (ParseAnswer $answer "150") {
$sending = $True
# CopyToAsync is .NET 4.5 and above (as stated by MSDN), but it seems to be available in 4.0 (W7/PS3/CLR4.0)
#
# $PSVersionTable
# Name Value
# ---- -----
# PSVersion 3.0
# WSManStackVersion 3.0
# SerializationVersion 1.1.0.1
# CLRVersion 4.0.30319.42000
# BuildVersion 6.2.9200.16481
# PSCompatibleVersions {1.0, 2.0, 3.0}
# PSRemotingProtocolVersion 2.2
#
$copytask = $file.CopyToAsync($stream)
while ($sending) {
if ($copytask.IsCompleted) {
$stream.Close()
$file.Close()
$connection.Close()
$sending = $False
# try to get an answer from the device after closing the data stream and the connection, the response from the device was
# very late during my tests
Start-Sleep -Milliseconds 5000
}
$answer = [String]::Empty
$answer = ReadAnswer
if (ParseAnswer $answer "226") {
$sending = $False
$result = $True
}
elseif (ParseAnswer $answer "553") {
# may only occur while we're uploading an in-memory image
$sending = $False
$result = $False
}
}
}
}
catch {
$result = $False
if ($sending) {
$stream.Close()
}
}
if ($file) {
$file.Close()
}
if ($connection) {
$connection.Close()
}
return $result
}
#######################################################################################
# #
# read a file from the FTP server #
# #
#######################################################################################
function ReadFile {
Param([Parameter(Mandatory = $False, Position = 0, HelpMessage = 'the file name, where the data will be stored; defaults to the pipeline, if no value is given')][String]$filename,
[Parameter(Mandatory = $False, Position = 1, HelpMessage = 'the (virtual) file name, defaults to "env"')][String]$name = "env",
[Parameter(Mandatory = $False, Position = 2, HelpMessage = 'the command to be used for passive data transfers, defaults to "P@SW"')][String]$passive_cmd = "P@SW"
)
if ($DebugPreference -eq "Inquire") { $DebugPreference = "Continue" }
# set passive mode
SendCommand $passive_cmd
$answer = ReadAnswer
if (-not (ParseAnswer $answer "227")) {
$ex = New-Object System.IO.IOException "Error setting passive transfer mode."
Throw $ex
}
# parse passive mode answer: 227 Entering Passive Mode (192,168,178,1,12,10) into named capture groups
if ($answer[0] -match "^227 Entering Passive Mode \((?'a1'[0-9]{1,3}),(?'a2'[0-9]{1,3}),(?'a3'[0-9]{1,3}),(?'a4'[0-9]{1,3}),(?'p1'[0-9]{1,3}),(?'p2'[0-9]{1,3})\).*$") {
$data_addr = [System.Net.IPAddress]::Parse($($Matches["a1"]+'.'+$Matches["a2"]+'.'+$Matches["a3"]+'.'+$Matches["a4"]))
$data_port = ( [System.Int32]::Parse($Matches["p1"]) * 256 ) + [System.Int32]::Parse($Matches["p2"])
}
# if no file name was specified, we'll use a temporary file and write its content to the pipeline later
if ($filename.Length -eq 0) {
try {
$tempfile = New-TemporaryFile
$filename = $tempfile.FullName
}
catch {
$filename = [System.IO.Path]::GetTempFileName()
$tempfile = New-Item -Name $filename
}
}
else {
$tempfile = $null
}
# open connection and stream
try {
$connection = New-Object System.Net.Sockets.TcpClient $data_addr, $data_port
$stream = $connection.GetStream()
}
catch {
if ($stream) {
$stream.Close()
}
if ($connection) {
$connection.Close()
}
return $False
}
# open target file
try {
$file = [System.IO.File]::Open($filename, "Create", "Write")
}
catch {
$ex = New-Object System.IO.IOException "Error opening output file."
Throw $ex
}
# set timeout on input stream, no more data means end of file
$stream.ReadTimeout = 500
# start transfer
SendCommand "RETR $name"
$answer = ReadAnswer
if (ParseAnswer $answer "150") {
if (-not (ParseAnswer $answer "226")) {
$receiving = $True
# receive data stream
while (-not $stream.DataAvailable) {
Start-Sleep -Milliseconds 100
}
try {
$copytask = $stream.CopyToAsync($file)
while ($receiving) {
$answer = ReadAnswer
if (ParseAnswer $answer "226") {
$receiving = $False
}
elseif ($copytask.IsCompleted) {
$receiving = $False
}
}
}
catch [System.IO.IOException] {
# usually our expected timeout, because the connection will not be closed by EVA
# Write-Debug "Timeout from network stream"
$receiving = $False
}
finally {
$stream.Close()
}
}
else {
# short data transfer only, already done
try {
$stream.CopyTo($file)
}
catch [System.IO.IOException] {
# timeout means EOF
}
finally {
$stream.Close()
}
}
}
elseif (ParseAnswer $answer "501")
{
Write-Output "Invalid file name '$name'."
return $False
}
# close file
if ($file) {
$file.Close()
}
if ($connection) {
$connection.Close()
}
if ($tempfile -ne $null) {
Get-Content $tempfile
Remove-Item $tempfile
}
else {
return $True
}
}
#######################################################################################
# #
# handle login into the EVA session #
# #
#######################################################################################
function Login {
Param([Parameter(Mandatory = $False, Position = 2, HelpMessage = 'the user name to be used to login')][String]$username = "adam2",
[Parameter(Mandatory = $False, Position = 3, HelpMessage = 'the needed password')][String]$password = "adam2"
)
$loggedIn = $False
$loopRead = $True
# write the "USER" command
SendCommand "USER $username"
$i = 0
for ($i = 0; $i -lt 10 -and $loopRead; $i++) {
# ReadAnswer has a delay embedded (100 ms), so hang tight, but be patient (10 loops max. => 1 second delay)
$answer = ReadAnswer
if (ParseAnswer $answer "331") {
SendCommand "PASS $password"
}
elseif (ParseAnswer $answer "230") {
$loggedIn = $True
$loopRead = $False
}
elseif (ParseAnswer $answer "530") {
$loopRead = $False
}
}
return $loggedIn
}
# init our globals and clear any pending errors
$Global:EVAConnection = $null
$Global:EVAStream = $null
$Global:EVAWriter = $null
$Error.Clear()
# connect now
try {
# create socket and connect to EVA, this will hold up the device in the "bootloader state", until a "REBOOT" command is issued
$Global:EVAConnection = New-Object System.Net.Sockets.TcpClient $Address, 21
# get the underlying stream
$Global:EVAStream = $Global:EVAConnection.GetStream()
# open a stream writer for the control channel
$Global:EVAWriter = New-Object System.IO.StreamWriter $Global:EVAStream
}
catch {
Write-Host "Error connecting to remote host $EVA_IP`n$($Error[0].ToString())."
if ($Global:EVAWriter -ne $null) {
$Global:EVAWriter.Close()
$Global:EVAWriter = $null
}
if ($Global:EVAStream -ne $null) {
$Global:EVAStream.Close()
$Global:EVAStream = $null
}
if ($Global:EVAConnection -ne $null) {
$Global:EVAConnection.Close()
$Global:EVAConnection = $null
}
Exit
}
$answer = ReadAnswer
if (ParseAnswer $answer "220") {
if (Login) {
$Global:LogoutBeforeClose = $True
SendCommand "SYST"
$answer = ReadAnswer
if (ParseAnswer $answer "215 AVM EVA") {
try {
if ($ScriptBlock) {
$ScriptBlock.Invoke()
}
else {
#####################################################################################
# #
# place your orders here, you may use the provided subfunctions or build your own #
# command chain #
# #
#####################################################################################
#
# Possible actions are:
#
# GetEnvironmentFile [ "env" | "count" ]
# GetEnvironmentValue <name>
# SetEnvironmentValue <name> [ <value> ]
# RebootTheDevice
# SwitchSystem
# BootDeviceFromImage <image_file>
# UploadFlashFile <flash_file> <target_partition>
#
# or you use some lower-level functions (some aren't useful here, it's simply too
# late to login - e.g.) like these:
#
# SendCommand
# ReadAnswer
# ParseAnswer
# ReadFile
# WriteFile
# Login
#
# You may specify your own script block as 2nd argument to this script file, in this
# script block you may use the low- and high-level functions from above.
#
#####################################################################################
# #
# end of "changeable" section, if you modify something outside, please do not ask #
# or blame the author #
# #
#####################################################################################
}
}
catch {
if ($Global:LogoutBeforeClose) {
SendCommand "QUIT"
$answer = ReadAnswer
$Global:LogoutBeforeClose = $False
}
throw $_.Exception
}
}
else {
Write-Verbose "Unexpected FTP server found:`n$answer"
if ($Global:LogoutBeforeClose) {
SendCommand "QUIT"
$answer = ReadAnswer
$Global:LogoutBeforeClose = $False
}
}
}
else {
Write-Verbose "Unable to login to EVA."
}
}
else {
Write-Verbose "Unexpected answer '$answer' from remote host."
}
# be gentle and close our open stream and socket
$Global:EVAWriter.Close()
$Global:EVAStream.Close()
$Global:EVAConnection.Close()
$Global:EVAWriter.Dispose()
$Global:EVAStream.Dispose()
$Global:EVAConnection.Dispose()
$Global:EVAWriter = $null
$Global:EVAStream = $null
$Global:EVAConnection = $null