ftp en Windows 2012,

Hoy vamos ha dar un repaso al ftp y todo por que no podía hacer una tarea en Windows 2012 que realizaba perfectamente en Windows 2003. Por resumir diré que hacia una especie de ftp a un servidor linux mediante un escript, y al trasladar el script a 2012 este no me funciona y tengo que rehacerlo.

Planteamiento:

Hacer una copia desde nuestro windows de varios directorios ubicados en un servidor ftp a nuestra máquina local y automatizarlo. Tal tara debe de hacerse sin ayuda de programas de terceros.

Problema:

El principal problemas es que tenemos muy olvidado el uso del ftp y para mas, cuando lo estamos aprendiendo vemos que no se puede hacer copias de directorios!

Herramienta: FTP

ftp [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [computer]

  • -v – Suppresses verbose display of remote server responses.
  • -n – Suppresses auto-login upon initial connection.
  • -i – Turns off interactive prompting during multiple file transfers.
  • -d – Enables debugging, displaying all ftp commands passed between the client and server.
  • -g – Disables filename globbing, which permits the use of wildcard chracters in local file and path names.
  • -s:filename – Specifies a text file containing ftp commands; the commands will automatically run after ftp starts. No spaces are allowed in this parameter. Use this switch instead of redirection (>).
  • -a – Use any local interface when binding data connection.
  • -w:windowsize – Overrides the default transfer buffer size of 4096.
  • computer – Specifies the computer name or IP address of the remote computer to connect to. The computer, if specified, must be the last parameter on the line.

Comandos del cliente:

  • ! – Runs the specified command on the local computer
  • ? – Displays descriptions for ftp commands
  • append – Appends a local file to a file on the remote computer
  • ascii – Sets the file transfer type to ASCII, the default
  • bell – Toggles a bell to ring after each file transfer command is completed (default = OFF)
  • binary – Sets the file transfer type to binary
  • bye – Ends the FTP session and exits ftp
  • cd – Changes the working directory on the remote computer
  • close – Ends the FTP session and returns to the command interpreter
  • debug – Toggles debugging (default = OFF)
  • delete – Deletes a single file on a remote computer
  • dir – Displays a list of a remote directory’s files and subdirectories
  • disconnect – Disconnects from the remote computer, retaining the ftp prompt
  • get – Copies a single remote file to the local computer
  • glob – Toggles filename globbing (wildcard characters) (default = ON)
  • hash – Toggles hash-sign (#) printing for each data block transferred (default = OFF)
  • help – Displays descriptions for ftp commands
  • lcd – Changes the working directory on the local computer
  • literal – Sends arguments, verbatim, to the remote FTP server
  • ls – Displays an abbreviated list of a remote directory’s files and subdirectories
  • mdelete – Deletes one or more files on a remote computer
  • mdir – Displays a list of a remote directory’s files and subdirectories
  • mget – Copies one or more remote files to the local computer
  • mkdir – Creates a remote directory
  • mls – Displays an abbreviated list of a remote directory’s files and subdirectories
  • mput – Copies one or more local files to the remote computer
  • open – Connects to the specified FTP server
  • prompt – Toggles prompting (default = ON)
  • put – Copies a single local file to the remote computer
  • pwd – Displays the current directory on the remote computer (literally, «print working directory»)
  • quit – Ends the FTP session with the remote computer and exits ftp (same as «bye»)
  • quote – Sends arguments, verbatim, to the remote FTP server (same as «literal»)
  • recv – Copies a remote file to the local computer
  • remotehelp – Displays help for remote commands
  • rename – Renames remote files
  • rmdir – Deletes a remote directory
  • send – Copies a local file to the remote computer (same as «put»)
  • status – Displays the current status of FTP connections
  • trace – Toggles packet tracing (default = OFF)
  • type – Sets or displays the file transfer type (default = ASCII)
  • user – Specifes a user to the remote computer
  • verbose – Toggles verbose mode (default = ON)

Solución

Tras estudiar un poco el comando ftp y sus posivilidades…

…he creado el siguiente escript al cual paso dos ficheros auxiliares en el que digo los destinos que quiero copiar.

Copia.bat

Echo off
Echo Copia de archivos del ftp al directorio local
cd C:\backup\
ren copiaseg _copiaseg
md copiaseg\dir_1
md copiaseg\dir_2
cd C:\backup\
ftp -niv -s:copia_ftp.txt
rmdir /S /Q C:\backup\_copiaseg /S

Los ficheros que paso, «copia_ftp», especifican los comando a ejecutar dentro del programa ftp:

copia_ftp.txt

open 10.10.10.10
user usuario contraseña
cd /directorio_1
lcd C:\backup\copiaseg\dir_1
mget *
bye

Como veis primero renombro la copia del día mes anterior por si acaso y luego creo de nuevo la estructura de directorios. Al ejecutar el comando ftp paso los comandos desde un archivo en el que voy cambiando ruta de origen y destino que quiero copiar, ya que el mget solo copia archivos, no directorios y solo a la ubicación «actual».

Automatización

Desde Windows 2012 usaremos el programador de tareas y ejecutaremos el .bat todos los días a la misma hora (por ejemplo).

 

Espero que os sirva de ayuda y lo compartáis.