Using the FTP batch script under Windows

Source: Internet
Author: User
Tags echo command ftp commands ftp connection glob remote ftp server

Interactive command under FTP

In general, we use FTP to a machine to upload files, download the operation,

In the command mode, it is basically:

1.FTP Host Name

2. Enter the user name

3. Enter your password

4. Toggle mode (ASC or bin)

5. Upload or download (Put or get)

6. Exit (bye or quit)


If you use some FTP tools, it's easier to operate.


FTP Batch processing script

If we want to automatically do some FTP action on a regular basis, we can write a batch file and put it on the windows task to execute periodically.

The general solution to this idea is: write two batch files, one is the main batch processing, used to invoke FTP; The other is a batch of FTP operations (the suffix of this file can be arbitrary).

Here is an example of two files: Main.bat; Ftptmp.bat

Main.bat's content is simple:

Ftp-n-S: "Ftptmp.bat"

The contents of Ftptmp.bat are as follows:

Open $ftphostuser $username $PASSWORDCD $filepathget $filenamebyeexit
The variables in the command should be replaced by themselves.


script with Parameters

Variables like the ones above may not be determined from the beginning, and this part needs to be passed in a dynamic way through variables.

Think about it, when the Windows. Bat script executes, you can add some parameters directly to the script and get it through%0%,%1%.

For example, when executing main.bat above, if you enter at the command line:


In the Main.bat script file

%0% corresponds to Main.bat.

%1% corresponds to 111.

......

See Example:

@echo Offecho%0%>>main.logecho%1%>>main.logftp-n-S: "Ftptmp.bat"

Now the problem is: Ftptmp.bat This file in the FTP script is not able to get the bat set of variables.

So, think of a solution to the idea can be: using Main.bat temporarily generated ftptmp.bat this file (variable replaced by the value passed in)

File similar to:

@echo Offecho Open%1%>>>ftptmp.batecho user%2%%3%>>ftptmp.batecho cd%4%>>ftptmp.batecho get%5% >>ftptmp.batecho Bye>>ftptmp.batecho exit>>ftptmp.batftp-n-s:ftptmp.bat

When called:

Main.bat host username password filepath filename



Reference


FTP [-v] [-d] [-i] [-n] [-G] [-s:filename] [-a] [-w:windowsize] [-a] [Host]

Parameters:

-V: Disables the display of remote server responses.

-D: Enables debugging, displaying all FTP commands that are passed between the client and the server.

-I: Turn off interactive prompts when multiple file transfers.

-N: Disables automatic logon to the initial connection.

-G: Disables the file name group, which allows wildcard character (* and?) to be used in local files and path names.

-s:filename: Specifies the text file that contains the FTP command, which will run automatically when FTP is started. No spaces are allowed in this parameter. Use this switch instead of redirection (>).

-A: Use any local interface when bundling data connections.

-w:windowsize: Overrides the default size of 4096 for the transfer buffer.

-A: Anonymous login.

Host: Specify the computer name or IP address to connect to the remote computer. If specified, the computer must be the last parameter.



Introduction to Simple Batch internal commands

1.echo command
Turn on echo or turn off the request Echo feature, or display a message. If there are no parameters, the echo command displays the current echo setting.
Grammar
echo [{On|off}] [message]
Sample: @echo Off/echo Hello World
In practical applications we will combine this command with a redirect symbol (also known as a pipe symbol, commonly used with > >> ^) to enter some commands into a file of a particular format. This will be reflected in a later example.

[Email protected] command
Indicates that the command after the @ is not displayed, and in the intrusion process (for example, using batches to format the enemy's hard disk), it is not natural to let the other party see the command you are using.
Sample: @echo off
@echo now initializing the program,please wait a minite ...
@format X:/q/u/autoset (Format This command is not allowed to use/y This parameter, it is gratifying that Microsoft left a autoset This parameter to us, the effect and/y is the same. )

3.goto command
Specifies to jump to the label, and when the label is found, the program processes the command starting from the next line.
Syntax: Goto label (label is a parameter that specifies the line in the batch program to which you want to turn.) )
Sample:
if {%1}=={} goto noparms
if {%2}=={} goto noparms (if,% 1,%2 if you do not understand it, skip it first and there will be a detailed explanation later.) )
@Rem check parameters if NULL show usage
: noparms
echo Usage:monitor.bat ServerIP portnumber
Goto END
The name of the tag can be random, but it is better to have a meaningful letter, the letter is added: to indicate that the letter is a label, the Goto command is based on this: to find the next jump to go there. It's better to have some instructions so that you can look at your intentions.

4.Rem command
Note Command, in C language equivalent to/*--------* *, it will not be executed, just a comment on the role, easy for others to read and your own later modification.
Rem Message
Sample: @Rem here is the descr1ption.

5.Pause command
When you run the Pause command, the following message is displayed:
Press any key to continue ...
Sample:
@echo off
: Begin
Copy a:*.* D:back
Echo, put a new disk into driver a
Pause
Goto BEGIN
In this example, all the files on the disk in drive A are copied to D:back. The comment that appears prompts you to put another disk in drive A, and the pause command suspends the program so that you can replace the disk, and then press my táng sash godless?

6.Call command
Another batch program is called from one batch program, and the parent batch program is not terminated. The call command accepts the label that is used as the calling target. If you use call outside of a script or batch file, it will not work at the command line.
Grammar
Call [Drive:][path] FileName [Batchparameters] [: Label [arguments]
Parameters
[Drive:} [Path] FileName
Specifies the location and name of the batch program to invoke. The filename parameter must have a. bat or. cmd extension.

7.start command
Calling an external program, all DOS commands and command-line programs can be called by the start command.
Common parameters of intrusion:
Min Start window minimized
Separate start 16-bit Windows programs in separate spaces
High starts the application in the high priority category
REALTIME starting the application in the REALTIME priority category
Wait to start the application and wait for it to end
Parameters these parameters for transmission to the command/program
The application that executes is a 32-bit GUI application when CMD. EXE returns a command prompt without waiting for the application to terminate. If executed within a command script, the new behavior does not occur.

An explanation of FTP commands

1!
Escape to the shell.
Temporarily exit from the FTP subsystem to the command prompt.
To return to the FTP subsystem, at the command prompt, type exit.
Grammar:!
2 Append
Append to a file
Attaches a local file to a file on the remote computer using the current file type setting.
Syntax: Append localfile [RemoteFile]
3 ASCII
Set ASCII Transfer type.
Set the file to be transmitted in ASCII mode (default value)
Syntax: ASCII
4 Bell
Beep when command completed
Once each file transfer command finishes executing, it switches to a audible sound.
Syntax: Bell
5 binary
Set binary Transfer Type
Sets the file to be transferred in binary mode.
Syntax: binary
6 bye
Terminate FTP session and exit.
Terminates the host FTP process and exits.
Syntax: bye
7 CD
Change remote working directory.
Change the working directory on the remote computer.
Syntax: CD remotedirectory
8 Close
Terminate FTP session
End the FTP session with the remote server and stay at the ftp> prompt.
Syntax: Close
9 Delete
Delete remote file.
Delete the files on the remote computer.
Syntax: Delete remotefile
Ten Dir
List contents of remote directory
Displays a list of directory files and subdirectories on the remote computer.
Syntax: dir [remote-directory] [Local-file]
Disconnect
Terminate FTP session.
Disconnect from the remote computer while preserving the ftp> prompt.
Syntax: Disconnect
Get
Receive file.
Copies remote files to the local computer using the current file transfer type.
Syntax: Get [Remote-file] [Local-file]
Glob
Toggle metacharacter expansion of the local file names.
Toggles the wildcard extension of a local file name.
Syntax: Glob
Hash
Toggle Printing ' # ' for each buffer transferred.
Toggles the digital signature (#) print for each data block that has been transferred.
Syntax: Hash
Help
Print Local Help information.
Displays the FTP subcommand description.
Syntax: Help [Command]
Lcd
Change local working directory.
Change the working directory on the local computer. By default, the working directory is the directory where FTP is started.
Syntax: LCD [directory]
Literal
Send arbitrary ftp command.
Sends the parameter verbatim to the remote FTP server. The single FTP reply code is returned.
Syntax: Literal Argument [...]
ls
List contents of remote directory.
Displays the file short directory and subdirectories on the remote directory.
Syntax: LS [remotedirectory] [LocalFile]
Mdelete
Delete multiple files.
Delete multiple files on the remote computer.
Syntax: Mdelete remotefiles [...]
Mdir
List contents of multiple remote directories.
Displays a list of files and subdirectories in the remote directory.
Syntax: Mdir remotefiles [...] LocalFile
Mget
Get multiple files.
Copies remote multiple files to the local computer using the current file transfer type.
Syntax: Mget remotefiles [...]
mkdir
Get Multiple Files
Create a directory on the remote computer.
Syntax: mkdir directory
% MLS
List contents of multiple remote directories.
Displays the file short directory and subdirectories on the remote directory.
Syntax: MLS remotefiles [...] LocalFile
Mput
Send multiple files.
Copies the local file to the remote computer using the current multiple file transfer types.
Syntax: Mput localfiles [...]
Open
Connect to remote TFTP.
Connect to the specified FTP server.
Syntax: Open Computer [Port]
Prompt
Force interactive prompting on multiple commands.
Switch between open mode and off mode of the prompt.
Syntax: prompt
Put
Send one file.
Copies the local file to the remote computer using the current file transfer type.
Syntax: Put LocalFile [RemoteFile]
Pwd
Print working directory on remote machine.
Prints the current directory on the remote computer.
Syntax: pwd
Quit
Terminate FTP session and exit.
End the FTP session with the remote computer and exit FTP to the command prompt.
Syntax: Quit
Quote
Send arbitrary ftp command.
Sends the parameter verbatim to the remote FTP server. The single FTP reply code is returned.
Syntax: Quote Argument [...]
Recv
Receive file.
Copies remote files to the local computer using the current file transfer type.
Syntax: recv remotefile [LocalFile]
Remotehelp
Get help from remote server.
Displays help for remote commands.
Syntax: remotehelp [Command]
Rename
Rename file.
Renames a remote file.
Syntax: rename FileName newfilename
RmDir
Remove directory on the remote machine.
Delete the remote directory.
Syntax: Mdir Directory
Send
Send one file.
Copies the local file to the remote computer using the current file transfer type.
Syntax: Send LocalFile [RemoteFile]
The status
Show current status.
Displays the current status of the FTP connection.
Syntax: status
Panax Notoginseng Trace
Toggle packet tracing.
Toggle Packet Tracking.
Syntax: Trace
Type
Set file Transfer type.
Sets or displays the file transfer type.
Syntax: Type [TypeName]
The user
Send New User Information
Specifies the user of the remote computer.
Syntax: User UserName [Password] [account]
Verbose
Toggle verbose mode.
Toggles the verbose mode.
Syntax: verbose

Using the FTP batch script under Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.