-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_ftp.yaml
80 lines (65 loc) · 2.91 KB
/
update_ftp.yaml
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
---
- name: Descargar todos los archivos .txt desde un FTP usando PowerShell
hosts: all
tasks:
- name: Crear directorio local si no existe
win_file:
path: C:\update
state: directory
- name: Crear script PowerShell en el servidor
win_copy:
content: |
$ftpServer = "ftp://10.15.1.26/"
$username = "update"
$password = "Y3lm020."
$localPath = "C:\\update\\"
Write-Host "Conectando al servidor FTP $ftpServer..."
# Configurar la solicitud FTP
try {
$ftpRequest = [System.Net.FtpWebRequest]::Create($ftpServer)
$ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftpRequest.Credentials = New-Object System.Net.NetworkCredential($username, $password)
$response = $ftpRequest.GetResponse()
$responseStream = $response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($responseStream)
$fileList = $reader.ReadToEnd()
$reader.Close()
$response.Close()
Write-Host "Lista de archivos obtenida: `n$fileList"
# Limpiar los nombres de los archivos
$txtFiles = $fileList -split "`r?`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ -match '\.txt$' }
if ($txtFiles.Count -eq 0) {
Write-Host "No se encontraron archivos .txt en el directorio FTP."
} else {
Write-Host "Archivos .txt encontrados: $($txtFiles -join ', ')"
}
# Descargar cada archivo .txt
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username, $password)
foreach ($file in $txtFiles) {
$remoteFile = $ftpServer + $file
$localFile = $localPath + $file
Write-Host "Descargando archivo $remoteFile a $localFile"
$webclient.DownloadFile($remoteFile, $localFile)
}
Write-Host "Descarga completada."
} catch {
Write-Host "Error durante la conexión FTP o la descarga de archivos: $_"
}
dest: C:\update\download_ftp.ps1
- name: Mostrar script creado en el servidor
debug:
msg: "Script PowerShell creado en: C:\\update\\download_ftp.ps1"
- name: Ejecutar el script PowerShell y capturar la salida
win_shell: powershell.exe -ExecutionPolicy Bypass -File C:\update\download_ftp.ps1
register: script_output
- name: Mostrar la salida del script PowerShell con saltos de línea
debug:
var: script_output.stdout_lines
- name: Eliminar el script PowerShell
win_file:
path: C:\update\download_ftp.ps1
state: absent
- name: Confirmar la eliminación del script PowerShell
debug:
msg: "Script PowerShell eliminado del servidor."