Jump to content

Read a text file of multiple IPs and launch against a set of lines.

Recommended Posts

Upfront notice: Just started working with AutoIt3, so noob to the language still. I'm not a total noob with programming just trying to wrap my head around AutoIt3 specifically. :imwithstupid:

Okay, here's the scenario. I want to hand my script a txt file of IP addresses (could be any number of IPs). What I want to do is have the script look at the text file and perform an action against a number of those addresses (essentially creating multiprocessing) and continue to loop through the file until its ran against all IPs.

I've been looking through the help files. I'm assuming it's a mix of




I'm having a bit of trouble figuring out the format for it in the script.

So example.

You have a txt file of say 57 lines each line has one IP address.

I want to run a psexec command against each one of these IPs but I don't want to do one at a time would take to long.

So I define a variable that will stand for how many machines to run against at once (how many instances of psexec in this case). Let's say we set the variable at 5.

How can I code it for the script to read the text file grab the first 5 lines as an array run a psexec command against each simultaneously (psexec $line1 ping localhost, for example) then loop back into the file grab the next 5 lines and so on and so forth until run against all lines?

Thanks for the help! :)

Link to post
Share on other sites
  • Moderators

Understand that AutoIt it not multi-threaded, so whether you grab one at a time or five at a time, you're going to be processing each IP one at a time. You'll probably realize better performance if you read them all into an array and be done with it, rather than the constant back and forth.


Something like this should get you started:

#include <Array.au3>
#include <File.au3>

$aArray = FileReadToArray(@DesktopDir & "\IPs.txt")
    For $element In $aArray
        ;Do stuff. Check out the Ping function, I would suggest pinging first, then taking action if you get a response.
Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites

Right thanks for the clarification on multi-threading. I get the feeling that word creates some strong responses here. :) I think I'm more looking for multi-processing in this case (multi-threading would be great, but I will work with what I can get. :)) I've seen a few examples on ways to handle child scripts/programs. I'll dig into them more.

Looking at your example I'm assuming I could do that and couple it with a _FileCountLines to control how many child processes I launch? I get it still will only happen one at a time, but on some networks I would run this script throttling the connections would be necessary and I wouldn't want to start another child process until ones before it finished. :)

Link to post
Share on other sites

You don't need _filecountlines when you use FileReadToArray or _FileReadToArray the count of lines is easy to get. Using the first function you'd use Ubound on the array to tell you how many lines it has, with the second function you'd look in the $array[0] element for the line count, if you've set it up to return it in the zero element.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites


Not exactly what you need but an example of pinging 100 randomly generated IP addresses (100 ms timeout)...

#include <array.au3>

; generate 100 random ip addresses on node 99.xxx.xxx.xxx ----------------------------------------------

local $ip_out, $fname = @scriptdir & '\ip.txt'
for $1 = 1 to 100
    $ip_out &= '99.' & random(0,255,1) & '.' & random(0,255,1) & '.' & random(0,255,1) & @crlf

; start of your code / using file generated above as input ---------------------------------------------

local $aIP = stringsplit(fileread($fname),@crlf,1), $rname = @scriptdir & '\RSLT.txt'
local $aRSLT[$aIP[0]-1][2]

for $1 = 1 to $aIP[0]-1
    $aRSLT[$1-1][0] = $aIP[$1]

local $st = timerinit()

progresson("Pinging IP's",'','')
for $1 = 0 to ubound($aRSLT) - 1
    $aRSLT[$1][1] = ping($aRSLT[$1][0],100)
    progressset(($1/ubound($aRSLT)) * 100,$aRSLT[$1][0],'')

ConsoleWrite(stringformat('Time to ping = %2.4f seconds',timerdiff($st)/1000) & @LF)

$ip_out = ''
for $1 = 0 to ubound($aRSLT) - 1
    $ip_out &= stringformat('%-20s  %-10s',$aRSLT[$1][0],$aRSLT[$1][1]) & @CRLF



Am working on something that will read a file then run a fixed number of scripts simultaneously passing 1 line for each script as a parm, until all lines have been passed.


Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to post
Share on other sites

I use a slightly modified version of Madadar's   _CheckOnlineStatus() UDF in a few of my scripts. I bump up the  $i_MaxProcess to 100 and can ping and get results for over 900 PCs in about 15 seconds.


Post a link to it...should solve jdash's problem....

edit: >Manadar's IP Scanner

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to post
Share on other sites


After re-visiting this thread it occured to me that we did not answer your core question, "read a file and process 'x' number of lines at a time". 

The following scripts are a driver / slave setup designed to do just that.

Driver script (Mult Runner.au3)

   Read a file.

  Call a "slave" script for each line of the file passing the line as a parameter to the "slave" script.

  Provide a throttle to control max number of "slave" scripts that can run concurrently.

  Log script start time to a common log.

Slave script (Slave.au3)

  Verify parm.

  Log start/stop time and PID to a common log.

Log Function

  All scripts write to a common log file.  File locking is done with _WinAPI_FileInUse.

  Note - I have had an intermittent file open problems when trying to run 50+ concurrent "slave" scripts.

Both scripts and the input file must reside in the same dir.  The log file will be created there also.

The "Slave" script is setup to do nothing more than start, sleep for a random time and end.  I would advise running the driver script with your input file and leaving the slave script as is till you are familiar with it's behaviour.








Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...