如何在文本文件中打印结果

如何在文本文件中打印结果

你好,我有这个脚本来测试网络服务,该脚本可以扫描 IP 是否有任何端口打开,并进行成功和不成功的迭代尝试,最后我想将这些结果打印到一个文本文件中!这样我就可以检查进行了多少次成功的尝试。有人能帮我吗?

@echo off

color 2

set /p "$IP= Enter IP Address: "
set /p "$services= Enter Service Name(e.g TELNET, FTP, MSTSC, TFTP): "
set /p "$Port= Enter Port No: "
set /p "$N= Enter Number of Iterations to be performed: "

for /l %%a in (1,1,%$N%) do start %$services% %$IP% %$Port%
@echo %IP%:%servicen%:%port%.%N% >> filename.txt
pause

最后我尝试用 echo 打印结果但没有成功

答案1

$您在变量名称中缺少符号,并且services变量拼写错误:

@echo off

color 2

set /p "$IP= Enter IP Address: "
set /p "$services= Enter Service Name(e.g TELNET, FTP, MSTSC, TFTP): "
set /p "$Port= Enter Port No: "
set /p "$N= Enter Number of Iterations to be performed: "

for /l %%a in (1,1,%$N%) do start %$services% %$IP% %$Port%
@echo %$IP%:%$services%:%$Port%.%$N% >> filename.txt
pause

答案2

您的代码可以改进,这就是我所做的,现在它可以捕获输入,进行一些检查,并使其在变量中可用。

每个使用语言、脚本和类似的东西来写作/工作的人,在一段时间后都会形成一种“运作方式”或“布局样式”,我以“流畅/柔和”的方式使用我的方式写作,但我明白在某些时候可能会难以理解/编辑/适应。

因此,请尽可能多地使用评论,我会在可能的情况下尽快回复...

了解此代码尚未准备好,这里缺少变量将要使用的命令,之后,结果可以以某种方式处理以满足我其实想要成功和失败的尝试的结果

如果不向我们提供您的命令或每个命令的尝试,我们就无法帮助您,因为它们每个都有通过脚本在命令行上进行交互的特殊性/有些在/....

仅有的命令start !$IP! !$services! :!$Port!并不是能够有效启动/执行任何 IP /端口上的任何操作的命令,这些操作可用于任何类型的服务,并且还可以获得满足以下条件的输出/结果:

Successful attempt !$IP!:!$services!:!$Port!.!_Iteract!
Unsuccessful attempt !$IP!:!$services!:!$Port!.!_Iteract!

有些会在不同的编码和它们自己的环境中有输出,单独使用命令不足以执行并证明获得的结果是否成功......

我留下了一些空白:label,以便您在以后的尝试中可以将每个服务的命令添加到脚本中。

@echo off && cd /d "%~dp0"

setlocal EnableDelayedExpansion && color 02



:ip
set _i=<nul & cls & echo\ 
set /p "$IP= Enter IP Address: " || goto=:ip

for /f %%i in ('cmd /u/c echo\"!$IP!"^<nul^|find/v " "^|find "."')do set /a "_i+=1+0"
if !_i! neq 3 echo/The ip address entered is not valid.. && timeout -1 && goto=:ip

echo\.!$IP!|findstr /b \.[0-9]|findstr /v [a-Z] >nul || goto=:ip
for %%p in (!$ip:.=^,!)do for %%i in ("%%~p leq -1","%%~p gtr 255")do <con: ^
     call if %%~i echo/The ip address entered is not valid.. && timeout -1 && goto=:ip
      
set "_IP=Enter IP Address: !$ip!" 
  
  
  
:service
cls & echo\ && echo\!_IP!
set /p "$services= Enter Service Name (e.g TELNET, FTP, MSTSC, TFTP): " || goto=:service
      
for %%i in ("!$services!")do echo\%%~i|findstr /ilb "TELNET FTP MSTSC TFTP" >nul || (
     echo/The service name entered is not valid.. && timeout && goto=:service )
    
for %%s in (TELNET,FTP,MSTSC,TFTP)do if /i "!$services!" == "%%~s" <nul ^
     set "_services=Enter Service Name (e.g TELNET, FTP, MSTSC, TFTP): %%~s" 



:port
cls & echo\ && echo\!_IP! && echo\!_services!
set /p "$Port= Enter Port No: " || goto=:Port

echo\!$Port!|findstr /b [0-9]|findstr /v [a-Z] >nul || <nul ^
     echo/The port number entered is not valid.. && timeout -1 && goto=:port 

for %%I in ("!$Port! geq 00001","!$Port! leq 64738")do <con: ^
     call if %%~I echo/The port number entered is not valid.. && timeout -1 && goto=:port

set "_port=Enter Port No: !$Port!"



:Iteract
cls & echo\ & echo\!_IP! & echo\!_services! & echo\!_port!
set /p "$Iteract=Enter Number of Iterations to be performed: " || goto=:Iteract

echo\!$Iteract!|findstr /b [0-9]|findstr /v [a-Z] >nul || <nul ^
     echo/The Iterations entered is not valid.. && timeout -1 && goto=:Iteract

for %%I in ("!$Iteract! geq 1","!$Iteract! leq 10")do <con: ^
     call if %%~I echo/The Iterations entered is not valid.. && timeout -1 && goto=:Iteract
     
set "_Iteract=Enter Number of Iterations to be performed:  !$Iteract!"



:TELNET
rem :: here you will enter your commands for interacting with your variables for the
rem :: TELNET service and capturing the results

goto=:EOF



:FTP
rem :: here you will enter your commands for interacting with your variables for the 
rem :: FTP service and capturing the results

goto :EOF



:MSTSC
rem :: here you will enter your commands for interacting with your variables for the 
rem :: MSTSC service and capturing the results

goto=:EOF



:TFTP
rem :: here you will enter your commands for interacting with your variables for the 
rem :: TFTP service and capturing the results

goto=:EOF



:run <Just a suggestion for getting output on file>
cls & echo\ & echo\!_IP! & echo\!_services! & echo\!_port! & echo\!_Iteract! 

>.\filename.txt (for /l %%a in (1,1,!$Iteract!)do start "" /b !$services! !_IP! !$Port! >nul && ( 
         echo\Successful attempt !$IP!:!$services!:!$Port!.!_Iteract! !time: =0! !date! ) || ^ 
         echo\Unsuccessful attempt !$IP!:!$services!:!$Port!.%%a 
        ) 

相关内容