Jump to content

DOS console tool


J_Y_C
 Share

Recommended Posts

So, I have a task that I needed to get the IP address of a set of websites. Now, I am firewalled where I am at, so my outgoing pings don't come back, but the IP is still resolved. I intended to get them by pinging them, but the built in ping method only returns time, and not IPs. As I got to thinking about it, it happens a lot where I issue a DOS command, but may need to interpret the results differently each time. So, I set out trying to find a way to get data from the DOS console. I couldn't find a post that discussed this, and I am sure there's probably an easier way, but I wrote a little tool that executes a DOs command and returns the console info.

func _DOS($var)
        ;$var is the dos command to be executed...
    RunWait(@ComSpec & " /c " & $var & " > " & @TempDir & "DOSconsoleGrab.txt","",@SW_HIDE)
    $df = FileOpen(@TempDir & "DOSconsoleGrab.txt",0)
    $data = FileRead($df)
    FileClose(@TempDir & "DOSconsoleGrab.txt")
    FileDelete(@TempDir & "DOSconsoleGrab.txt")
    Return $data
EndFunc

Basically, in DOS, any time you issue a command and then follow it with > and a file name, it will dump the console output to that file. So, this just executes a DOS command and dumps the console data to a temporary text file which it then reads from and give back to you, deleteing the temp files first.

I've never posted anything that might help someone else, and I am in debt to this board, so maybe this is a start?

Edited by J_Y_C
Link to comment
Share on other sites

  • 4 weeks later...

Below is an example of how to use the new StdoutRead, StderrRead and StdinWrite functions now available in AutoIt 3.2.

It fires a cmd file that contains a DIR command and a pause command. It then waits to see when the "Press any key to continue . . ." string is found in the stdout channel and send a carriage return to close the box.

I've used two rolling buffers for the situation if by chance the string is split across two consecutive reads.

;*********************

; test.au3

;*********************

#include <Constants.au3>

Dim $PID

Dim $numChars

Dim $buff

Dim $tmpBuff

Dim $bQuit

Dim $mySubstr

Dim $str1

Dim $str2

$mySubstr = "Press any key to continue . . ."

$bQuit = 0

$PID = Run("C:\test.cmd","C:\",@SW_HIDE,$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

$buff = ""

$tmpBuff = ""

While 1

$numChars = StdoutRead($PID,0,1)

If @error Then ExitLoop

If $numChars > 0 Then

$tmpBuff = StdoutRead($PID,$numChars)

$str1 = $buff & $tmpBuff

If StringInStr($str1,$mySubstr) Then

StdinWrite($PID,@CR)

StdinWrite($PID)

ExitLoop

EndIf

$buff = $tmpBuff

$tmpBuff = ""

EndIf

Sleep(500)

WEnd

While 1

$buff = StderrRead($PID)

If @error Then ExitLoop

MsgBox(0, "STDERR read:", $buff)

Wend

;***********************

; test.cmd

;***********************

DIR /s C:\

PAUSE

;**********************

Edited by singapuree
Link to comment
Share on other sites

Open a command window and type ping /?

I think that you can return the IP with the right switch.

If you search te forum you should be able to find a script that I posted a long time ago that would allow you to just enter ping and it wouls return a list of switches and how to use them

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Ah ha, thanks all.

It's actually been a minute since I posted this, and I think you are all correct, there are lots of better ways to do this. At the time, I just couldn't find one that was as fast for me.

If you look in AutoIt Smith's fileman, he has a great solution for reading/writing to stdIO:

http://www.autoitscript.com/forum/index.php?showuser=4117

I hope he doesn't mind that I posted it, he has it posted on another thread about TCP stuff, but it's relevant to this thread...

singapuree:

When I tried a similar method to what you posted, I could only get it to work with DOS functions. When I would run console based Java code, it wouldn't work with that...

Edited by J_Y_C
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...