Windows 批处理文件插入空行/删除单行 - nmap 输出
Windows batch file to insert a blank line / remove single lines - nmap output
我正在尝试清理 nmap 结果并获得一个 cleaner/easier 来处理文件,其中只有我需要的数据 - 基本上是 IP 和打开的端口。
(不需要集成到一个解决方案中,我可以让他们一起工作)
此行清理初始输出:
findstr "Nmap open"|findstr /v "no-response"
它与 txt Nmap 和打开的端口保持一致。然后删除包含 "no-response" 的行(其中也包含 "Nmap")
这是结果输出:
Nmap scan report for 1.1.1.1
21/tcp open
22/tcp open
80/tcp open
Nmap scan report for 1.1.1.2
Nmap scan report for 1.1.1.3
22/tcp open
Nmap scan report for 1.1.1.4
80/tcp open
443/tcp open
Nmap scan report for 1.1.1.5
80/tcp open
554/tcp open
我希望该输出包含空白行,并省略没有后续打开端口的行,即:
Nmap scan report for 1.1.1.1
21/tcp open
22/tcp open
80/tcp open
Nmap scan report for 1.1.1.3
22/tcp open
Nmap scan report for 1.1.1.4
80/tcp open
443/tcp open
Nmap scan report for 1.1.1.5
80/tcp open
554/tcp open
不确定这是否可行 - 似乎应该...
虽然这个问题过于宽泛,因为它没有尝试解决这个问题,但我决定提供一个使用 undocumented findstr
feature 的脚本,即搜索行外(参见所有 rem
代码中备注):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (path and name of input file; `%~1` is first argument)
set "_BEGIN=Nmap" & rem // (expected beginning of lines; must not contain `=`, `"`)
set "_INCL=open" & rem // (expected part of subsequent lines; must not contain `"`)
set "_EXCL=no-response" & rem // (part of subsequent lines to exclude; must not contain `"`)
rem // Gather carriage-return character:
for /F %%C in ('copy /Z "%~f0" nul') do set "$CR=%%C"
rem // Gather line-feed character:
(set ^"$LF=^
%= blank line =%
^")
rem // Reset flag variable that is used to not insert line-break before first match:
set "FLAG="
rem /* Loop through matching lines; the first `findstr` command excludes all lines that contain
rem the predefined part; the second one uses the feature of `findstr` to match beyond lines
rem when the search string contains line-breaks, although only the portion up to the (first)
rem line-break is returned; hence the first search string ensures that there is at least one
rem line with the prefedined part in between the lines with the predefined beginning; and
rem the second search string matches the lines with the predefined parts in between: */
for /F "delims=" %%L in ('
cmd /V /C findstr /V /R /C:" !_EXCL!\>" "!_FILE!" ^| ^
cmd /V /C findstr /R /C:"^^!_BEGIN! *..*!$CR!!$LF!..* * !_INCL!\>" /C:" !_INCL!\>"
') do (
rem // Store current line string:
set "LINE=%%L"
rem // Toggle delayed expansion to avoid trouble with `!`:
setlocal EnableDelayedExpansion
rem // Check whether current line starts with predefined beginning:
if "!_BEGIN!!LINE:*%_BEGIN%=!"=="!LINE!" (
rem // Line starts with predefined beginning, hence conditionally return line-break:
if defined FLAG echo/
rem // Output current line string:
echo(!LINE!
endlocal
rem // Set flag to insert line-breaks before further matches:
set "FLAG=#"
) else (
rem // Line does not start with predefined beginning, so just output current line string:
echo(!LINE!
endlocal
)
)
endlocal
exit /B
鉴于脚本存储为prettify-nmap-log-file.bat
使用以下命令行处理某个输入文件(如log.nmap
):
prettify-nmap-log-file.bat "log.nmap"
要将结果写入另一个文件(如 log.nmap.txt
),请使用 redirection:
prettify-nmap-log-file.bat "log.nmap" > "log.nmap.txt"
试一试 batch-file,仅修改文本文件的双引号路径后,在线 5
:
@(Set LF=^
% 0x0A %
)
@For /F %%# In ('Copy /Z "%~f0" NUL')Do @Set "CR=%%#"
@Call :Sub "%UserProfile%\Desktop\dummy.txt"
@GoTo :EOF
:Sub
@Set "#=#"
@(For /F Delims^=^ EOL^= %%# In ('^""%__AppDir__%cmd.exe"/V/U/D/C""%%__AppDir__%%findstr.exe"/I "^^^^Nmap ^^^^[0-9]*/.*open\^^^>" "%~1"|"%%__AppDir__%%findstr.exe"/IV "^^^^Nmap.*!CR!!LF!Nmap.*"|"%%__AppDir__%%findstr.exe"/IV "^^^^[0-9N].*!CR!!LF![^^^^\ ]""^"')Do @Echo("%%#"|"%__AppDir__%findstr.exe"/I "^.N">NUL&&(If Not Defined # (Echo(&Echo(%%#)Else Echo(%%#&Set "#=")||Echo(%%#)>"%~dpn1_filtered%~x1"
最后一行看起来非常长,因为我包含了文件的完整路径,这些文件通常可以使用 %PATH%
和 %PATHEXT%
变量访问。如果那些重要的变量发生了某些事情,这只是防止它失败。
我正在尝试清理 nmap 结果并获得一个 cleaner/easier 来处理文件,其中只有我需要的数据 - 基本上是 IP 和打开的端口。
(不需要集成到一个解决方案中,我可以让他们一起工作)
此行清理初始输出:
findstr "Nmap open"|findstr /v "no-response"
它与 txt Nmap 和打开的端口保持一致。然后删除包含 "no-response" 的行(其中也包含 "Nmap")
这是结果输出:
Nmap scan report for 1.1.1.1
21/tcp open
22/tcp open
80/tcp open
Nmap scan report for 1.1.1.2
Nmap scan report for 1.1.1.3
22/tcp open
Nmap scan report for 1.1.1.4
80/tcp open
443/tcp open
Nmap scan report for 1.1.1.5
80/tcp open
554/tcp open
我希望该输出包含空白行,并省略没有后续打开端口的行,即:
Nmap scan report for 1.1.1.1
21/tcp open
22/tcp open
80/tcp open
Nmap scan report for 1.1.1.3
22/tcp open
Nmap scan report for 1.1.1.4
80/tcp open
443/tcp open
Nmap scan report for 1.1.1.5
80/tcp open
554/tcp open
不确定这是否可行 - 似乎应该...
虽然这个问题过于宽泛,因为它没有尝试解决这个问题,但我决定提供一个使用 undocumented findstr
feature 的脚本,即搜索行外(参见所有 rem
代码中备注):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (path and name of input file; `%~1` is first argument)
set "_BEGIN=Nmap" & rem // (expected beginning of lines; must not contain `=`, `"`)
set "_INCL=open" & rem // (expected part of subsequent lines; must not contain `"`)
set "_EXCL=no-response" & rem // (part of subsequent lines to exclude; must not contain `"`)
rem // Gather carriage-return character:
for /F %%C in ('copy /Z "%~f0" nul') do set "$CR=%%C"
rem // Gather line-feed character:
(set ^"$LF=^
%= blank line =%
^")
rem // Reset flag variable that is used to not insert line-break before first match:
set "FLAG="
rem /* Loop through matching lines; the first `findstr` command excludes all lines that contain
rem the predefined part; the second one uses the feature of `findstr` to match beyond lines
rem when the search string contains line-breaks, although only the portion up to the (first)
rem line-break is returned; hence the first search string ensures that there is at least one
rem line with the prefedined part in between the lines with the predefined beginning; and
rem the second search string matches the lines with the predefined parts in between: */
for /F "delims=" %%L in ('
cmd /V /C findstr /V /R /C:" !_EXCL!\>" "!_FILE!" ^| ^
cmd /V /C findstr /R /C:"^^!_BEGIN! *..*!$CR!!$LF!..* * !_INCL!\>" /C:" !_INCL!\>"
') do (
rem // Store current line string:
set "LINE=%%L"
rem // Toggle delayed expansion to avoid trouble with `!`:
setlocal EnableDelayedExpansion
rem // Check whether current line starts with predefined beginning:
if "!_BEGIN!!LINE:*%_BEGIN%=!"=="!LINE!" (
rem // Line starts with predefined beginning, hence conditionally return line-break:
if defined FLAG echo/
rem // Output current line string:
echo(!LINE!
endlocal
rem // Set flag to insert line-breaks before further matches:
set "FLAG=#"
) else (
rem // Line does not start with predefined beginning, so just output current line string:
echo(!LINE!
endlocal
)
)
endlocal
exit /B
鉴于脚本存储为prettify-nmap-log-file.bat
使用以下命令行处理某个输入文件(如log.nmap
):
prettify-nmap-log-file.bat "log.nmap"
要将结果写入另一个文件(如 log.nmap.txt
),请使用 redirection:
prettify-nmap-log-file.bat "log.nmap" > "log.nmap.txt"
试一试 batch-file,仅修改文本文件的双引号路径后,在线 5
:
@(Set LF=^
% 0x0A %
)
@For /F %%# In ('Copy /Z "%~f0" NUL')Do @Set "CR=%%#"
@Call :Sub "%UserProfile%\Desktop\dummy.txt"
@GoTo :EOF
:Sub
@Set "#=#"
@(For /F Delims^=^ EOL^= %%# In ('^""%__AppDir__%cmd.exe"/V/U/D/C""%%__AppDir__%%findstr.exe"/I "^^^^Nmap ^^^^[0-9]*/.*open\^^^>" "%~1"|"%%__AppDir__%%findstr.exe"/IV "^^^^Nmap.*!CR!!LF!Nmap.*"|"%%__AppDir__%%findstr.exe"/IV "^^^^[0-9N].*!CR!!LF![^^^^\ ]""^"')Do @Echo("%%#"|"%__AppDir__%findstr.exe"/I "^.N">NUL&&(If Not Defined # (Echo(&Echo(%%#)Else Echo(%%#&Set "#=")||Echo(%%#)>"%~dpn1_filtered%~x1"
最后一行看起来非常长,因为我包含了文件的完整路径,这些文件通常可以使用 %PATH%
和 %PATHEXT%
变量访问。如果那些重要的变量发生了某些事情,这只是防止它失败。