如何从批处理文件写入输出但变量不断变化

时间:2018-10-26 23:43:38

标签: batch-file

这是我的第一篇文章,我花了两个多小时才试图弄清楚该怎么做。

因此,基本上,我通过Batch并使用名为tcping的程序编写了端口扫描工具,并且我有一个名为%scan%和%openport%的变量,这是当前打开的端口。我想将此端口写入文本文件,然后在更改时写入新端口,但是我只能写入新端口,因为我不能以两种不同方式两次写入变量,因为它只会更新%openport%使用新端口。

这是我的代码

@echo off
:: BatchGotAdmin
::-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" 
"%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> 
"%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::--------------------------------------




REM The code Up there is Property of https://stackoverflow.com/questions/1894967/how-to-request-administrator- access-inside-a-batch-file
REM All code below is coded by Paradex
:PortScan
@echo off
set /p Ip=Enter a IP or Website to ping:
cls
goto :LoopScan

:LoopScan
set scan=0
set /a scan=%scan%+1 
if "%scan%"=="2" goto :LoopScan1
goto :Checkport

:LoopScan1
set /a scan=%scan%+79
goto :Checkport

:Checkport
tcping -n 1 -a -s 1 -w 1000 %ip% %scan%
tcping -n 1 -a -s 1 -w 1000 %ip% %scan% | findstr "Open"

if %errorlevel% == 0 (
cls
color 0a
echo Port is Open.
set openport=%scan%
goto :writedata
) else (
cls
color 4
echo Port is Closed.
)
endlocal
goto :LoopScan1



goto :end


:writedata
if %openport% == %scan% (
cls
goto :loopscan1
) else (
cls

)







echo Open Ports For %ip% > logfile.txt
echo ------------------- >> logfile.txt
echo %openport1% >> logfile.txt


goto :loopscan1




:end

对不起,如果这个问题含糊不清,我以前从未在这里发布过

1 个答案:

答案 0 :(得分:0)

我无法理解您的需求,但是最好的建议是建议您使用临时文件来记录变量集,然后使用 for 进行调用。 / p>

类似这样的东西:

rem Using only one > will overwrite the file.
echo %openport% > log.tmp

rem Now, let's call it back.
for /f "tokens=*" %%c in ('type "log.tmp"') do set openport=%%~c