1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batching: Get a hostname and IP from a list of file names

Discussion in 'DOS/Other' started by autoamerican, Jul 4, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. autoamerican

    autoamerican Thread Starter

    Joined:
    Jul 4, 2012
    Messages:
    1
    I'm working on a script right now and am stuck. What I've got is a script that reads a list of filenames in a directory and converts those filenames to hostnames (each file is named HOSTNAME.log) and dumps these to a log, then runs another command on that log to get the IP addresses for the host names and puts this in a second log, and then finally goes through that second log to strip out addresses in a certain subnet and give me the final output in a third log.

    So far, this part all works - I get a clean list of IPs. What I would LIKE it to do is give me a final list of not just the IP addresses, but hostnames as well (preferably something formatted like 255.255.255.1 HOSTNAME.domain.net), and I'd like it to create only one single file, not two interim files and a final clean output.

    I've been modifying my script to try and add the hostname part, but so far haven't had any luck. What am I missing? This is the error I keep getting:

    L:\>if exist c:\ipaddresses.log del c:\ipaddresses.log
    i` was unexpected at this time.

    L:\> for /f "skip=3 delims=: tokens=2 usebackq" i`) do @echo j >> c:\ipaddres
    ses.log

    Code:

    REM @ECHO OFF
    REM ^^ watching the code

    REM ------------------------------------------------------
    REM Read the file names in the log directory on the server,
    REM convert those file names to a list of hostnames.
    REM ------------------------------------------------------

    cd %1
    if exist c:\names.log del c:\names.log
    for /F "delims=" %%j in ('dir c:\Directory /A-D /B /O:GEN') do echo %%~nj >> c:\names.log

    REM ------------------------------------------------------
    REM Read the list of hostnames, convert them to a list of
    REM hostnames / IP addresses.
    REM ------------------------------------------------------

    if exist c:\ipaddresses.log del c:\ipaddresses.log
    for /f %%i in (c:\names.log) do (
    for /f "skip=3 delims=: tokens=2 usebackq" %j in (`nslookup %i`) do @echo %i %j >> c:\ipaddresses.log
    )

    REM ------------------------------------------------------
    REM Clean up our IP address file to strip out IPs within
    REM the local group
    REM ------------------------------------------------------

    if exist c:\conlog.log del c:\conlog.log
    type c:\ipaddresses.log |findstr /V /E 192.168.*.* >> c:\conlog.log
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Pretty sure the guys over at the DOSTips forums will help you. I post on both forums and I see no point in posting on both websites. They have a lot more knowledge of batch scripting over there then most of us here.
     
  3. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Remember that when you use a FOR LOOP in a batch file you need double up the percent signs for the variable. You are missing the double percent sign on 4 different loop variables.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1059676