Jump to content

Using AutoIt as a Window killer


Recommended Posts

Hi there! I have just recently discovered AutoIt and I gotta say, VERY WELL DONE!

Here's what I need help with...

I am looking to possibly using Autoit as a means of preventing users from accessing certain applications and websites. Mostly websites. Now, before I'm told to use a firewall/proxy solution, for my specific situation, it doesn't work. So here's what we would like to be able to do.

I'd like to be able to have a text file which contains the names/titles of all Applications or websites that users might go to. Autoit could be running, and then when it detects any of the windows found in this list, it executes WinClose, terminating the window. As soon as the window was terminated, the program would continue scanning for these windows.

Is there a way to use WinWait but with a range of names instead of just one? Or would it be best to put them all into a file, have the script load the entries into an array, and everytime the user moves the mouse, or presses a key or something, it triggers an event to scan all windows for something that matches something in the list.

Any suggestions would be great. Thanks

Link to comment
Share on other sites

Not to scare you away from autoit or anything but for administration uses i strongly reccomend using a tool called automate, it comes with a handy gui and is probably exactly what your looking for. However if you wanted to use autoit you could just do a WinList ( ["title" [, "text"]] ) function (see help file).

Edited by SupraNatural

Visit http://www.blizzedout.com/forums/register....referrerid=8306 for the top blizzard hacks. WoW, TfT, D2/LOD, CS. You name it we got it!

Link to comment
Share on other sites

Hi there! I have just recently discovered AutoIt and I gotta say, VERY WELL DONE!

Here's what I need help with...

I am looking to possibly using Autoit as a means of preventing users from accessing certain applications and websites. Mostly websites. Now, before I'm told to use a firewall/proxy solution, for my specific situation, it doesn't work. So here's what we would like to be able to do.

I'd like to be able to have a text file which contains the names/titles of all Applications or websites that users might go to. Autoit could be running, and then when it detects any of the windows found in this list, it executes WinClose, terminating the window. As soon as the window was terminated, the program would continue scanning for these windows.

Is there a way to use WinWait but with a range of names instead of just one? Or would it be best to put them all into a file, have the script load the entries into an array, and everytime the user moves the mouse, or presses a key or something, it triggers an event to scan all windows for something that matches something in the list.

Any suggestions would be great. Thanks

<{POST_SNAPBACK}>

you may want to check out secbrowser, it's got a free trial, made by tropsoft i think, and it monitors to see what pages come up whenever browser is instanciated. can run it with an allowable list, a disallowed list, full block... blah blah blah. google it up though, worth a look and may save you some work.
Link to comment
Share on other sites

Not to scare you away from autoit or anything but for administration uses i strongly reccomend using a tool called automate, it comes with a handy gui and is probably exactly what your looking for. However if you wanted to use autoit you could just do a WinList ( ["title" [, "text"]] ) function (see help file).

<{POST_SNAPBACK}>

we already had a talk about this supra! stop taking the easy way out!!!

<2 guagmire>

While 1
 If WinExists("Window title here") then 
     WinKill("Window title here")__or you can assign it to a variable 
WEnd

thats a continuos looop it checks every ~750 milliseconds i think and itll run till u close it

for from a txt file

While 1
$text = "C:\block.txt"
_FileReadToArray($text,$aRecords)
For $x = 1 to $aRecords[0]
If WinExists($aRecords[$x]) Then 
    WinKill(-1);not sure if thatll work
Next
WEnd

</2 guagmire>

Edited by B3TA_SCR1PT3R

[right][font="Courier New"]...Run these streets all day, I can sleep when I die.[/font] [/right]

Link to comment
Share on other sites

Welcome to the forums!

A script of this nature would not be too hard to produce. You basically want to write a script that reads a file of strings, places them into an array and then every × milliseconds runs through the array of strings, closing any windows that match those titles.

Something like this should get you started:

#include <file.au3>
opt("winTitleMatchMode", 2)

local $i, $wins
_fileReadToArray("C:\MyFile", $wins)

while (1)
    for $i = 1 to $wins[0]
        winClose($wins[$i])
    next
    sleep(1000)
wEnd
Link to comment
Share on other sites

Welcome to the forums!

A script of this nature would not be too hard to produce. You basically want to write a script that reads a file of strings, places them into an array and then every × milliseconds runs through the array of strings, closing any windows that match those titles.

Something like this should get you started:

#include <file.au3>
opt("winTitleMatchMode", 2)

local $i, $wins
_fileReadToArray("C:\MyFile", $wins)

while (1)
    for $i = 1 to $wins[0]
        winClose($wins[$i])
    next
    sleep(1000)
wEnd

<{POST_SNAPBACK}>

Thanks for the info. Here's what I did.

#include <Array.au3>

Dim $line[500]

$file = fileopen("c:\test.dat",0)

$kills = 1

while 1

$line[$kills] = filereadline($file)

if @error = -1 then exitloop

$kills = $kills + 1

wend

fileclose($file)

while 1

$x = 1

while $x < $kills

if winexists($line[$x]) then

winclose($line[$x])

endif

$x = $x + 1

wend

wend

It seemed to work VERY well with only a few concerns. First off, I wonder if the file gets WAY big, if it will bog the machine down so I wonder if there's a better way to write this. Plus, the Autoit icon sits in the system tray so I need to be able to hide it.

Thanks for the suggestions though. Much appreciated.

Link to comment
Share on other sites

Thanks for the info. Here's what I did.

#include <Array.au3>

Dim $line[500]

$file = fileopen("c:\test.dat",0)

$kills = 1

while 1

$line[$kills] = filereadline($file)

if @error = -1 then exitloop

$kills = $kills + 1

wend

fileclose($file)

while 1

$x = 1

while $x < $kills

  if winexists($line[$x]) then

  winclose($line[$x])

  endif

  $x = $x + 1

wend

wend

It seemed to work VERY well with only a few concerns. First off, I wonder if the file gets WAY big, if it will bog the machine down so I wonder if there's a better way to write this. Plus, the Autoit icon sits in the system tray so I need to be able to hide it.

Thanks for the suggestions though. Much appreciated.

<{POST_SNAPBACK}>

i have a couple of suggestions to optimize code, but just to help me avoid looking foolish (if that can be helped) i'd like to try them before posting, it should only take a minute. can you attach your input file? test.dat so i'm using the same input
Link to comment
Share on other sites

i have a couple of suggestions to optimize code, but just to help me avoid looking foolish (if that can be helped) i'd like to try them before posting, it should only take a minute.  can you attach your input file? test.dat so i'm using the same input

<{POST_SNAPBACK}>

I've got the tray icon hidden, but I also need to have the process NOT consume 99% CPU which it currently does. Perhaps your addition/mod may do this.

Looking for any other input as well

Link to comment
Share on other sites

Oh ya, and I attached test.txt instead of test.dat because it didn't like the test.dat file. b sure to mod your code accordingly

<{POST_SNAPBACK}>

no sweat. ok here's what i changed it to, then i'll explain why i made the changes i made.

#NoTrayIcon 
#include <Array.au3>
HotKeySet("{pause}","ForTheLoveOfGodPleaseMakeItStop")
$file = fileopen("c:\test.dat",0)
$LongArseString = ""
while 1
$line = filereadline($file)
if @error = -1 then exitloop
$LongArseString = $LongArseString & $line
wend
fileclose($file)


while 1
$list = WinList()
for $x = 0 to $list[0][0]
    if StringInStr($LongArseString,$list[$x][0]) and stringlen($list[$x][0]) > 3 Then
        WinClose($list[$x][0])
    endif
Next
wend

Func ForTheLoveOfGodPleaseMakeItStop()
    Exit
EndFunc

first i got rid of the array that holds the disallowed list. rather than having a loop that checks each element of that array, i made a string that can be checked all at once with a StringInStr() check. This also eliminates the possibility of issues if your file ever exceeding 500 lines. and should speed up your code atleast a little bit because it cuts out all of the extra comparisons, and should save a little mem by cutting out the possibility of empty elements.

i got rid of the tray icon and added a kill button, so that you can stop the script any time you want by pressing pause, but incase you decide to block sites from access you won't have to worry about them being able to click on your icon to pause etc. i tried to smooth it out a little too, you had a couple of nested while loops, got rid of one of those, but that's just personal preference...

Link to comment
Share on other sites

no sweat.  ok here's what i changed it to, then i'll explain why i made the changes i made.

#NoTrayIcon 
#include <Array.au3>
HotKeySet("{pause}","ForTheLoveOfGodPleaseMakeItStop")
$file = fileopen("c:\test.dat",0)
$LongArseString = ""
while 1
$line = filereadline($file)
if @error = -1 then exitloop
$LongArseString = $LongArseString & $line
wend
fileclose($file)
while 1
$list = WinList()
for $x = 0 to $list[0][0]
    if StringInStr($LongArseString,$list[$x][0]) and stringlen($list[$x][0]) > 3 Then
        WinClose($list[$x][0])
    endif
Next
wend

Func ForTheLoveOfGodPleaseMakeItStop()
    Exit
EndFunc

first i got rid of the array that holds the disallowed list.  rather than having a loop that checks each element of that array, i made a string that can be checked all at once with a StringInStr() check.  This also eliminates the possibility of issues if your file ever exceeding 500 lines. and should speed up your code atleast a little bit because it cuts out all of the extra comparisons, and should save a little mem by cutting out the possibility of empty elements.

i got rid of the tray icon and added a kill button, so that you can stop the script any time you want by pressing pause, but incase you decide to block sites from access you won't have to worry about them being able to click on your icon to pause etc.  i tried to smooth it out a little too, you had a couple of nested while loops, got rid of one of those, but that's just personal preference...

<{POST_SNAPBACK}>

Thanks for the mods. Appreciated. Only problem I have now is that it still consumes WAY too much CPU time.

Does AutoIT have a DoEvents function like VB? You could place DoEvents anywhere in VB to allow the OS to do normal tasks. This prevented the application from consuming excessive CPU time.

Link to comment
Share on other sites

I tried your code here on my side and when I went to "Pogo.com" it did not kill the window.

BUT, I did try something with the script that appears to solve my CPU usage problem.

If I insert a sleep (1000) in my loop, after it completes the loop, it waits a full second before doing it again, this allows the OS to do OS tasks, and brings the CPU usage down to almost nothing.

Link to comment
Share on other sites

I tried your code here on my side and when I went to "Pogo.com" it did not kill the window.

BUT, I did try something with the script that appears to solve my CPU usage problem.

If I insert a sleep (1000) in my loop, after it completes the loop, it waits a full second before doing it again, this allows the OS to do OS tasks, and brings the CPU usage down to almost nothing.

<{POST_SNAPBACK}>

i see the issue. the data file lists portions of titles, and we're trying to search for full titles in partial ones... which obviously doesn't work, i'll rewrite it in just a minute (have to handle a call at work real fast)
Link to comment
Share on other sites

i see the issue.  the data file lists portions of titles, and we're trying to search for full titles in partial ones... which obviously doesn't work, i'll rewrite it in just a minute (have to handle a call at work real fast)

<{POST_SNAPBACK}>

here we go. i've just brought the file into an array, set the wintitlematch option to allow partial matches, then checked if any of them exist. so we're kind of back where we started, but with code a little smaller and faster....

OPT("WinTitleMatchMode",4)
#include <Array.au3>
#INCLUDE<file.au3>
HotKeySet("{pause}","ForTheLoveOfGodPleaseMakeItStop")
DIM $BLAH
$BLAH = _FileReadToArray("C:\test.dat",$BLAH)

while 1
for $x = 1 to UBound($BLAH)
    if WinExists($BLAH[$X]) Then
        WinClose($BLAH[$X])
    endif
    sleep(1000)
Next
wend

Func ForTheLoveOfGodPleaseMakeItStop()
    Exit
EndFunc
Link to comment
Share on other sites

here we go.  i've just brought the file into an array, set the wintitlematch option to allow partial matches, then checked if any of them exist.  so we're kind of back where we started, but with code a little smaller and faster....

OPT("WinTitleMatchMode",4)
#include <Array.au3>
#INCLUDE<file.au3>
HotKeySet("{pause}","ForTheLoveOfGodPleaseMakeItStop")
DIM $BLAH
$BLAH = _FileReadToArray("C:\test.dat",$BLAH)

while 1
for $x = 1 to UBound($BLAH)
    if WinExists($BLAH[$X]) Then
        WinClose($BLAH[$X])
    endif
    sleep(1000)
Next
wend

Func ForTheLoveOfGodPleaseMakeItStop()
    Exit
EndFunc

<{POST_SNAPBACK}>

Problem now is when I compile it to EXE, I still have a 99% usage issue. Damn-o
Link to comment
Share on other sites

Scrap the sleep portion not working. It was my own damn fault. Works well now...

Now to add all the fun stuff. My students are SO going to hate me now!!!

Thanks for the help bro!

<{POST_SNAPBACK}>

np man, glad i could help. just incase you have students that would recognize the exe in the task manager, you may want to name it something that they wont' be able to pick out instantly
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...