Network security-windows batch processing

Basic simple commands

1.重定向
>>为追加
>为覆盖
echo ddd > zft.txt


2.type查看文件

type zft.txt

type zft.txt | more 分页显示


3.
rd . /s/q
cd ..\


4.写入多行
copy con zft.txt 复制zft.txt到屏幕
copy con 文件名.扩展名
开始编辑内容
ctrl+z结束编写


5.移动
move hah.txt  c:\


6.删除文件
del 路径/文件名

批量删文件
del *.txt 删除所有txt结尾的

del *.* 删除该目录所有


7.md feifei      创建文件
attrib +h feifei 隐藏文件
attrib +h +s +a feifei 隐藏文件  h隐藏文件夹  s提升为受保护的文件夹    a别人只有读权限
attrib -h feifei 取消隐藏文件

dir /a 观看包括隐藏文件夹

8.修改关联性
assoc .txt=exefile   所有txt改为exe
assoc .txt=txtfile   改回来

9.关机
shutdown -s -f -t 时间 定时关机 -c "显示内容"
shutdown -r -f -t 时间 定时重起
shutdown -a 取消关机


10.修改文件名
ren 旧名字 新名

Batch processing

The role of windows batch
processing each command in batches from top to bottom until the last one is executed

How to create a batch
file name. Bat
method: create a new notepad file, and then change the extension to .bat

Batch processing basics

1.@echo off function:

  • Turn off the echo function, that is, the shielding process, it is recommended to put it in the first line of the batch

Insert picture description here
Insert picture description here

2.pause

  • Generally batch processing will automatically close for you, so the purpose of making a pause is to prevent automatic closing

3.title title
Insert picture description here

4.echo.

  • When executing a batch script, you can leave a blank line

5.set sets variables, commonly used in the interactive assignment of scripts

  • Interactive assignment: set /p time=Please enter the time:

  • Reference variable: %time%

time is the controllable variable entered by the user

Case
@echo off
title applet
echo =========
echo Welcome to use
echo =========
set /p time=Please enter the time:
shutdown -s -f -t %time%
echo The timer shutdown has been set, thank you for using
pause

6: Usage with goto
: the tag name
goto realizes the jump

Example:
@echo off
title Shut down at a fixed time
color 0a
:menu
echo =================
Echo menu
echo 1. Timed shutdown
echo 2. Cancel shutdown
echo 3. Exit
echo == ================

set /p num=Your choice:
if “%num%” == “1” goto 1
if “%num%” == “2” goto 2
if “%num%” == “3” goto 3
echo don’t Enter
guto menu randomly

:1
set /pa=Please enter the time (unit/second):
shutdown -s -f -t %a%
guto menu

:2
shutdown -a
guto menu

:3
exit

Batch processing syntax summary

1.@echo off function:

  • Use him in order to show only the results

2.pause

  • Use it in order not to close automatically after the program is executed

3.title title

  • Make a title for the program

4.echo.

  • When executing a batch script, you can leave a blank line

5.set sets variables, commonly used in the interactive assignment of scripts

  • Similar to extracting user input variables in a programming language , bringing in the function to use the value of the variable

6: Usage with goto

  • Similar to functions in programming languages
  • :Function name
  • go to function name call function

7. If statement

  • if “%num%” == “1” goto 1, similar to python, you can use goto to call functions, and you can also echo output content

8. The
basic form of the for statement of the for loop Windows bat script is as follows:

在cmd窗口中:for %I in (command1) do command2 
在批处理文件中:for %%I in (command1) do command2

The reason for distinguishing between the cmd window and the batch file environment is that in these two environments, the behavior of the command statement is basically the same, but the details are still slightly different. The most obvious difference is: In the cmd window, the formal variable I after for must be quoted with a single percent sign, that is, %I; while in a batch file, the formal variable I must be quoted with a double percent sign, that is, %%I. For the sake of convenience, if not particularly emphasized, the following explanations will take the batch file environment as an example.
Let's first take a look at the basic elements of the for statement:

for, in, and do are the keywords of the for statement, and all three of them are indispensable;
%%I is a reference to the formal variable in the for statement, even if the variable l does not participate in the execution of the statement in the statement after do, it must appear的;
After in, the parentheses before do cannot be omitted;
command1 represents a string or variable, and command2 represents a string, variable or command statement;

Let's look at a demo of a Windows bat script (denoted as demo1):

@echo off
for  %%I in (ABC) do echo %%I
pause

Save it as a .bat file (batch file) and execute it. You will see this message in the pop-up batch window:

Insert picture description here
The for loop for batch files is so simple. Let's take a look at the precautions for the for statement and run a more complex for loop example.

The formal variable I of the for statement can be replaced with any of the 26 letters. These letters are case-sensitive, that is, %%I and %%i will be regarded as not the same variable; the formal variable I can also be changed However, in order not to conflict with the 10 formal variables of %0~%9 in batch processing, please do not replace %%I with any one of %%0~%%9;
in and The string or variable represented by command1 between do can be one or multiple. Each string or variable is called an element. Between each element, use the space bar, tab, and comma. , Semicolon or equal sign;
for statement extracts each element in command1 in turn, assigns its value to the formal variable I, and takes it to command2 after do to participate in the execution of the command; and extracts only one element at a time, and then executes The command statement after a do, regardless of whether this element is brought to command2 to participate in the operation of command2; when the statement after do is executed once, the next element in command1 is extracted, and command2 is executed again, and so on. Until all the elements in command1 have been extracted, the for statement declares the end of execution.

With the above foundation, let's take a look at the following example. This example modifies part of the content in demo1 (denoted as demo2), and the result will be very different:

@echo off
for  %%I in (A,B,C) do echo %%I
pause

The results of the operation are as follows:

Insert picture description here

If the dot in the string of bbs.bathome.cn is replaced with a space, tab or equal sign, the execution result will be the same as the execution result of demo2.

Now, let’s analyze the execution process of the for statement in the demo2 code:

1. The for statement uses a comma as a separator to divide the string A, B, C into three elements: A, B, and C, which determines that the statement after do will be executed 3 times;

2. The first execution process is like this: first take the string bbs as the value of the formal variable I, and bring it into the statement after do to be executed, that is, execute the echo %%I statement, and the value of I at this time is A , Therefore, the result of the first execution will display the string A on the screen; the second execution is the same as the first execution, except that the value of I has been replaced by the value in command1 at this time The second element is the string B; in this way, after the third echo is executed, the entire for statement is executed. At this time, the next statement, which is the pause command, will be executed.

Advanced usage:

1) Search what files are in the current directory?

@echo off
for %%i in (*.*) do echo "%%i"
pause

2) Search all text files in the current directory?

@echo off
for %%i in (*.txt) do echo "%%i"
pause

Batch practice

@echo off
title 测试
color 0a
:menu
echo ===========================
echo 	   菜单
echo 	1.查看ip
echo 	2.查看端口
echo 	3.创建一个用户
echo		4.查看当前用户
echo		5.退出
echo ===========================


set /p num=您的选择:
if "%num%" == "1" goto 1
if "%num%" == "2" goto 2
if "%num%" == "3" goto 3
if "%num%" == "4" goto 4
if "%num%" == "5" goto 5
echo 别TM乱输入
goto menu 

:1
ipconfig
goto menu

:2
netstat -ano
goto menu

:3
net user zft zft /add
goto menu

:4
net user
goto menu

:5
exit

Guess you like

Origin blog.csdn.net/weixin_44110913/article/details/109173676