首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows批处理文件以插入空行/删除单行- nmap输出

Windows批处理文件以插入空行/删除单行- nmap输出
EN

Stack Overflow用户
提问于 2019-12-06 16:37:11
回答 2查看 368关注 0票数 0

我试图清理nmap结果,并获得一个更干净/更容易处理的文件,这正是我所需要的数据-基本上,IP和开放端口。

(不需要将它集成到解决方案中,我可以让他们一起工作)

这一行清除了初始输出:

代码语言:javascript
复制
findstr "Nmap open"|findstr /v "no-response"

它使用txt和打开的端口保持行。然后,它移除包含“无响应”(其中也包含"Nmap“)的行。

这是结果输出:

代码语言:javascript
复制
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     

我希望该输出包括空行,并省略没有后续开放端口的行,即:

代码语言:javascript
复制
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  

不确定这是否可行--看起来应该是.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-09 15:36:32

在只修改文本文件的双引号路径后,在批文件上尝试使用5

代码语言:javascript
复制
@(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%变量使用。如果这些重要的变量发生了一些事情,这只会防止它失败。

票数 0
EN

Stack Overflow用户

发布于 2019-12-09 09:37:43

虽然这个问题过于宽泛,因为它没有试图解决这个问题,但我决定提供一个使用功能的脚本,即搜索行外的内容(请参阅代码中的所有rem注释):

代码语言:javascript
复制
@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):

代码语言:javascript
复制
prettify-nmap-log-file.bat "log.nmap"

若要将结果写入另一个文件(liek log.nmap.txt),请使用重定向

代码语言:javascript
复制
prettify-nmap-log-file.bat "log.nmap" > "log.nmap.txt"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59217035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档