0

So this is my first post and i have spent a little over two hours just trying to figure out how to do this.

So basically i wrote a Port Scanning tool via Batch and using a program called tcping, and i have a variable called %scan% and %openport% which is the current port that is open. I want to write this port into a text file and then write the new port when the it changes, But i can only write the new port since i can't write the variable twice in two different ways because it will just update %openport% with the new port.

Here's my code

@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

Sorry if the question is vague i haven't ever posted on here before

  • If you `set openport=%scan%` and the immediately `goto :writedata` and compare them, they'll always match. Think about it. `i = 1`, followed by `if i == 1` will always be true. Your `if` in `:writedata` is redundant. Both branches `cls`, so just `cls` once at the beginning. Then you can use a single `if %openport% == %scan% goto :loopdata`. – Ken White Oct 27 '18 at 00:24
  • But how will the Variable update in the .txt file so that i can see all opened ports? – Clorox Bleach Oct 27 '18 at 01:22
  • You still update it; just not where you are now Think about what I wrote, and then see if you can figure out where that needs to be done. It doesn't do much good for you to copy/paste code to use if you don't understand it, because (like right now) you can't change it when you need to do so. – Ken White Oct 27 '18 at 01:26
  • I wrote most of this code myself i don't just "copy and paste" code – Clorox Bleach Oct 27 '18 at 01:35
  • 1
    Your code has comments `REM The code up there is property of SO` and `REM All code below is coded by Paradex`, neither of which is you. How did you write most of the code yourself when all the code was written by other people according to your own post? – Ken White Oct 27 '18 at 01:38
  • I go by Paradex on instagram and i gave credit where it was due to the ONLY code i copied from stack overflow. – Clorox Bleach Oct 27 '18 at 01:45

1 Answers1

0

I can't understand what you need, but my best shoot is to suggest to you to use temp files to keep a record of the variables set, and then call it back with for.

Something like this:

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