Read from a list of hosts instead of querying whole domain

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Read from a list of hosts instead of querying whole domain

#1 Post by bradit » 30 Mar 2015 02:42

Hi Guys,

I've come across this batch script which essentially fires up psloggedon (by Symantec) for each IP on the domain and then neatly writes the output to a text file. I was wondering if there is a way to get it to read a lists of hosts from a text file instead so that I can query a set list of computers without having to do the whole domain.

From my understanding, currently it gets a list of everything on the domain using net view and then fires up PsLoggedOn for each and writes the results to an output file.

Here's the batch as it stands:

Code: Select all

@echo off
echo %date% >> C:\Output.txt
echo %time% >> C:\Output.txt
setlocal EnableDelayedExpansion
for /f "Tokens=1" %%c in ('net view /domain:"%USERDOMAIN%"^|Findstr /L /C:"\\"') do (
set isloggedon=0
for /f "Tokens=*" %%u in ('PsLoggedOn -L %%c^|find /i "%USERDOMAIN%\"') do (
set isloggedon=1
call :report %%c "%%u"
)
if !isloggedon!==0 call :report %%c nobody
)
endlocal
goto :EOF
:report
set work=%1
set comp=%work:~2%
set user=%2
set user=%user:"=%
call set user=%%user:*%USERDOMAIN%\=%%
@echo %comp% %user%
Echo %comp% , %user% >> C:\Output.txt


Any help would be much apprechiated, thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read from a list of hosts instead of querying whole doma

#2 Post by foxidrive » 30 Mar 2015 04:16

Replace this line with the one below it - hostnames one per line in the file.

Code: Select all

for /f "Tokens=1" %%c in ('net view /domain:"%USERDOMAIN%"^|Findstr /L /C:"\\"') do (


Code: Select all

for /f "usebackq delims=" %%c in ("c:\folder\hostlist.txt") do (

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#3 Post by bradit » 30 Mar 2015 04:26

Hi, thanks for the reply Foxidrive.

I've tried the change you suggested but it's just causing PsLoggedOn to hang with no output.

In the script I notice that it also tells PsLoggedOn to use %USERDOMAIN%, does this line also need to be changed to get it to read hosts from the text file?

Thanks,

Brad

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read from a list of hosts instead of querying whole doma

#4 Post by foxidrive » 30 Mar 2015 06:22

Try this command with one of your hostnames and see if there is an error:

Code: Select all

PsLoggedOn -L hostname


%USERDOMAIN% is not set in your code and must be a system variable.

Also use this command at a cmd prompt to see what the output is as your hostnames may not be in the same format.

Code: Select all

net view /domain:"%USERDOMAIN%"|Findstr /L /C:"\\"

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#5 Post by bradit » 30 Mar 2015 06:37

so,

Code: Select all

PsLoggedOn -L hostname
tells PsLoggedOn to search all machines on the domain for the hostname (treating it as a username).

The -L just tells it to show the local logon.

It seems to be the \\ which defines the host as a host.

here's the help file for PsLoggedOn:

Usage: psloggedon [- ] [-l] [-x] [\\computername | username]

- Displays the supported options and the units of measurement used for output values.

-l Shows only local logons instead of both local and network resource logons.

-x Don't show logon times.

\\computername Specifies the name of the computer for which to list logon information.

username If you specify a user name PsLoggedOn searches the network for computers to which that user is logged on. This is useful if you want to ensure that a particular user is not logged on when you are about to change their user profile configuration.


For the second part, net view gives a list of hostnames preceeded by \\

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#6 Post by bradit » 30 Mar 2015 06:44

I've added // to the start of all of my hosts in hostlist.txt but now the PsLoggedOn is just giving a 'nobody' for each machine, but it is reading the hostlist. (if it can't find anyone logged on localy it will throw a 'nobody')

Trouble is, one of the machines is the one I'm using so it should be giving results.

Any ideas?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Read from a list of hosts instead of querying whole doma

#7 Post by Squashman » 30 Mar 2015 06:45

bradit wrote: psloggedon (by Symantec)

Since when does Symantec own the the rights to that program.

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#8 Post by bradit » 30 Mar 2015 06:56

Apologies, "Sysinternals".

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read from a list of hosts instead of querying whole doma

#9 Post by foxidrive » 30 Mar 2015 06:57

bradit wrote:I've added // to the start of all of my hosts in hostlist.txt


// or \\ ?

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#10 Post by bradit » 30 Mar 2015 07:24

\\ sorry, busy day

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read from a list of hosts instead of querying whole doma

#11 Post by foxidrive » 30 Mar 2015 07:31

bradit wrote:For the second part, net view gives a list of hostnames preceeded by \\


With the /domain switch included?
The first token of the output of that is what the first loop is passing to the next part.

I don't have the network to test with so can't give advice without that info.

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#12 Post by bradit » 30 Mar 2015 07:51

no jsut simply \\hostname

i.e

\\hostname1
\\hostname2
\\hostname3
\\hostname4
\\hostname5

bradit
Posts: 8
Joined: 30 Mar 2015 02:31

Re: Read from a list of hosts instead of querying whole doma

#13 Post by bradit » 30 Mar 2015 07:57

The PsLoggedOn query that needs to be ran for each hostname in the hostlist is:

psloggedon -l \\hostname

Where hostname is the host from the input file (the input file currently contains hosts including the \\ )

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Read from a list of hosts instead of querying whole doma

#14 Post by Squashman » 30 Mar 2015 10:06

Code: Select all

FOR /F "delims=" %%G in (hostnames.txt) DO psloggedon -l %%G

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read from a list of hosts instead of querying whole doma

#15 Post by foxidrive » 30 Mar 2015 12:12

It works fine here to give the value in the %%c variable Squashman. I don't know what the OP is doing.

d:\abc>type c:\folder\hostlist.txt
\\arnieschwarzenegger
\\bobdylan

d:\abc>type a.bat
@echo off
for /f "usebackq delims=" %%c in ("c:\folder\hostlist.txt") do (echo "%%c")
pause

d:\abc>a.bat
"\\arnieschwarzenegger"
"\\bobdylan"
Press any key to continue . . .

Post Reply