BAT Script processing FTP super-strong case Analysis _dos/bat

Source: Internet
Author: User
Tags chmod exit in ftp commands ftp file ftp login parent directory ftp file transfer runique
Objective:
The company has hundreds of Windows servers, every time the program is updated, if it is a table copy data, I am afraid to arrange 10 people, one day is not necessarily done, so it is necessary to study a full automatic update scheme, so I thought of FTP, do a primary FTP station, on hundreds of client computer to start bat script, Every 30 seconds detection, if the main station has updated procedures, automatic download updates, within minutes, on 400 Windows Server, can be fully automatic update procedures complete, quite practical, is the original study of bat script ideas posted out, I hope that the bulk of the operation of the staff in one Direction. Of course, the batch update server, each company has its own solution, this bat is not the best way, you can use the shell, you can use Perl, or Python and other scripts to solve.

A, FTP command description

1, in the Windows system because FTP is a program, if the bat script directly invoke the FTP command, in the execution will always pop-up command line window, loop in the >FTP cursor this, so to change the way, using-s:filename, more commands Please enter-H view.



2, FTP login successful, need to download a remote single file, use Get command, if download multiple files, with mget, it supports wildcards, in the use of mget, you need to choose each file y/n, if you want to not interactive download all the files, You can enter a prompt command to turn off interactive mode once.

the mput and Mget commands should note two questions:

Ⅰ, unable to specify the name of the target file, all files on the command line are treated as source files
Ⅱ, mput,mget Two commands can not upload and download the directory, only the file
Ⅲ, Prompt: Every time you execute one of these commands, in the loop between on and off, when the FTP script starts, the Prompt defaults to on, so generally when uploading, downloading files, perform a Prompt, turn off the interactive on, and turn off.

3, the file transmission mode:
Binary, binary transmission
ASCII, ASCII transmission
In the FTP file transfer process, ASCII transmits HTML and text-written files, while binary code transmission can transmit text and non text (executable files, compressed files, pictures, etc.), with versatility, binary code transmission faster than ASCII transmission, so when creating a bat script, Generally enter the bin command to enable binary transmissions. If you transfer a non-text file in ASCII mode, a heap of garbled files may appear, and if you upload some CGI scripts, you may not be able to run such scripts and see Server error errors on the browser.

The difference between ASCII and binary mode is the handling of carriage return wrapping. The binary mode does not handle any data, and the ASCI mode converts the carriage return line to the native carriage return character, for example, under \n,windows under Unix under \r\n,mac or \ r. The UNIX system downlink terminator is a byte, that is, hexadecimal 0 A, whereas Ms's system is two bytes, or hexadecimal 0d0a.

So when you download files from UNIX FTP server in ASCII (either binary or text files, Redhat's vsftp default is binary,ascii by default), a 0D is automatically inserted each time a byte is detected as 0 a. So if your files are binary files such as executable files, zip packages, and so on, it's definitely not going to work. If your file is Unix text file, you use ASCII mode is correct, if misuse of binary mode, you look at the file on Windows is no line, inside is a black square.
When transferring files with FTP clients, because the software is more intelligent, it can automatically enable transmission mode according to the suffix, so don't worry about the problem.

second, the case verification:

Environment Description: 192.168.133.34 has an FTP server side (Windows Edition), FTP user name is Lgh, password is www.jb51.net. The user's root directory is D:\lgh. The document below is shown below. Sql.rar is Sql.bat compressed file, we pay attention to the map, I will later in the example cited in this file, the Mget,get,prompt,bin,ascii and other FTP commands interpreted casualgirlfriend.

Instance one: Direct bat invoke FTP command

Ftpbat.bat Content
(* * * Everyone set up bat file not named Ftp.bat, execution will have problems * * *)
Copy Code code as follows:

Ftp
Open 192.168.133.34
Lgh
Www.jb51.net
Get Sql.rar
Bye

From the results of the graph, the bat script directly calls the XP FTP command is not good, the execution of the h:\>ftp will continue to die loop. I have the XP system here is SP3, the other version, I did not test. If there is a difference, you are welcome to leave a message.

example two: Upload with put,get, download a single file

Upload file flower.zip on XP machine H:\JS directory to FTP server
Download the Sql.rar file from the FTP server to the H:\JS directory on the XP machine
Ftp-get-put.bat contents are as follows:
Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
echo Get Sql.rar >> "%ftpfilename%"
Echo put Flower.zip >> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


Let's take a look at the results, see two pictures below. From the XP system, H disk JS directory, more than a SQL.RAR,FTP server root directory more than a flower.zip, this shows that the script just completed the normal implementation, to achieve the intended purpose.

example three: Upload with put,get, download multiple files

Copy the Flower.zip on the XP machine and rename it to Flower2.zip
Delete the Sql.rar file that downloaded the second instance on the XP machine, so as not to affect the operation
On the FTP server to upload the second instance of the Flower.zip file deleted, so as not to affect the operation
Upload files Flower.zip and flower2.zip to the FTP server on the XP machine H:\js directory
Ftp-get-put-many.bat contents are as follows:
Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
Echo put Flower.zip flower2.zip>> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


Check the root directory on the FTP server to see if Flower.zip and Flower2.zip have been uploaded successfully. From the graph, only successfully uploaded the Flower2.zip file.

Just in that Ftp-get-put-many.bat script.
Echo put Flower.zip flower2.zip>> "%ftpfilename%"
Flower2.zip is the last file of the transmission, is not put, means that bat in the process of uploading, only to recognize the last file. We'll add a file flower3.zip to verify that.

Copy the Flower.zip on the XP machine and rename it to Flower3.zip
On the FTP server to upload the second instance of the Flower2.zip file deleted, so as not to affect the operation

Ftp-get-put-many.bat content is modified as follows:


Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
Echo put Flower.zip flower2.zip flower3.zip>> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


As a result, or only successfully uploaded flower2.zip, because the bat double click, the implementation process will flash past, we do not see the implementation process, to the command line to execute, to see exactly what went wrong.

Command line, execute Ftp-get-put-many.bat script, from the implementation of the situation, flower.zip three files are put, but in the transmission, but only transmitted flower2.zip, really puzzled, have know friends? Give me some direction.

In another way, use the wildcard character *, try it.
Ftp-get-put-many.bat content is modified as follows:


Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
Echo put Flower*.zip >> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


Delete all uploaded flower files on the FTP server, so as not to affect the operation
Execute Ftp-get-put-many.bat, the result only flower.zip file upload succeeded. Ah, flower3.zip file, how can not upload.??

In fact, using put to transfer multiple files, can be completely replaced by a duplicate, such as: Ftp-get-put-many.bat content modified as follows:

Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
Echo put Flower.zip >> "%ftpfilename%"
Echo put Flower2.zip >> "%ftpfilename%"
Echo put Flower3.zip >> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


example four: Upload with mput,mget, download multiple files

Delete files on flower on the FTP server

Ftp-mget-mput-many.bat contents are as follows:

Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
echo mput flower.zip flower2.zip flower3.zip>> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


Double-click the Ftp-mget-mput-many.bat, found the system after a pop-up window, it jammed, stop before

The reason is that the mput command is used, but the interaction is not closed with prompt

Ftp-mget-mput-many.bat content is modified as follows:

Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
echo Prompt >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
echo mput flower.zip flower2.zip flower3.zip>> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


To perform Ftp-mget-mput-many.bat again, the results are shown in the following figure:

Try it with the wildcard character *
Delete files on flower on the FTP server
Ftp-mget-mput-many.bat content is modified as follows:


Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
echo Prompt >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
echo mput flower*.zip >> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


Still successfully uploaded the relevant flower*.zip file. Because the result is the same as the previous step, so we don't catch the picture.

Example Five: Transferring non-text files between Windows systems with ASCII

Upload the rmb.jpg on the XP computer to the root directory of the FTP server via ASCII.


Ftp-get-ascii.bat content is modified as follows:


Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.133.34 > "%ftpfilename%"
echo Lgh >> "%ftpfilename%"
echo www.liuguohua.com >> "%ftpfilename%"
echo ASCII >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
Echo Put rmb.jpg>> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


After performing Ftp-get-ascii.bat, found that rmb.jpg can be normal upload, and can normally open, that, this phenomenon and the previous so the ASCII can not transmit non-text things discrepancies, this is how to return to the matter.
This is because the FTP server is a Windows system and the bat script on the XP computer is also running on the Windows system, so bat uses FTP to transfer files in the Windows system, either in ASCII or with binary. Next we do an experiment, the FTP server selected CentOS, and then measured the ASCII transmission of non-text files.


Example Six: Transferring non-text files between Windows systems and Linux systems in ASCII

Build a VSFTPD server on the Rhel 5.4 64-bit system

It says Redhat. The binary transmission is enabled by default, and ASCII transmission is turned off, so we have to modify the VSFTPD configuration file, modify the/etc/vsftpd/vsftpd.conf file, allow the client to carry out the ASCII transmission, and restart the VSFTPD

Put the rmb.jpg on the FTP server, we note that under the H:\js directory only ftp-getascii.bat files

The contents of the Ftp-getascii.bat file are modified as follows:

Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.145.226 > "%ftpfilename%"
echo FTP1 >> "%ftpfilename%"
echo redhat >> "%ftpfilename%"
echo ASC >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
echo Get rmb.jpg >> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


Execute Ftp-getascii.bat, see the result, rmb.jpg file has downloaded to XP computer, double click this file, find the image garbled, file is destroyed, all is the trouble of the ASCII code transmission.

Change the bin transfer and test
Delete the rmb.jpg file on the XP computer, and then double-click the Ftp-getascii.bat file to download a rmb.jpg file from the Linux FTP server.

Modify Ftp-getascii.bat to convert ASCII to binary transmission

Copy Code code as follows:

@echo off
Set Ftpfilename=autoftp.cfg
echo Open 192.168.145.226 > "%ftpfilename%"
echo FTP1 >> "%ftpfilename%"
echo redhat >> "%ftpfilename%"
Echo bin >> "%ftpfilename%"
echo LCD h:\js >> "%ftpfilename%"
echo Get rmb.jpg >> "%ftpfilename%"
echo Bye >> "%ftpfilename%"
Ftp-s: "%ftpfilename%"
Del "%ftpfilename%"


As a result, the file is downloaded normally and the image is displayed correctly.


Extended reading:
The internal commands used by FTP are as follows (the brackets indicate an option):
1.! [Cmd[args]]: Perform interactive shell,exit in the local machine back to the FTP environment, such as:!ls*.zip.
2.$ Macro-ame[args]: Perform macro definition macro-name.
3.account[password]: Provides the supplemental password required to access system resources after a successful logon to the remote system.
4.append Local-file[remote-file]: Appends the local file to the remote system host and uses the local file name if no remote system file name is specified.
5.ASCII: Use ASCII type transfer mode.
6.bell: The computer rings once after each command completes.
7.bin: Use binary file transfer mode.
8.bye: Exits the FTP session process.
9.case: When using Mget, the upper case of the remote host file name is converted to lowercase letters.
10.CD Remote-dir: Access to remote host directory.
11.cdup: Enter the parent directory of the remote host directory.
12.chmod mode File-name: Sets the access mode of the remote host file File-name to mode, such as: chmod 777 a.out.
13.close: Interrupts the FTP session with the remote server (corresponding to open).
14.CR: When the file is transferred using ASSCII, the carriage return line wrap is converted to a back line.
15.delete remote-file: Deletes the remote host file.
16.debug[debug-value]: Set debug mode, display every command sent to the remote host, such as: Deb up 3, if set to 0, means cancel Debug.
17.dir[remote-dir][local-file]: Displays the remote host directory and stores the results in the local file local-file.
18.disconnection: Same as close.
19.form format: Sets the file transfer mode to format, default to file mode.
20.get Remote-file[local-file]: remote-file The remote host file to the local-file of the local hard drive.
21.glob: Sets the file name extension for Mdelete,mget,mput, which does not extend the filename by default, and the-G parameter of the command line.
22.hash: 1024 bytes per transmission, displaying a hash symbol (#).
23.help[cmd]: Displays the help information for the FTP internal command cmd, such as: get.
24.idle[seconds]: Sets the remote server's hibernate timer to [seconds] seconds.
25.image: Set binary transmission mode (same binary).
26.LCD[DIR]: Switches the local working directory to dir.
27.ls[remote-dir][local-file]: Displays the remote directory Remote-dir and stores the local file Local-file.
28.macdef Macro-name: Defines a macro that, when it encounters an empty row under Macdef, ends the macro definition.
29.mdelete[remote-file]: Deletes the remote host file.
30.mdir remote-files Local-file: Similar to dir, but can specify multiple remote files, such as: Mdir *.o.*.zipoutfile.
31.mget remote-files: Transfer multiple remote files.
32.mkdir dir-name: Build a directory in the remote host.
33.MLS remote-file local-file: Same nlist, but multiple file names can be specified.
34.mode[modename]: Sets the file transfer mode to Modename, default to stream mode.
35.modtime file-name: Displays the last modification time of the remote host file.
36.mput local-file: Transfer multiple files to a remote host.
37.newer File-name: If the File-name modification time in the remote machine is closer than the time of the file with the same name as the local hard disk, the file is retransmission.
38.nlist[remote-dir][local-file]: Displays a list of files for the remote host directory and stores the Local-file on the local hard drive.
39.nmap[inpattern Outpattern]: Set file name Mapping mechanism, so that when the file transfer, some characters in the file are converted to each other, such as: Nmap $1.$2.$3[$1,$2]. [$2,$3], the file name changes to A1,A2 when the file is transferred a1.a2.a3. This command is especially useful for remote hosts that are not Unix
The situation of the machine.
40.ntrans[inchars[outchars]]: To set the translation mechanism for a file name character, such as NTRANS1R, the filename lll becomes RRR.
41.open Host[port]: establishes a specified FTP server connection that specifies the connection port.
42.passive: Enter passive transmission mode.
43.prompt: Set up interactive prompts for multiple file transfers.
44.proxy Ftp-cmd: In a secondary control connection, an FTP command is executed that allows two FTP servers to be connected to transfer files between two servers. The first FTP command must be open to establish a connection between two servers first.
45.put Local-file[remote-file]: Transfer local file Local-file to a remote host.
46.pwd: Displays the current working directory of the remote host.
47.quit: With bye, exit FTP session.
48.quote arg1,arg2 ... : sends the parameter verbatim to the remote FTP server, such as: Quote Syst.
49.recv Remote-file[local-file]: with Get.
50.reget Remote-file[local-file]: similar to get, but if local-file exists, it is resumed from the last transmission interrupt.
51.rhelp[cmd-name]: Request help for a remote host.
52.rstatus[file-name]: If no filename is specified, the status of the remote host is displayed, otherwise the file status is displayed.
53.rename[from][to]: Change the remote host file name.
54.reset: Clear answer queue.
55.restart marker: From the specified logo marker, restart get or put, such as: Restart 130.
56.rmdir dir-name: Deletes the remote host directory.
57.runique: Set file name uniqueness store, if file exists, then add suffix to the original file. 1,.2 and so on.
58.send Local-file[remote-file]: same put.
59.sendport: Set the use of the port command.
60.site arg1,arg2 ... : sends the parameter as a site command to a remote FTP host verbatim.
61.size file-name: Displays the remote host file size, such as: Site Idle 7200.
62.status: Displays the current FTP status.
63.struct[struct-name]: Sets the file transfer structure to struct-name, using the stream structure by default.
64.sunique: Set the remote host file name store to unique (corresponds to Runique).
65.system: Displays the operating system type of the remote host.
66.tenex: Sets the file transfer type to the desired type for the Tenex machine.
67.tick: Sets the byte counter at the time of transmission.
68.trace: Set package tracking.
69.type[type-name]: Set file Transfer Type to Type-name, default to ASCII, such as: Type binary, set binary transmission mode.
70.umask[newmask]: Sets the default umask for the remote server to Newmask, such as: Umask 3.
71.user User-name[password][account]: to the remote host to indicate their identity, require a password, you must enter a password, such as: User anonymous my@email.
72.verbose: The-v parameter with the command line, that is, to set up a detailed report, all responses to the FTP server will be displayed to the user, and the default is on.
73.? [cmd]: With help.

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.