?
Solved

Grabbing the ip address instead of computer name

Posted on 2009-06-30
9
Medium Priority
?
207 Views
Last Modified: 2012-05-07
Hi guys,

I have the following batch file called check_services.bat

Syntax:

check_services.bat <computername>

Currently, what happens is that the computer name is used for the sc command part, for example:

sc \\%1 query McAfeeFramework

However, what I want to do is the following:

check_services.bat <computername>

then find out the ip address of the computername entered, and use the ip address for

sc \\%1 query McAfeeFramework
sc \\%1 query EnterceptAgent
sc \\%1 query AeXNSClient
sc \\%1 query McShield

instead of %1 (which is the computer name)

Any help greatly appreciated.

check_services.bat <computername>

@echo off
@echo Checking the following 4 services on a machine %1 .....
 
rem check_services.bat <computername>
ping -n 1 %1 >nul 2>&1
if %errorlevel%==0 (
   echo %1: Up
) else (
   echo %1: Down
)
 
sc \\%1 query McAfeeFramework
sc \\%1 query EnterceptAgent
sc \\%1 query AeXNSClient
sc \\%1 query McShield

Open in new window

0
Comment
Question by:Simon336697
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 24752340

@echo off
echo Checking the following 4 services on a machine %1 .....
 
rem check_services.bat <computername>
for /f "tokens=1-6 delims=[.] " %%a in ('ping %1 -n 1 ^| find "["') do set ip=%%c.%%d.%%e.%%f
ping -n 1 %1 >nul 2>&1
if %errorlevel%==0 (
   echo %1 [%ip%]: Up
) else (
   echo %1 [%ip%]: Down
)
sc \\%ip% query McAfeeFramework
sc \\%ip% query EnterceptAgent
sc \\%ip% query AeXNSClient
sc \\%ip% query McShield

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24754045
HI x66_x72_x65_x65,
Mate that WORKS brilliantly.
I dont know if i can ask this of you, but:

Currently, this works against a given computer name, for example:

script.bat <computername>

Is it possible somehow to modify this, so that:

1) You could run the script against a list of computers in a text file?
eg.
script.bat c:\computers.txt
0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 24756211
The following is what you're looking for...

This script also writes a log to availability.txt, showing hostname, ip address, and up/down state.


@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
setlocal enabledelayedexpansion
for /f %%c in (%1) do (
  call :GETINFO %%c
  echo %%c [!ip!]: !Availability!>>availability.txt
)
goto :eof
 
:GETINFO
if [%1]==[] goto :eof
Set Availability=
for /f "tokens=2 delims=[]" %%a in ('ping %1 -n 1 ^| find "["') do set ip=%%a
ping %ip% -n 2 -w 100 >nul 2>&1  
If %errorlevel%==1 goto DOWN
 
echo %1 [%ip%]: Up
Set Availability=Up
sc \\%ip% query McAfeeFramework
sc \\%ip% query EnterceptAgent
sc \\%ip% query AeXNSClient
sc \\%ip% query McShield
goto :eof
 
:DOWN
echo %1 [%ip%]: Down
Set Availability=Down

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:Simon336697
ID: 24758153
Hi x66_x72_x65_x65,
Champion.
x66_x72_x65_x65,
The output of running your latest great script is:

script.bat computers.txt
where computers.txt (serverA is pingable, serverB is not)
serverA
serverB
serverA

Output is:

script.bat computers.txt

serverA [10.1.2.3]: Up
serverB [10.1.2.3]: Up
serverA [10.1.2.3]: Up

and

availability.txt

serverA [10.1.2.3]: Up
serverB [10.1.2.3]: Up
serverA [10.1.2.3]: Up

So,

Somehow, it is returning the same ip address for every computer listed in computers.txt, and reporting the Availability as Up, even though it is Down.
0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 24758389
Forgot to clear the variable... try this.

@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
setlocal enabledelayedexpansion
for /f %%c in (%1) do (
  call :GETINFO %%c
  echo %%c [%ip%]: !Availability!>>availability.txt
)
goto :eof
 
:GETINFO
if [%1]==[] goto :eof
Set Availability=
for /f "tokens=2 delims=[]" %%a in ('ping %1 -n 1 -w 1000 ^| find "["') do (
set ip=%%a
set p=Checking the following 4 services on a machine %1 [%%a] .....
)
ping %ip% -n 2 -w 100 >nul 2>&1  
If %errorlevel%==1 goto DOWN
 
echo %1 [%ip%]: Up
Set Availability=Up
title %p% & echo %p%
sc \\%ip% query McAfeeFramework
sc \\%ip% query EnterceptAgent
sc \\%ip% query AeXNSClient
sc \\%ip% query McShield
set ip= & set p=
goto :eof
 
:DOWN
echo %1 [%ip%]: Down
Set Availability=Down
set ip= & set p=

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24758454
Hi mate,
Getting closer :>)

script.bat computers.txt

serverA [10.1.2.3]: Up
serverB [10.83.45.2 ]: Down
serverA [10.1.2.3]: Up

and

availability.txt

serverA [10.1.2.3]: Up
serverB  [10.1.2.3]: Down
serverA [10.1.2.3]: Up

In the command window, the offline machine is reporting correctly, but in availability.txt, it is taking on the value of serverA for its ip address

0
 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 2000 total points
ID: 24759245
Okay, this should do it...


@echo off
if [%1]==[] (
echo useage: script [computers.txt]
echo         computers.txt contains hostname or ip address, one per line.
goto :eof
)
setlocal enabledelayedexpansion
for /f %%c in (%1) do (
  call :GETINFO %%c
  echo %%c [!ip!]: !Availability!>>availability.txt
  set ip= & set p=
)
goto :eof
 
:GETINFO
if [%1]==[] goto :eof
Set Availability=
for /f "tokens=2 delims=[]" %%a in ('ping %1 -n 1 -w 1000 ^| find "["') do (
set ip=%%a
set p=Checking the following 4 services on a machine %1 [%%a] .....
)
ping %ip% -n 2 -w 100 >nul 2>&1  
If %errorlevel%==1 goto DOWN
 
echo %1 [%ip%]: Up
Set Availability=Up
title %p% & echo %p%
sc \\%ip% query McAfeeFramework
sc \\%ip% query EnterceptAgent
sc \\%ip% query AeXNSClient
sc \\%ip% query McShield
goto :eof
 
:DOWN
echo %1 [%ip%]: Down
Set Availability=Down

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24783867
Thanks so much x86 and sorry about the delay.
0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31600093
Brilliant x66!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question