Jump to content

Recommended Posts

Posted

Hi all,

I'm trying to write down a script to check if hosts are a alive in a subnet.

The script takes the IP list to ping from win-segment.txt and also there's this file blacklist.txt for exclusions.

I guess I'm missing something here because since I added the "blacklist" feature, the script does not run and I can't understand where it fails.

This is the script:

SetLocal EnableDelayedExpansion

del alive.txt
del nosysaid.txt

for /f %%q in (win-segment.txt) do (
set found = 0
for /f %%s in (blacklist.txt) do (
if %%q == %%s (set found = 1)
if %found% = 1 (
ping %%q -n 1 -w 20
if !errorlevel! == 0 echo %%q >> alive.txt)
pause
)
)

I'd really appreciate if someone could tell me what I did wrong.

Thanks,

Nir


Posted

if %found% = 1 (

if %found% == 1 (

and you have some "queer" spaces, that may prevent the actual working.

but there should be simpler ways, here is one:

@ECHO OFF
SetLocal EnableDelayedExpansion
IF EXIST alive.txt del alive.txt
IF EXIST nosysaid.txt del nosysaid.txt

FOR /f %%q in (win-segment.txt) DO (
FOR /f %%r in ('TYPE blacklist.txt^|FIND "%%q"') DO (
ping %%q -n 1 -w 20
if !errorlevel! equ 0 echo %%q >> alive.txt
pause
)
)

and here another one:

@ECHO OFF
IF EXIST alive.txt del alive.txt
IF EXIST nosysaid.txt del nosysaid.txt

FOR /f %%q in (win-segment.txt) DO FIND "%%q" blacklist.txt 1>nul&&ping %%q -n 1 -w 20&&ECHO %%q>> alive.txt

jaclaz

Posted

Hi Jaclaz,

Thanks for the response.

When I run your proposed script I see it ping the IPs in the blacklist.

Nir

Posted

You need to better explain what you are trying to do!

If I look at your example script this is what appears to be performed:

Go through each line of win-segment.txt one at a time,
(your list of IPs)
.

For the first string of that line search blacklist.txt for a match

If a match is found ping that IP and upon success write that address to alive.txt.

Are you simply wanting a listing of live blacklisted IPs? or was your intention to get a listing of all non-blacklisted live IPs?

BTW, your biggest problem in your example was that the found variable was being changed multiple times throughout the loops, as you had already enabled delayed expansion you'd certainly have been better advised to use !found! instead of %found%.

Posted

Hi Yzöwl,

Sorry if I wasn't clear about this, I'm pretty new to this stuff.

the blacklist.txt should contain IPs which will be excluded from the ping.

for each line of the win-segment.txt file the script should verify if the IP is in the blacklist.txt file.

if the IP does not exist it should ping it and write it in the alive.txt file.

If the IP exists it should be skipped.

Thanks,

Nir

Posted

Would something like this do you?

@ECHO OFF
FOR %%# IN (ALIVE NOSYSAID) DO IF EXIST %%#.TXT DEL %%#.TXT
FOR /F %%# IN ('FINDSTR/VG:"BLACKLIST.TXT" "WIN-SEGMENT.TXT" 2^>NUL') DO (
(>NUL PING %%# -n 1 -w 20)&&(>>alive.txt ECHO=%%#))

Posted

Beautiful, that did the trick. Thank you!

I'm still trying to follow the syntax but it's working perfectly.

Thanks a lot.

Nir

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...