Reading out put of previous command in bat command file - batch-file

I am very new to batch programming, I am trying to write a batch file that is a fake virus. I need to obtain the IP address from the previous command IPCONFIG into the variable VarIP. Can you help me?
My code:
echo off
echo Trying to hack your computer
ipconfig
echo Now hacking your IP
ping -t VarIP
echo on
pause

It's pretty simple to extract part of the output from any console command by using find to eliminate the lines in the output that you do not want, then using the for command to extract a portion of the line found by find:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2 delims=:" %%i in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=%%i&set VarIP=!VarIP: =!)
ping -t !VarIP!
endlocal
Hopefully you are just creating a practical joke on a friend and aren't up to anything more nefarious.

Another Version without "Tokens" for NT :
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=!%%a%!)
ping -t %VarIP%

This is a useful method to get IP info:
#echo off
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get IPAddress /value | find "I" "') do echo IPv4 %%~a IPV6 %%~b
pause

Related

Single Line FOR /F command FINDSTR error handling unexpected results

Problem defined is to have a command line that takes the output of nslookup from a FOR /F loop and either sends to the IP Address to a text file or sends a error message.
The simplest iteration I have is:
FOR /F "skip=3 usebackq tokens=1*" %a IN (`^"nslookup ADROOT02.adroot.the-server-company.co.za 2^>^&1^"`) DO #echo %a | findstr /C:"Address" >nul 2>&1 && echo found || echo not found
However, this only works if an address is found, but no output is received if the findstr fails; i.e. no || echo not found is not activated for some reason.
I am looking for assistance is finding the issue/resolution to have a single command line executable that can define the IP addresses.
#ECHO OFF
SETLOCAL
FOR %%d IN (google.com ADROOT02.adroot.the-server-company.co.za ) DO (
NSLOOKUP %%d 2>&1|FIND "Non-existent domain" >nul&IF ERRORLEVEL 1 (ECHO %%d found) ELSE (ECHO %%d NOT found)
)
GOTO :EOF
Yields
google.com found
ADROOT02.adroot.the-server-company.co.za NOT found
for me.
With guidance from this forum I was able to put a command together that outputs the IP address from nslookup or error if no IP found:
FOR /F "usebackq tokens=1* delims=: " %a IN (`^"nslookup server-company.co.za 2^>^&1` ^| findstr ^/N ^/C^:^"Address^" 2^>^&1 ^"`) DO IF %a GTR 3 ( SET res2=%b & echo %res2:~10%) ELSE (echo ERROR)

Batch file to collect UUID and Serial number

I have been using this batch file to collect the Serial number and UUID number and output to a CSV and now it no longer works.
#echo off
set outputfile="Y:\HP\UUDI.csv"
for /f "delims== tokens=2" %%i in ('wmic csproduct Get "UUID" /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
echo UUID,Serial,>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,>>%outputfile%
If someone can look at this file and help me understand what went wrong I would appreciate it
I don't understand what did you mean by "No Longer Works" ? Please be more explicit when you ask a question !
here is a test and tell me if this works or not on your side and i will edit this aswer according to your response !
#echo off
set "outputfile=%~dp0UUDI.csv"
#for /f %%i in (
'wmic csproduct Get "UUID" /value ^& wmic bios get serialnumber /value'
) do (
#for /f %%j in ("%%i") do set "%%j" & echo "%%j"
)
echo UUID,SerialNumber>"%outputfile%"
echo %UUID%,%SERIALNumber%>>"%outputfile%"
If exist "%outputfile%" Start "" "%outputfile%" & Exit
The only reason I can see for your provided code to change its behavior, is that which was commented already by Mofi. That is, you've somehow caused the location of WMIC.exe to have been removed from the %Path% environment.
I have decided to provide an alternative method of achieving your goal using your chosen command utility WMIC.exe, and using its full path, to prevent such a reliance in future.
The WMIC command is traditionally one of the slower ones, so this method invokes it only once. All you should need to do is Echo your commands, currently on lines 12and 14, each separated as in line 13. If any of your commands requires to Get more than one property, you should separate those with caret escaped commas, e.g. Get Property1^,Property2. The results, (subject to line/environment length limitations), will then be saved to variables, %Title%, and %Record%, which can later be output to a file outside of the loop. Note: all commands should use /Value, or the more correct, /Format:List.
Example, (don't forget to adjust your output file path on line 4 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "outputfile=Y:\HP\UUDI.csv"
Set "WMIC=%SystemRoot%\System32\wbem\WMIC.exe"
Set "FIND=%SystemRoot%\System32\find.exe"
Set "Title="
Set "Record="
For /F "Tokens=1,* Delims==" %%G In ('
(
Echo CSProduct Get UUID /Value
^&
Echo BIOS Get SerialNumber /Value
^)
^| %WMIC% ^| %FIND% "="
') Do (If Not Defined Title (Set "Title=%%G") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Title!") Do (EndLocal
Set "Title=%%I,%%G"))
If Not Defined Record (Set "Record=%%H") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Record!") Do (EndLocal
Set "Record=%%I,%%H")))
If Defined Title ( Echo %Title%
Echo %Record%) 1>"%outputfile%"

How to auto detect interface that connected to the internet

So i have remote machine with simple .bat file:
C:\Program Files\Wireshark\tshark.exe -i 1 -w c:\file.pcap
This command start Tshark process and start sniffing
Now my problem is that i start this .bat file in several machines and in each machine the correct interface is different so with the commend Tshaek -D i can get all the interfaces with the index numbers but from here i need to know which interface is the one the connected to the internet.
And of course i need to do that inside my.bat file and this need to be my interface number (in my example the interface number is hard code and equal to 1)
Any suggestions ?
This is very hacky, but should do.
#echo off
for /f "tokens=2 delims=:" %%i in ('ipconfig ^|findstr "IPv4"') do (
ping -n 1 -S%%i google.com |findstr /i reply
if not errorlevel 1 for /f %%a in ('ipconfig ^| findstr /I /N "%%i"') do set cnt=%%a
)
set /a cnt-=5
for /f "tokens=1* delims=:" %%i in ('ipconfig ^|more +%cnt%') do set adap=%%i & goto runts
:runts
set adap=%adap:*adapter =%
for /f "usebackq delims=." %%i in (`""C:\Program Files\Wireshark\tshark.exe" -D | findstr /I "%adap%""`) do (
"C:\Program Files\Wireshark\tshark.exe" -i %%i -w c:\file.pcap
)
What we do is, run ipconfig and get all IPv4 addresses and use each as source to ping google.com. Using findstr we then match reply and then errorlevel 0 will determine whether the adapter was the active one.
Now we simply go up 5 lines to get the description of the adapter, then we findstr that adapter using tshark -D and simply run the full tshark command.
Note, this will be completely different if you use IPv6.

Extract IP and DHCP and set to variable from ipconfig /all. Batch

I am trying to extract the DHCP status and IP address from ipconfig /all and set it to a variable.
Can this be done?
It's easier using Netsh try this:
#echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=* delims=" %%a in (
'netsh interface ipv4 show config name^="local area connection"'
) do (
set /a cnt+=1
if !cnt! equ 3 (
goto :break
) ELSE (echo(%%a
)
)
:break
Change local area connecton to suit your environment.
Well - I'm doing this with grep.
I don't know exactly what you need, but for example if you need the DHCP-server:
for /F %%i in ('ipconfig -all ^| grep "DHCP Server" ^| grep -Eo '[0-9][0-9.]+'') do set DHCPServer=%%i
and for the local IP:
for /F %%j in ('ipconfig ^| grep "IPv4" ^| grep -Eo '[0-9][0-9.]+'') do set IpAdress=%%j
Both commands are working inside a batch file.

Batch Script to run a computer test

I am not really a programmer. I am a newbie.
Can you guys please help me with a windows batch file that will run a computer test?
Detect any hard drives then run a chkdisk on them
Detect any usb port
Detect any flash drive plug in the pc
Check the display pixels
I have started with this to detect the USB but somehow getting some errors
#echo off
setlocal EnableDelayedExpansion
set PNPDeviceID=4002FDCCE0E4D094
set Q='wmic diskdrive where "interfacetype='USB' and PNPDeviceID like '%%%PNPDeviceID%%%'" assoc /assocclass:Win32_DiskDriveToDiskPartition'
echo %Q%
for /f "tokens=2,3,4,5 delims=,= " %%a in (%Q%) do (
set hd=%%a %%b, %%c %%d
call :_LIST_LETTER !hd!)
goto :_END
:_LIST_LETTER
(echo %1 |find "Disk ") >nul|| goto :_EOF
for /f "tokens=3 delims==" %%a in ('WMIC Path Win32_LogicalDiskToPartition ^|find %1') do set TMP_letter=%%a
set Part_letter=%TMP_letter:~1,2%
echo %Part_letter% %1
goto :_EOF
:_END
:_EOF
:: *** end
pause
Here is a batch script that does almost everything you need.
Checkdisk will run against any local fixed drives. You can have it run in a new window (chkdskAsync=true) or have it run inside the program (chkdskAsync=false). Additionally, specify the chkdsk flags using the chkdskLaunchArguments= variable.
#ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION
:: Use these lines to specify how CHKDSK and Notepad will run
:: ===============================================
SET chkdskLaunchArguments=/F /R
SET chkdskAsync=true
SET launchNotepad=true
:: ===============================================
COPY /Y NUL C:\Users\public\data.txt >nul 2>&1
ECHO Getting drive(s) information
FOR /F "tokens=*" %%A IN ('wmic logicaldisk get caption^, description^, providername^, volumename') DO (
ECHO.%%A>>C:\Users\public\data.txt
)
FOR /F "tokens=1,2 skip=1" %%A IN ('wmic logicaldisk get caption^, drivetype') DO (
IF %%B EQU 3 (
IF "!chkdskAsync!" EQU "true" (
START CMD /C CHKDSK %%A %chkdskLaunchArguments%
) ELSE (
CHKDSK %%A %chkdskLaunchArguments%
)
)
)
ECHO Getting resolution information
FOR /F "tokens=*" %%A IN ('wmic path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution') DO (
ECHO.%%A>>C:\Users\public\data.txt
)
ECHO Information gathered ... saved to C:\Users\public\data.txt
ECHO Showing results.
IF "!launchNotepad!" EQU "true" (
START /WAIT notepad.exe /A C:\Users\public\data.txt
)
ENDLOCAL
EXIT
When it is complete it saves everything to C:\Users\Public\data.txt. It will also launch the file in notepad (if launchNotepad=true is set to ... true).
The output looks like so:
Caption Description ProviderName VolumeName
A: Network Connection \\somefileserver.domain\ New Volume
C: Local Fixed Disk
E: Removable Disk someuser Drive
H: Network Connection \\somefileserver.domain\users$\someuser Data Share and User Home Dirs
P: Network Connection \\somefileserver.domain\Data Data Share and User Home Dirs
S: Network Connection \\somefileserver.domain\share
CurrentHorizontalResolution CurrentVerticalResolution
1920 1080
NOTE: Make sure to run this as an administrator. If you want chkdsk to run
Unfortunately, there isn't a way to grab all USB devices other than storage. Hope this helps!
I didn't realize this question is 4 years old. Going to leave my answer for posterity and anyone digging for answers.
I would recommend trying
chkdsk volume=C:\ D:\ E:\ F:\
to check the disks each as a separate volume

Resources