Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Collect all IPs For online workstations only on my domain  (Read 7589 times)

0 Members and 1 Guest are viewing this topic.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Collect all IPs For online workstations only on my domain
« on: February 23, 2021, 08:51:31 PM »
Hi All,

I want to collect the IPs for  online workstations on my domain and i have two scripts the first script to only display which is the online  and the other script to collect the IPs for any host name inside the input file named (hosts.txt).
and i want to merge this two scripts to do what i want which is to ping any host name taken for the input file then try to ping on it and if it's online try to bring it's IP then insert the result into output file but i couldn't

Can Any professional here to assist
here is the mentioned two scripts.

First one to ping all host names and separate them inside two output files.

Code: [Select]
@echo off
for /f %%i in (hosts.txt) do (
   SET bHOSTUP=0
   ping -n 1 %%i -w 2000 |find "TTL=" > NUL && SET bHOSTUP=1
   IF !bHOSTUP! equ 1 (
      set g=%%i
      CALL :HOSTUP %%i
   ) else (
      set g=%%i
      CALL :HOSTDOWN %%i
   )
)

GOTO EOF


:HOSTUP
Echo %g% : Is Pingable
Echo %g% : Is Pingable>>Online_workstations.txt
GOTO EOF


:HOSTDOWN
Echo %g% : is Offline or Not Exist
Echo %g% : is Offline or Not Exist>>Offline_workstations.txt
GOTO EOF


:EOF
exit /B


And Here is the second script which used to collect the IPs for all host names located in the input file also
Code: [Select]

@echo off
for /F "tokens=* delims=" %%C in (hosts.txt) do (
for /f "tokens=1,2 delims=[]" %%A in ('ping %%C ^| find "Pinging"') do set ipaddress=%%B && echo %%C : %%B
)


I want to merge these two scripts into one batch (one step)( separate online workstations and offline workstations and if found that the workstation is online try to get its IP and insert this information inside the output file.

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Collect all IPs For online workstations only on my domain
« Reply #1 on: February 24, 2021, 01:43:48 AM »
Hi  ;)
Just give a try for the combined script and tell me the results on your side !

Scan_Hosts_IP.bat
Code: [Select]
@echo off
Title Collect all IPs For online workstations only on my domain
Set "HOSTS_File=%~dp0hosts.txt"

IF NOT EXIST "%HOSTS_File%" (
Color FC
ECHO(
Echo( Please Check this "%HOSTS_File%" location with this script "%~nx0"
TimeOut /T 8 /NoBreak>nul
EXIT
)

Set "LogFile_OnLine=%~dp0_Online_workstations.txt"
If Exist "%LogFile_OnLine%" Del "%LogFile_OnLine%"
Set "LogFile_OffLine=%~dp0_Offline_workstations.txt"
If Exist "%LogFile_OffLine%" Del "%LogFile_OffLine%"

SetLocal EnableDelayedExpansion
@for /f %%i in ('Type "%HOSTS_File%"') do (
SET bHOSTUP=0
Ping -n 1 %%i -w 2000 |find "TTL=">NUL && SET bHOSTUP=1
IF [!bHOSTUP!] equ [1] (
set "HostName=%%i"
Call :GET_IP %%i
CALL :HOSTUP %%i
) else (
set "HostName=%%i"
CALL :HOSTDOWN %%i
)
)

Start "Log" /MAX "%LogFile_OnLine%"
GOTO EOF
::----------------------------------------------------------------------
:HOSTUP
IF defined IP (
Echo !HostName! : Is Pingable - IP = !IP!
Echo !HostName! : Is Pingable - IP = !IP!>>"%LogFile_OnLine%"
) Else (
Echo !HostName! : Is Pingable
Echo !HostName! : Is Pingable>>"%LogFile_OnLine%"
)
GOTO EOF
::----------------------------------------------------------------------
:HOSTDOWN
Echo !HostName! : is Offline or Not Exist
Echo !HostName! : is Offline or Not Exist>>"%LogFile_OffLine%"
GOTO EOF
::----------------------------------------------------------------------
:GET_IP <HOST> <IP>
Set "IP="
@for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %1') do Set "IP=%%b"
Exit /B
::----------------------------------------------------------------------
:EOF
exit /B
::----------------------------------------------------------------------
« Last Edit: February 24, 2021, 02:04:05 AM by Hackoo »

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Collect all IPs For online workstations only on my domain
« Reply #2 on: March 02, 2021, 05:53:28 AM »
Hi  ;)
For who is still interested for any improved of this Batch script : You can found it here for any update : Scanner_IP_MAC_Colors.bat
Have a nice day  ;) :D

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Collect all IPs For online workstations only on my domain
« Reply #3 on: March 06, 2021, 02:56:32 AM »
Thanks, Hackoo for appreciate helping