Batch processing common network commands and symbolic articles _dos/bat

Source: Internet
Author: User
Tags echo command goto

2.1 Learn ping ipconfig arp start shutdown taskkill command

Learning points:
1. Test IP Connection: Ping
2. View IP configuration: ipconfig
3. Resolve Address: ARP
4. Open command: Start
5. To shut down or restart the computer: shutdown
6. Closes the specified process Taskkill

One. Test IP connection: Ping

A command to check whether the network is smooth or network connection speed. Here, we only know some basic parameters on it.

-N Count sends the number of ECHO packets specified by count.
Defines the number of packets sent to the destination IP, which defaults to 4 times. This command allows you to gather some information. such as the average time the packet returns,
The quickest time is how much, the slowest time is how many and so on.


Example 1
C:\Documents and Settings\mzybar>ping-n 3 www.baidu.com

Pinging www.jb51.net [121.14.88.14] with bytes of data:

Reply from 121.14.88.14:bytes=32 time=3ms ttl=57
Reply from 121.14.88.14:bytes=32 time=3ms ttl=57
Reply from 121.14.88.14:bytes=32 time=2ms ttl=57

Ping statistics for 121.14.88.14:
Packets:sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trips times in Milli-seconds:
Minimum = 2ms, Maximum = 3ms, Average = 2ms


Let's analyze It:

Ping statistics for 121.14.88.14:
Packets:sent = 3, Received = 3, Lost = 0 (0% loss),
From here you can know that in the process of sending 3 packets to www.baidu.com, 3 returned, no packet lost.

Approximate round trips times in Milli-seconds:
Minimum = 2ms, Maximum = 3ms, Average = 2ms
The 3 packets have the fastest return speed of 2ms, the slowest is 3ms, and the average speed is 2ms.


Example 2
Ping-n 127.0.0.1 >nul
Ping this machine 11 times, can be used for batch processing delay of 10 seconds. The >nul in the command is the masked output.
The short type can be written as:
Ping-n 127.1 >nul

-W timeout Specifies the time-out interval in milliseconds.

Example 3
Ping 1-n 1-w 10000 2>nul 1>nul
The batch can be used to delay 10 seconds.

Two. View IP configuration: ipconfig

Because of the frequent interception of TCP/IP configuration information for a computer in batches, let's just talk about/all parameters.

/ALL parameters
Function: Displays full TCP/IP configuration information for all adapters. When using ipconfig with no parameter options, it is configured for each
The interface displays the IP address, subnet mask, and default gateway value.

Example 1
C:\Documents and Settings\mzybar>ipconfig

Windows IP Configuration


Ethernet Adapter Local Area Connection:

Connection-specific DNS Suffix. :
IP address. ...........:192.168.0.73
Subnet Mask ........... : 255.255.255.0
Default Gateway ..... . : 192.168.0.254

The information shown here is:
1. The local connection name of the computer is: local connection
2. The native IP is: 192.168.0.73
3. Subnet mask is: 255.255.255.0
4. The default gateway is: 192.168.0.254


Example 2
C:\Documents and Settings\mzybar>ipconfig/all

Windows IP Configuration

Host name ...: A-073//Computer name (...)
Primary Dns Suffix ...:
Node Type ............:unknown
IP Routing Enabled ... : No
WINS Proxy Enabled ... : No

Ethernet Adapter Local Area Connection://computer's local connection name

Connection-specific DNS Suffix. :
Description ........... : Realtek rtl8168/8111 pci-e Gigabit E//NIC model
Thernet NIC
Physical Address ..... . : 00-e0-4c-59-9b-57//Nic MAC address
Dhcp Enabled ...... : No
IP address ...: 192.168.0.73//IP addresses, and ...
Subnet Mask ........... : 255.255.255.0//Subnet mask
Default Gateway ..... . : 192.168.0.254//Default gateway address
DNS Servers ........... : 61.144.56.100//Primary DNS address
202.96.128.166//Vice DNS address

When the ipconfig command has a/all parameter, the full TCP/IP configuration information for all adapters is displayed, and if your computer has multiple network adapters, this life
The order will list all their information.

Three. Resolve Address: ARP

Displays and modifies items in the Address Resolution Protocol (ARP) cache. The ARP cache contains one or more tables that are used to store IP addresses and their
The resolved Ethernet or Token Ring physical address. Each Ethernet or Token Ring network adapter installed on the computer has its own separate table.


-a parameter or the-G parameter, which is used to view all items in the cache. The results of the-A and-G parameters are the same, for years G has been on UNIX platforms
The option to display all items in the ARP cache, and Windows uses ARP-A (-a can be considered all, meaning all), but it can also
Accept the more traditional-g option.

Arp-a IP
If we have more than one network card, then using Arp–a plus the IP address of the corresponding network card, you can display only the ARP cache items associated with the network adapter.
In the default IP case, all current ARP cache tables are displayed.

Example 1
C:\Documents and Settings\mzybar>arp-a

interface:192.168.0.20---0x2
Internet Address Physical Address Type
192.168.0.1 00-1a-92-90-19-fe Static
192.168.0.2 00-1a-92-90-19-eb Static
192.168.0.3 00-1a-92-49-1c-a9 Static
192.168.0.4 00-1a-92-90-19-32 Static
192.168.0.5 00-1a-92-38-07-09 Static
192.168.0.252 00-23-54-a9-f5-4a Dynamic
192.168.0.253 00-1d-60-1d-aa-d1 Dynamic
192.168.0.254 00-0a-eb-0d-96-ff Dynamic

Analyze the data above:
Internet Address: Represents IP addresses.
Physical Address: Represents the physical location of the network card, that is, the MAC address of the NIC.
Type: Represents the types of the ARP table, "Dynamic" is animated, and "Static" is static.

Arp-s IP Physical Address
We can manually enter a static project into the ARP cache. The project will remain in a valid state during the computer boot process, or in the event of an error
In the event of a mistake, the manually configured physical address will automatically update the item. This command enables ARP binding.

Example 2
Arp-s 192.168.1.1 00-1a-92-90-19-fe
Adds a static ARP implementation ARP binding to 192.168.1.1.

Arp-d IP
Use this command to manually delete a static project. You can enter the arp-d command, and the default IP will delete the current computer's ARP table.
In dealing with ARP spoofing, we usually delete the ARP table of the current computer before we implement the ARP binding.

Example 3

@echo off
arp–d
arp–s 192.168.0.1 00-1a-92-90-19-fe
arp–s 192.168.0.2 00-1a-92-90-19-eb
arp–s 192.168.0.3 00-1a-92-49-1c-a9
arp–s 192.168.0.4 00-1a-92-90-19-32
arp–s 192.168.0.5 00-1a-92-38-07-09 Pause

Four. Open command: Start

Basic format: Start "title" FileName
Where title can be omitted, filename such as with spaces or special symbols, with "" enclosed.

Example 1
Start http://www.baidu.com
Here is the open Baidu, title omitted.

Example 2
Start "" "%windir%\system32\notepad. EXE ""%userprofile%\ Desktop \abc.txt
Use Notepad (Notepad) to open the Abc.txt on the desktop.

Example 3
Start ""%userprofile%\ Desktop \abc.doc

This is followed by "" To start the file Abc.doc, otherwise it will not start.
If there is no "" after start, the title is omitted, and if the file name is "", then start will think of it as a title, thus becoming an omitted
FileName, the default is to open cmd. So adding "after start" can prevent this kind of accident.


The parameters/min and/max make the open window minimized and maximized.

Example 4
Start/min "" "%windir%\system32\notepad. EXE ""%userprofile%\ Desktop \abc.txt
Minimize the Abc.txt to open the desktop with Notepad.

Five. Shut down or reboot the computer: shutdown

Usage: Shutdown [-i |-L | s |-r |-a] [-f] [-M \\computername] [-t XX] [-c ' comment '] [D-UP:XX:YY]

No parameters to display this message (same as?)
-I display GUI interface, must be the first option
-L logoff (cannot be used with option-m)
-S shutdown this computer
-R shutdown and restart this computer
-A abort system shutdown
-M \\computername remote computer shutdown/reboot/Abort
-T XX setting the timeout for shutdown is xx seconds
-C "comment" closes comment (maximum 127 characters)
-F forced-run application shutdown without warning
-D [u][p]:xx:yy shutdown reason code
U is user code
P is a planned shutdown code
XX is a primary reason code (a positive integer less than 256)
YY is a secondary reason code (a positive integer less than 65536)


Example 1
Shutdown-l
This command can only unregister native users and not for remote computers.

Example 2
At 12:00 Shutdown-s
With the-s parameter, the shutdown computer is performed at 12:00.

Example 3
Shutdown-r-T 60
Reboot the computer after 60 seconds. Here is the set reboot countdown for 60 seconds. If the time is not set, the default is 30 seconds.

Example 4
Shutdown-f-S-T 300
Force the computer to shut down after 300 seconds with the-f argument.

Example 5
Shutdown-a
We can use SHUTDOWN-A to terminate the current shutdown when we encounter a shockwave or shock wave virus or execute the shutdown command above.
So that you can avoid the loss caused by shutting down the machine.

Six. Close the specified process Taskkill

The Taskkill command can end one or more tasks or processes based on the process ID or image name.

/im imagename
Specifies the image name of the process that will terminate.
Example 1
Taskkill/im Qq.exe

/F Process
Specifies the process that will be forced to terminate. For remote processes to ignore this parameter, all remote processes will be forced to terminate.
Example 2
taskkill/f/im Spoolsv.exe

/PID Process ID Specifies the PID of the processes to terminate
Example 3
Taskkill/pid 1230/pid 1241/pid 1253
This is the process of terminating the PID 1230 1241 1253, respectively.

On the DOS Network command is still quite a lot, some are very powerful such as regsvr32,netsh,sc,wmic, and so on, these are not much said, hope
Do a lot of searching for the relevant information, ^&^.

2.2 Learning the most common special symbols

Learning points:
1. >, >> redirection characters
2.| Command pipe character
3. &, &&, | | Group command
4. ^ Escape characters
5. % variable Guide
6. "" Define the character

First, redirect >, >>

The > redirector, which means passing and overwriting, has the effect of passing the results of the run to a later range (either a file or an implied
Recognized system console, which is the command prompt.

Example 1
Echo hello120 >1.txt
New file 1.txt, the content is "hello120" (Note: There is a space at the end of the line, a blank line after the file)

Example 2
(Echo hello120) >1.txt new file 1.txt, the content is "hello120" (Note: There is no space at the end of the line, but a blank line after the file)
>1.txt echo hello120 New file 1.txt, the content is "hello120" (Note: There is no space at the end of the line, but a blank line after the file)

Example 3
>1.txt <nul set/p=hello120 or >1.txt Set/p=hello120<nul
New file 1.txt, content is "hello120" (Note: There is no space at the end of the line, and no blank lines after the file)

Attention:
If the above example 1.txt original content, in the use of the > Redirection command, will be covered by the new content, this must be noted!


The >> redirector appends the output of the command to the device behind it. The role of this symbol and > is somewhat similar, but they are different >> are
Passed and appended at the end of the file, while > is overwritten. Use the same as >.

Example 1
Echo hello120 >>1.txt
If 1.txt does not exist, the new file 1.txt is created, the content is "hello120", and if 1.txt exists, append the content to "hello120"
To the end of the text.

Example 2
Echo hello> 1.txt
Echo World>>1.txt

The 1.txt content is as follows:
Hello
World

Two, command pipe character |
Represents the execution of a command or statement before it as a processing object for a command or statement after it, in short, by putting its previous output
For the input after it.

Example 1
tasklist | find/i "Qq.exe" && taskkill/f/im Qq.exe
Pass the tasklist (process list) to the Find command by using the pass-through "|" to perform the process of locating QQ.

Example 2
Echo y|rd/s C:\ABC
Use the Pipe command | To pass the results of Echo y to the rd/s c:\abc command, so that you can automatically enter Y.


★ Note that pipeline Command | The input method, SHIFT key \

Third, the combination of command &, &&, | |
&, &&, | | As a combination command, as the name suggests, you can combine multiple commands when a command is executed.

The & symbol allows you to execute multiple commands at the same time, when the first command fails and does not affect the execution of the command behind it. Here & the commands on both sides are sequentially executed
To be executed from after the trip.
Format: First Command & second command [& Third command ...]

Example 1
Dir z:\ & dir y:\ & dir c:\
The above command will display the contents of the Z,Y,C disk continuously, regardless of whether the disk exists.


The && symbol allows multiple commands to be executed at the same time, the following command will not be executed when the command is encountered, and if there is no error, the execution is done
All commands.
Format: First command && second command [&& Third command ...]

Example 2
Dir z:\ && dir y:\ && dir c:\
This command is similar to the above, but the difference is that when the previous command fails, the following command is not executed.


|| The symbol allows you to execute multiple commands at the same time, executing the second command when a command fails, and does not perform the following after encountering the correct command
Command and executes all commands if the correct command is not present.
Format: first Command | | The second command [| | | The Third order ...]

Example 3
tasklist | find/i "Qq.exe" && taskkill/f/im Qq.exe | | Echo, you opened QQ? I don't believe it.

Through && and | | Two symbols may be considered to have divided the above example into three commands:
First command:: tasklist | find/i "Qq.exe"
Second command: taskkill/f/im qq.exe
The third command: Echo you opened QQ? I don't believe it.

Here we analyze,
If the QQ process cannot be found, that is, the first command fails, && does not execute the second command: Taskkill command, because the Taskkill command does not
Execution, as the second command fails, then | | A third command will be executed: the echo command.
If the QQ process is found, that is, the first command succeeds, && will execute the second command: The Taskkill command, because the second command was executed successfully,
|| The third command will not be executed: the echo command.


★ Pipe Command | | Input method, enter two times Shift key \
★ Combination commands and redirection commands, pipe commands must be used with priority attention. The pipeline command has precedence over the redirection command, and the redirection command is superior
The first level is higher than the combined command.

Thinking:
List the files and folders in the C and D disks to the A.txt file.

You may immediately write down the following command:
Dir c:\ && dir d:\ > A.txt
In this way after the A.txt only D disk information! Why? Because the precedence of a combination command has no precedence over the redirection command! So the order is in.
Rows are divided into two parts: dir c:\ and dir d:\ > A.txt
Here to use the combination command && meet the requirements of the topic, we can write this:
Dir c:\ > A.txt && dir d:\ >> a.txt
In this way, according to the priority level, DOS will divide this sentence into two parts: dir c:\ > A.txt and dir d:\ >> a.txt
Of course, we can also write this:
Dir c:\ > A.txt & dir d:\ >> a.txt

Four, escape character ^
Generally, ^ appears as an escape character.

Because in the CMD environment, some characters have special functions, such as >, >> indicating redirection, | Represents pipeline,&, &&, | | Represents a statement connection ... It
They all have specific functions, and if you need to output them as characters, echo >, echo | ...... And that's going to go wrong.--cmd Interpreter will
Treat them as characters with special functions, rather than being treated as ordinary characters, this time, you need to escape processing these special characters:
Each special character is preceded by an escape character ^.
Therefore, to output these special characters, you need to use echo ^>, echo ^|, Echo ^|^|, echo ^^ ... To deal with the format;

In addition, this escape character can also be used as a continuation symbol.

Example 1
@echo off
echo This is ^
a ^
Sentence
Pause

Why is an escape character descriptors at the end of a line to act as a continuation? The reason is simple because there is also an invisible symbol at the end of each line, that is, a carriage return
Character, the escape characters at the end of the line let the carriage return invalid, thereby playing a continuation of the role.

V, variable guide%

① when the percent sign appears in pairs, and contains non special characters, generally do variable reference processing, such as:%var%,%str%

Example 1

@echo off
set STR=ABC
echo variable The value of STR is:%str%
pause

The result is displayed on the screen:
The value of the variable str is: ABC
Press any key to continue ...


② In addition, the percent semicolon as a variable reference also has a special form, that is, the reference to formal parameters, at this point, followed by a single percent of 0~9 10
Number, such as%0,% 1,%2 ...
%0 Represents the batch file itself, including the full path and extension
% 1 to%9 represents the first argument to the Nineth argument

Example 2

@echo
off if defined str goto next
set str=
set/p str= Please pull the file into this window and return: Call
"%~0"%str%
pause

: Next
CLS
echo This batch file full path is: "%~0"
echo dragged to this window the full path to the file is: "%~1"
goto:eof

When ③ appears in the SET/A statement, it means that the division of two numbers is the remainder, which is called the modulo operation, which is written in the Command Line window and in the batch file.
Slightly different: In the command Line window, only a single% is required, and in a batch file, a sequential Party semicolon is required, written as%.

For example, if you run set/a num=4%2 in a command line window, the result will show 0, because the remainder of 4 divided by 2 is 0;
If you save it as a batch file, the statement changes slightly:

Example 3

The remainder of the @echo off set/a num=4%%2 echo 4 divided by 2 is%num%
pause


④ Escape symbol: If you want to display the% itself, you need to escape it in the front with%.

Example 4

@echo off
echo a percent semicolon:%%
echo two percent sign:%%%%
echo three percent sign:%%%%%%
Pause


Six, the definition of ""

① always use "" to enclose a path in a path that represents a space or special symbol.

Example 1
CD/D CD "D:\Program Files\^%*abc"
Because there are spaces and special symbols in the path, enclose the path with "".


② indicates that the content is a complete string.

Example 2
@echo off
Set "VAR=ABC 123"
Echo%var%
Pause

This indicates that the value of Var is the string "ABC 123".

Because there are a lot of symbols, this is only said a few of the above commonly used, have the time to watch this article "batch processing commonly used symbols detailed",

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.