Results 1 to 5 of 5

Thread: DOS batch scripting - read a list of hosts from a file

  1. #1
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    DOS batch scripting - read a list of hosts from a file

    I'm trying to write a simple DOS batch file to carry out some commands to remote machines. The list of remote machines needs to be read from an external file (eg. hosts.txt) which would look like this:

    hosts.txt
    ======
    111.222.223.001
    111.222.223.002
    111.222.223.003
    . . .
    111.222.223.099

    i.e. a list of IP addresses.

    Now, the commands I need to run for each of these machines (IP addresses) are similar to the following:

    sc \\%1 stop psex...
    del \\%1\admin$\psex...
    del \\%1\admin$\system32\psex...
    sc \\%1 delete psex...

    i.e. for each IP address listed in hosts.txt, I want to run the above four commands. So, how do I read in my hosts.txt into my batch file such that each IP address in hosts.txt is substituted for %1 in the above four commands, all in a loop so that each line of hosts.txt is read?

    I hope that makes sense! Note that this will need to be run on a Windows 2003 Server machine, in a command prompt.

  2. #2
    Administrator Moby-Dick's Avatar
    Join Date
    Jul 2003
    Location
    There's no place like ::1 (IPv6 version)
    Posts
    10,665
    Thanks
    53
    Thanked
    385 times in 314 posts

    Re: DOS batch scripting - read a list of hosts from a file

    You need to use the FOR command
    my Virtualisation Blog http://jfvi.co.uk Virtualisation Podcast http://vsoup.net

  3. #3
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Re: DOS batch scripting - read a list of hosts from a file

    ^ Thanks, Moby!

    Anyway, i've done a bit of digging around and cobbled the following script:

    @echo off
    echo PSexec Cleanup Utility V1.0
    echo.
    echo.

    if "%1" NEQ "" goto :checkfile
    echo Usage: cleanup hosts_file
    echo.
    goto :eof

    :checkfile
    if exist %1 goto :doping
    echo Input file does not exist
    echo.
    goto :eof

    :doping
    for /f "tokens=*" %%I in (%1) do call :cleanup %%I
    goto :eof

    :cleanup

    echo Cleaning up machine: %1 ...
    sc \\%1 stop psexesvc
    del \\%1\admin$\psexesvc.exe
    del \\%1\admin$\system32\psexesvc.exe
    sc \\%1 delete psexesvc
    echo ----------------------------------------------------
    :: DONE

    ^ No space after '::' and DONE on the last line. I had to show one to stop a smiley being displayed!

    Just call it cleanup.bat and create a file listing your hostnames/IP addresses and then run the script with the name of hostnames file as the argument.

    I hope that helps someone out in the future if you ever need to script remote commands to multiple machines.

  4. #4
    Ex-MSFT Paul Adams's Avatar
    Join Date
    Jul 2003
    Location
    %systemroot%
    Posts
    1,926
    Thanks
    29
    Thanked
    77 times in 59 posts
    • Paul Adams's system
      • Motherboard:
      • Asus Maximus VIII
      • CPU:
      • Intel Core i7-6700K
      • Memory:
      • 16GB
      • Storage:
      • 2x250GB SSD / 500GB SSD / 2TB HDD
      • Graphics card(s):
      • nVidia GeForce GTX1080
      • Operating System:
      • Windows 10 x64 Pro
      • Monitor(s):
      • Philips 40" 4K
      • Internet:
      • 500Mbps fiber

    Re: DOS batch scripting - read a list of hosts from a file

    Something like this might do you:
    Code:
    @echo off
    for /f %%i in (hosts.txt) do call :ACTIONS %%i
    
    :ACTIONS
    sc \\%1 stop psex...
    del \\%1\admin$\psex...
    del \\%1\admin$\system32\psex...
    sc \\%1 delete psex...

    Okies, I see you found the same thing pretty much
    Last edited by Paul Adams; 20-08-2007 at 01:54 PM. Reason: Replied to own post while I was writing :p
    ~ I have CDO. It's like OCD except the letters are in alphabetical order, as they should be. ~
    PC: Win10 x64 | Asus Maximus VIII | Core i7-6700K | 16GB DDR3 | 2x250GB SSD | 500GB SSD | 2TB SATA-300 | GeForce GTX1080
    Camera: Canon 60D | Sigma 10-20/4.0-5.6 | Canon 100/2.8 | Tamron 18-270/3.5-6.3

  5. #5
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Re: DOS batch scripting - read a list of hosts from a file

    ^ Thanks, Paul. It was the /f after the 'for' that was throwing me!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help on my maxtor harddrives
    By arthurleung in forum PC Hardware and Components
    Replies: 10
    Last Post: 12-06-2007, 09:40 PM
  2. Replies: 13
    Last Post: 03-05-2005, 08:21 AM
  3. Dos Batch File Help
    By EvilMunky in forum Help! Quick Relief From Tech Headaches
    Replies: 4
    Last Post: 08-03-2005, 12:15 PM
  4. Batch File
    By mrdata2003 in forum Software
    Replies: 4
    Last Post: 26-01-2004, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •