Case study of Bat Script Processing Over ftp

Source: Internet
Author: User
Tags ftp commands ftp file ftp site remote ftp server ftp client runique

Preface:
The company has hundreds of windows servers, and every time the program is updated, if it is a copy of data, just afraid to arrange ten people, not necessarily done in a day, so we must study a fully automatic update solution, so I thought of FTP. I made a master FTP site and started the BAT script on hundreds of clients every 30 seconds. If the master site has an update program, I will automatically download the update, within a few minutes, all the 400 windows servers can be fully automated and updated, which is quite practical. Now I have posted the script idea of BAT, hoping to provide a direction for a large number of O & M personnel. Of course, each company has its own solution to batch Update servers. This BAT method is not optimal. You can use shell, perl, or python scripts to solve the problem.

I. FTP command description

1. In windows, FTP is a program. If FTP commands are directly called in the bat script, the command line window will pop up during execution, and the cursor will be directed at> FTP, therefore, you need to use-S: filename in another way. For more commands, enter-h.

2. After FTP logon is successful, use the get command to download a single remote file. If multiple files are downloaded with mget, it supports wildcards. When mget is used, select Y/N for each file. If you want to download all files without interaction, enter the prompt command once to disable the interaction mode.

Note the following two issues for the Mput and mget commands:

I. the target file cannot be specified as a life-saving word. All files on the command line are regarded as source files.
Ii. mput and mget commands cannot upload or download directories.
Iii. Prompt: every time you execute this command, it loops between on and off. When the ftp script starts, prompt is on by default. Therefore, when uploading or downloading files, run the prompt once to turn off the interactive on and off.

3. file transmission mode:
Binary, Binary transmission
Ascii, ascII Transmission
During FTP file transmission, ASCII files written in HTML and text are transmitted, while binary code transmission can transmit texts and non-texts (Execution files, compressed files, images, etc.), which is universal, binary code transmission is faster than ASCII code transmission. Therefore, when creating a bat script, enter the bin command to enable binary transmission. If non-text files are transmitted in ASCII mode, a bunch of garbled characters may be displayed. If you upload some CGI scripts, you may not be able to run these scripts, server 500 error is displayed in the browser.

The difference between the Ascii and binary modes is the processing of carriage return line breaks. Binary Mode does not process any data. asci mode converts the carriage return line to the carriage return character of the local machine, for example, \ n in Unix and \ r \ n in Windows, \ r for Mac. The unix system downstream Terminator is a byte, that is, the hexadecimal 0A, while the ms system is two bytes, that is, the hexadecimal 0D0A.

Therefore, when you use ascii to download files from the unix ftp server (whether binary or text files, redhat's vsftp is binary by default, and ascii is disabled by default ), every time a byte is detected as 0A, A 0D will be automatically inserted. Therefore, if your file is a binary file, such as an executable file or a compressed package, it will definitely not be used. If your file is a unix text file, you are correct in ascii mode. If binary mode is misused, you can see that there is no line feed on windows, there are black blocks.
When using an FTP client to transfer files, because these software is more intelligent, it can automatically enable the transmission mode based on the suffix, so do not worry about problems.

Ii. instance verification:

Environment Description: There is an FTP server (for windows) on 192.168.133.34. The FTP user name is lgh and the password is www.jb51.net. The root directory of this user is D: \ lgh. See. SQL .rar is SQL. for bat compressed files, please pay attention to the figure. I will reference this file in the following instances and interpret ftp commands such as mget, get, prompt, bin, and ascii in a clear and white manner.

Example 1: Use BAT to call the FTP command

Ftpbat. bat content
(*** You cannot create a bat file named ftp. bat. There will be a problem during execution ***)Copy codeThe Code is as follows: ftp
Open 192.168.133.34
Lgh
Www.jb51.net
Get SQL .rar
Bye

From the results shown in the figure, the bat script cannot directly call the FTP command of xp. During execution, H: \> ftp will keep endless loops. The XP system here is SP3, and other versions are not tested. If there are differences, please leave a message.

Example 2: Use put and get to upload and download a single object

Upload the file flower.zip in the directory H: \ jsdirectory on the XP Server to the FTP server.
Download the SQL .rar file in the ftpserver to the H: \ JS directory on the XP Server
The ftp-get-put.bat content is as follows:Copy codeThe Code is 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 the following two figures. From the perspective of the XP system, the JS directory of the H drive has multiple SQL .rars and the root directory on the ftpserver has multiple flower.zip files. This shows that the script has been executed properly and has achieved the expected purpose.

Example 3: Use put and get to upload and download multiple files

Copy flower.zipto the Windows XP Machine and rename it flower2.zip.
Delete the SQL .rar file downloaded for the second time on the Windows server to avoid affecting this operation.
Delete the flower.zip file on the ftpserver for the second time, so as not to affect this operation.
Upload the files flower.zip and flower2.zip in the directory H: \ jsdirectory on the XP Server to the FTP server.
The ftp-get-put-many.bat content is as follows:Copy codeThe Code is 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 whether flower.zipand flower2.zip have been uploaded successfully in the ftpserver root directory. As shown in the figure, the flower2.zip file is successfully uploaded.

In the ftp-get-put-many.bat script
Echo put flower.zip flower2.zip> "% ftpfilename %"
Flower2.zip is the last file to be transferred. Does it mean that bat only recognizes the last file when processing the upload. We will add another file flower3.zip for verification.

Copy flower.zipto the Windows XP Machine and rename it flower3.zip.
Delete the flower2.zip file on the ftpserver for the second time, so as not to affect this operation.

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

Copy codeThe Code is 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 %"

Finally, flower2.zip is successfully uploaded, because when bat is double-clicked, the execution process will flash, and we cannot see the execution process. Go to the command line to execute it and see what went wrong.

Sorry, I don't know. Do you have any friends? Direction

In another way, use the wildcard * to try.
The ftp-get-put-many.bat content is modified as follows:

Copy codeThe Code is 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 to avoid affecting this operation.
The ftp-get-put-many.batcommand is executed, and only the flower.zip file is uploaded successfully. Why can't I upload a flower3.zip file at the same time .???

In fact, the use of put transmission of multiple files, completely replace the repeated way, for example: ftp-get-put-many.bat content modification is as follows:

Copy codeThe Code is 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 4: Use mput and mget to upload and download multiple files

Delete files related to flower on the FTP server

The ftp-mget-mput-many.bat content is as follows:

Copy codeThe Code is 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 that the system after a window pops up, stuck, stopped

The reason is that the mput command is used, but the Prompt is not used to close the interaction.

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

Copy codeThe Code is 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 %"

Execute the ftp-mget-mput-many.bat again. The results are as follows:

Use wildcard *
Delete files related to flower on the FTP server
The ftp-mget-mput-many.bat content is modified as follows:

Copy codeThe Code is 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 %"

Upload the flower *. zip file. Because the results are the same as those in the previous step, we will not capture the image.

Example 5: use ascii to transfer non-text files between windows systems

Upload RMB .jpg on Windows XP to the root directory of the FTP server through ascii.

The ftp-get-ascii.bat content is modified as follows:

Copy codeThe Code is 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, you will find that the RMB .jpg file can be uploaded normally and opened normally. Then, this phenomenon is different from the preceding ascii file which cannot be transmitted without text. How can this problem be solved.
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 transfers files using FTP in the windows system, whether it is using ascii, you can still use binary. Next, let's perform another experiment. Select centos on the FTP server, and then test the non-text file transmission problem of ascii.

Example 6: use ascii to transmit non-text files between windows and linux systems

Create a vsftpd server on the RHEL 5.4 64-bit System

As mentioned above, redhat enables binary transmission by default and disables ascii code transmission. Therefore, we need to modify the vsftpd configuration file and/etc/vsftpd. conf file, allows the client to perform ascii transmission, and restart vsftpd

Put RMB .jpg in the FTP server, you pay attention to the H: \ js directory only under the ftp-getascii.bat File

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

Copy codeThe Code is 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. check that the RMB .jpg file has been downloaded to the XP computer. Double-click the file and find that the image is garbled and the file is damaged, all of which are caused by ascii code transmission.

Change bin transmission, test it
Hosts file.

Modify the ftp-getascii.bat to convert ascii to binary

Copy codeThe Code is 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 and the image is displayed normally.

Additional reading:
The internal commands used by ftp are as follows (brackets indicate optional ):
1 .! [Cmd [args]: Execute the interactive shell on the local machine and exit to return to the ftp environment, for example :! Ls *. zip.
2. $ macro-ame [args]: Execute macro to define macro-name.
3. account [password]: Provide the supplemental password required to access system resources after logging on to the remote system.
4. append local-file [remote-file]: append the local file to the remote system host. If the remote system file name is not specified, the local file name is used.
5. ascii: Use the ascii type transmission method.
6. bell: after each command is executed, the computer rings once.
7. bin: Binary File Transfer Mode.
8. bye: exit the ftp session.
9. case: when using mget, convert uppercase letters in the remote host file name to lowercase letters.
10. cd remote-dir: Enter the remote host directory.
11. cdup: Enter the parent directory of the remote host directory.
12. chmod mode file-name: Set the file-name access mode of the remote host file to mode, for example, chmod 777 a. out.
13. close: interrupt the ftp session with the remote server (corresponding to open ).
14. cr: When a file is transmitted using asscii, the carriage return line is converted into a return line.
15. delete remote-file: delete remote host files.
16. debug [debug-value]: sets the debugging mode. Each Command sent to the remote host is displayed, for example, deb up 3. If it is set to 0, the debug is canceled.
17. dir [remote-dir] [local-file]: displays the remote host directory and saves the result to the local file.
18. disconnection: Same as close.
19. form format: sets the file transmission mode to format. The default mode is file.
20. get remote-file [local-file]: transfers the remote-file of the remote host to the local-file of the local hard disk.
21. glob: Set the extension of the mdelete, mget, and mput file names, which is the same as the-g parameter in the command line.
22. hash: A hash symbol (#) is displayed for every 1024 bytes transferred (#).
23. help [cmd]: displays the help information of the ftp Internal Command cmd, for example, help get.
24. idle [seconds]: Set the Sleep timer of the remote server to [seconds] seconds.
25. image: sets the binary transmission mode (the same as binary ).
26. LCD [dir]: Switch the local working directory to dir.
27. ls [remote-dir] [local-file]: displays the remote Directory remote-dir and stores the local-file.
28. macdef macro-name: defines a macro. When an empty row under macdef is encountered, the macro definition ends.
29. mdelete [remote-file]: delete remote host files.
30. mdir remote-files local-file: similar to dir, but multiple remote files can be specified, such as mdir *. o. *. zipoutfile.
31. mget remote-files: Transfers multiple remote files.
32. mkdir dir-name: create a directory on the remote host.
33. mls remote-file local-file: Same as nlist, but multiple file names can be specified.
34. mode [modename]: sets the file transmission mode to modename. The default mode is stream.
35. modtime file-name: displays the last modification time of the remote host file.
36. mput local-file: Transfers multiple files to the remote host.
37. newer file-name: if the modification time of file-name on the remote machine is closer than that of files with the same name on the local hard disk, the file will be re-transmitted.
38. nlist [remote-dir] [local-file]: displays the list of files in the remote host directory and stores the local-file on the local hard disk.
39. nmap [inpattern outpattern]: sets the file name ing mechanism so that some characters in the file are converted to each other during file transmission, such as nmap $1. $2. $3 [$1, $2]. [$2, $3], transfer the file a1.a2. when a3, the file name is changed to a1, a2. This command is particularly applicable to non-UNIX remote hosts.
Machine status.
40. ntrans [inchars [outchars]: sets the file name character translation mechanism, for example, ntrans1R, then the file name LLL will change to RRR.
41. open host [port]: Specifies the ftp server connection. You can specify the connection port.
42. passive: enters the passive transmission mode.
43. prompt: Set interaction prompts when multiple files are transferred.
44. proxy ftp-cmd: Execute an ftp command in the secondary control connection. This command allows two ftp servers to be connected to transfer files between the two servers. The first ftp command must be open to first establish a connection between two servers.
45. put local-file [remote-file]: transfers the local-file to the remote host.
46. pwd: displays the current working directory of the remote host.
47. quit: Same as bye, quit the ftp session.
48. quote arg1, arg2. ..: Send the parameter to the remote ftp server, for example, quote syst.
49. recv remote-file [local-file]: Same as get.
50. reget remote-file [local-file]: similar to get. However, if local-file exists, it will be resumed from the last transmission interruption.
51. rhelp [cmd-name]: request for help from the remote host.
52. rstatus [file-name]: If no file name is specified, the remote host status is displayed; otherwise, the file status is displayed.
53. rename [from] [to]: Change the remote host file name.
54. reset: Clear the answer queue.
55. restart marker: Start get or put again from the specified mark marker, for example, restart 130.
56. rmdir dir-name: Delete the remote host directory.
57. runique: Set the unique storage of file names. If the file exists, add the suffix... 1 and. 2 after the original file.
58. send local-file [remote-file]: Same as put.
59. sendport: Set the PORT command.
60. site arg1, arg2. ..: Send the parameter to the remote ftp host as the SITE command.
61. size file-name: displays the file size of the remote host, for example, site idle 7200.
62. status: displays the current ftp status.
63. struct [struct-name]: sets the file transmission structure to struct-name, and uses the stream structure due to lack of time.
64. sunique: Set the remote host file name storage to unique (corresponding to runique ).
65. system: displays the operating system type of the remote host.
66. tenex: Set the file transfer type to the required type of the TENEX server.
67. tick: sets the byte counter during transmission.
68. trace: Set package tracing.
69. type [type-name]: sets the file transfer type to type-name. The default value is ascii, for example, type binary. Sets the binary transfer mode.
70. umask [newmask]: Set the default umask of the remote server to newmask, for example, umask 3.
71. user-name [password] [account]: indicates your identity to the remote host. If you need a password, enter the password, for example, user anonymous my @ email.
72. verbose: Same as the-v parameter of the command line, that is, set the detailed report mode. All responses of the ftp server will be displayed to the user. The default value is on.
73 .? [Cmd]: Same as 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.