1.

Solve : Ip address from text file?

Answer»

I am writing a batch file that will allow users to run REMOTE processes on other machines. I have that part down. I have now been charged with another duty. I am asking the user if they need to resolve a host name to ip address, I am unclear about how to do this. I'm thinking something like this

ping >file.txt

And here's where I'm stuck. I need a way for the batch file to parse the txt file and look for the word "Reply" (as in reply from) or "Request" (as in request timed out) and take the ip address and save it into a variable that I can call at a later time. I'm pretty sure that I am going to have to USE the FIND command but I have no clue on how to use it.You can use the pipe and dispense with the text file:

Code: [Select]for /f "tokens=1-3 delims=[] " %a in ('ping hostname -n 1 ^| find /i "pinging"') do set IP=%c

As usual there is more than one way to do this. Change hostname to something useful.

Hope this helps. 8-)Thanks for the help but I think that I need a little more. I am by no means an expert on the for command but I need the file to find weather or not the host is alive. I am assuming that the "pinging" section of your response only looks at the beginning of the ping output. I need the file to look to see if the host is alive by finding the "Reply from" part of the ping or if the host is turned off by finding the "Request timed out" part of the ping, then based on that I can tell the operator that the machine they want is either off or on and give them the IP address. Can the for command do that. Also if you don't mind, I would really like to know what the whole command you gave me actually means. Is there another part of the forum to go and look at how to use the FOR command as I am really starting to get into the batch file THING and I think that the FOR command could really help me out. In that case search for "Reply" and change the delimiters:

Code: [Select]@echo off
for /f "tokens=1-3 delims=: " %%a in ('ping hostname -n 1 ^| find /i "reply"') do set IP=%%c
if .%IP%==. echo Host is not reponding

Basically you're taking the output of the PING command and sending it thru the pipe | to the FIND command where the search for "REPLY" takes place. The FOR command acts as a parser. If you run PING from the command line, you'll notice the REPLY line consists of space delimited words which include the IP address. The IP also has a trailing semi-colon. By using the space and the : as delimiters, you can isolate the IP address. Also notice the IP is the third word on the line. We need three variables to get to this value and since we declared %%a in the FOR statement, the third variable is referenced as %%c.

Probably not the BEST explanation you'll ever get. Run FOR /? (this works for any command) from a command prompt for the official documentation. Not the best but reasonably better than most.

IMHO, with the batch file thing, you end up coding the data and not the method.

Good luck. 8-)You can use this in a batch file, and pass it the parameter of the host or IP address you are testing:
Code: [Select]@echo off
ping -n 1 %1|find /i "reply" >NUL&if errorlevel 1 (echo Host "%1" is dead, or ICMP is firewalled) else (echo Host "%1" is alive)So if you use this code in PingTest.bat, you could ust the command
PingTest 192.168.1.1
or
PingTest JSmith01
Thanks for the quick reply SideWinder. I am using what you gave me and it seems to be working, the only thing is that I cannot get the message that the "host is not responding".

Here's what I have so far

@echo off
set /p compname=Enter host name
for /f "tokens=1-3 delims=: " %%a in ('ping %compname% -n 1 ^| find /i "reply"') do set IP=%%c
if .%IP%==. echo Host is not reponding

If I put all T's in the compname variable (I don't have a computer with all t's) the batch doesn't show me "Host is not responding"

Something I am or am not doing right?I always forget the part to reset the variables:

Code: [Select]@echo off
set IP=
set /p compname=Enter host name
for /f "tokens=1-3 delims=: " %%a in ('ping %compname% -n 1 ^| find /i "reply"') do set IP=%%c
if .%IP%==. echo Host is not responding

Normally you would reset the variables at the bottom of the batch file.

Happy Computing. 8-)That fixed it. Thanks a ton. I'm gonna fool around with this and let you know how it turns out.

Also mybe while I got ya. Is there a way to tell the file to use one or the other variables if one of them has something in it and the other one doesn't . Lemme explain. The start of the file asks is you want to resolve a host name. You have fixed that part. But if the operator says no to the hostname thing and decides to put in the ip him/her self that is a whole nother variable. Basically what I am asking is if they say yes to resolving a host name then it sets one variable and leaves the other blank (if they had said no to resolving.) but if they say no to resolving then the other variable is filled while the compname variable stays empty. Is there a way to tell the batch file to only use the variable that has something stored in it and not to use the ones that nothing set to them.

I hope this makes sense to ya, I know it took me a while to figure out exactly what I wanted this thing to do.

Thanks again.If the user indicates no ping is needed, set the IP to whatever they input and BRANCH around the ping logic you developed in the previous post (you can use a goto/:label construct).

Ping works with both IP addresses and names. Personally I would not give the user that option. Do the ping regardless....don't rely on the user to get it right.

Just a thought. 8-)Thanks, After I posted that I started thinking it through and found a solution that will work with what I need.

Thanks a bunch for everything you really helped me out and saved me a ton of research time that I can use to learn VB and maybe write front end GUI's for this instead of DOS which I prefer but many don't.

Thanks again. I'll be back to pick your brain again no doubt.



Discussion

No Comment Found