chosey Posted March 8, 2011 Posted March 8, 2011 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 EnableDelayedExpansiondel alive.txtdel nosysaid.txtfor /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
jaclaz Posted March 8, 2011 Posted March 8, 2011 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 OFFSetLocal 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 20if !errorlevel! equ 0 echo %%q >> alive.txt pause ))and here another one:@ECHO OFFIF 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.txtjaclaz
chosey Posted March 8, 2011 Author Posted March 8, 2011 Hi Jaclaz,Thanks for the response.When I run your proposed script I see it ping the IPs in the blacklist.Nir
Yzöwl Posted March 8, 2011 Posted March 8, 2011 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 matchIf 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%.
chosey Posted March 8, 2011 Author Posted March 8, 2011 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
Yzöwl Posted March 8, 2011 Posted March 8, 2011 Would something like this do you?@ECHO OFFFOR %%# IN (ALIVE NOSYSAID) DO IF EXIST %%#.TXT DEL %%#.TXTFOR /F %%# IN ('FINDSTR/VG:"BLACKLIST.TXT" "WIN-SEGMENT.TXT" 2^>NUL') DO ( (>NUL PING %%# -n 1 -w 20)&&(>>alive.txt ECHO=%%#))
chosey Posted March 9, 2011 Author Posted March 9, 2011 Beautiful, that did the trick. Thank you!I'm still trying to follow the syntax but it's working perfectly.Thanks a lot.Nir
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now