Jump to content

Command Prompt batch file fails


Recommended Posts

So I thought to capture output another way..

$DOS = RunWait(@ComSpec & " /c " & "echo ?DT | nc 192.168.1.6 80", "", default, $STDERR_CHILD + $STDOUT_CHILD)
  Local $scan1 = StdoutRead( $DOS, default, binary = true)
  _ArrayDisplay( $scan1)
  MsgBox (0,"stdoutread","this is camip" & $scan1)


except the _ArrayDisplay( $scan1) doesn't display anything and $scan1 blank in MsgBox.
Possibly because the data is binary?
Now to try and write $scan1 to a file using _FileWriteFromArray() except there is no option for binary data.
There's a better way?

And it doesn't work "_FileWriteFromArray( "C:\ncat\newscan.dta", $scan1,1) with 3rd parameter either 0 or 1.  No file created.

Edited by fopetesl
More code

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites

4 hours ago, fopetesl said:

I've no issue except the second (not $DOS2) call to capture data to scan2.dtaalways fails even if I call it twice

$DOS2 = Run(@ComSpec & " /k " & "data1.bat") ; 2nd call works here
Sleep(100)
$DOS3 = Run(@ComSpec & " /k " & "data2.bat")
Sleep(100)
$DOS3 = Run(@ComSpec & " /k " & "data2.bat") ; 2nd call here doesn't

Dude.  Why don't you post the contents of the Batch files.  There is obviously something strange going on, and unless we have all the information, we are just going to continue running in circles chasing our tails....  If you are trying to receive the output that the commands which are running in the batch file are generating, then just forget about the batch file and migrate all of the commands over into the autoit script.  In all honesty, you seem to be making this wayyy harder than it has to be.

Edited by MattHiggs
Link to comment
Share on other sites

17 minutes ago, MattHiggs said:

Dude.  Why don't you post the contents of the Batch files.  There is obviously something strange going on, and unless we have all the information, we are just going to continue running in circles chasing our tails....  If you are trying to receive the output that the commands which are running in the batch file are generating, then just forget about the batch file and migrate all of the commands over into the autoit script.  In all honesty, you seem to be making this wayyy harder than it has to be.

OK, two similar batch files to collect 2 x 754 bytes each, max package size can send.
1)

echo off
    echo data1
    echo ?DT | nc 192.168.1.6 80 > scan1.dta
 


& 2)

echo off
echo data2
    echo ?DT | nc 192.168.1.6 80 > scan2.dta
Edited by fopetesl
data missing

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites


Earlier similar versions which return only eight bytes of mixed binary & ASCII data all
work 100%, e.g.

echo off
Rem Check if head is @ HOME..
echo ?HM | nc 192.168.1.6 80 > homerep
 

 

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites

14 minutes ago, fopetesl said:


Earlier similar versions which return only eight bytes of mixed binary & ASCII data all
work 100%, e.g.

echo off
Rem Check if head is @ HOME..
echo ?HM | nc 192.168.1.6 80 > homerep
 

 

 

So for some reason, I can't get netcat to work on my system (just sits at the console without running command.  I assume connection is timing out...).  However, maybe this link might at least point you in the right direction:

https://null-byte.wonderhowto.com/how-to/hack-like-pro-use-netcat-swiss-army-knife-hacking-tools-0148657/

ALso, what system are you trying to connect to.   "?HM" is not the windows environment variable for the home directory, so is this a windows or linux machine you are trying to access with netcat?

Link to comment
Share on other sites

And I don't know if this will help you at all, but you might want to consider using the Swiss file knife .  One of its many, many, many uses is as follows (copied from the command prompt help content):

 sfk httpserv   - run an instant HTTP server.
                       type "sfk httpserv -help" for help.
      sfk ftpserv    - run an instant FTP server
                       type "sfk ftpserv -help" for help.
      sfk ftp        - instant anonymous FTP client
      sfk web        - send HTTP request to a server
      sfk wget       - download HTTP file from the web
      sfk tcpdump    - print TCP conversation between programs
      sfk udpdump    - print incoming UDP requests
      sfk udpsend    - send UDP requests
      sfk ip         - tell own machine's IP address(es).
                       type "sfk ip -help" for help.
      sfk netlog     - send text outputs to network,
                       and/or file, and/or terminal
      sfk fromnet -h - receive and print network text
      sfk ping       - ping multiple machines in one go

 

Edited by MattHiggs
Link to comment
Share on other sites

2 hours ago, MattHiggs said:

So for some reason, I can't get netcat to work on my system (just sits at the console without running command.  I assume connection is timing out...).  However, maybe this link might at least point you in the right direction:

https://null-byte.wonderhowto.com/how-to/hack-like-pro-use-netcat-swiss-army-knife-hacking-tools-0148657/

ALso, what system are you trying to connect to.   "?HM" is not the windows environment variable for the home directory, so is this a windows or linux machine you are trying to access with netcat?

@MattHiggs. Some interesting reading links there though a quick scan doesn't tell me anything new.

The machine I'm communicating with is a fibre measurement system but it doesn't run LINUX though I did write the firmware for it 15 years ago.
The GUI now needs bringing up to date but has to still use Ethernet communication since there are quite a few machines around the world.  USB would be easier but in 2002 it wasn't really an option.
Looking at the Swiss File Knife, it's a versatile tool but I cannot see it overcoming the Run() or RunWait() problem.
Having attempted so many variations to read STDIO I suspect that AutoIt uses the same routines to perform the reading and the bug, (if there is one), is using the same code for all flavours.

Is there any other way to debug other that Jos' suggestion?

Who writes the native AutoIt code?

What version of Netcat are you trying to run? On what OS? There are both Windoze & LINUX versions available plus another bigger version which throws a "file not found" error. My Win nc version is 60KB v1.11

I just ran data1.bat by clicking from file explorer. It works. So it isn't a Windows issue, it's down to AutoIt.

 

Edited by fopetesl
Another test

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites

Not sure if I'm getting the correct result from the ncat, but I was able to get it to write to the .dta file:

#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <File.au3>


$DOS = Run(@ComSpec & " /c " & "echo ?DT | nc64 192.168.1.6 80", "", @SW_HIDE, $STDOUT_CHILD)
Local $line
While 1
    $line &= StdoutRead($DOS)
    If @error Then ExitLoop
    sleep(100)
Wend
$hFile = FileOpen(@ScriptDir & "\scan1.dta",  $FO_OVERWRITE)
FileWrite($hFile, $line)
FileClose($hFile)

I used nc64 but I'm pretty sure you can change that to nc and it'll still work.

EDIT: If the actual file is in binary, you can try this to see if it works, I can't test it because I'm denied from that IP you posted:

#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <File.au3>


$DOS = Run(@ComSpec & " /c " & "echo ?DT | nc64 192.168.1.6 80", "", @SW_HIDE, $STDOUT_CHILD)
Local $line
While 1
    $line &= StdoutRead($DOS, False, True)
    If @error Then ExitLoop
    sleep(100)
Wend
$hFile = FileOpen(@ScriptDir & "\scan1.dta",  $FO_OVERWRITE + $FO_BINARY)
FileWrite($hFile, $line)
FileClose($hFile)

 

Edited by anthonyjr2
add binary edit

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

 

3 minutes ago, fopetesl said:

Nice try, anthonyjr2 but it still doesn't work.

What's puzzling why it takes 18 seconds to time out.

Are you able to successfully run the commands just from the command line?  Is netcat actually communicating with the destination machine?  Is the script being compiled as x86?  Is it being compiled as a command line utility?

Link to comment
Share on other sites

2 minutes ago, MattHiggs said:

 

Are you able to successfully run the commands just from the command line?  Is netcat actually communicating with the destination machine?  Is the script being compiled as x86?  Is it being compiled as a command line utility?

Yes. ALL the commands either in a bat file or typed on command line work 100% from either a command prompt window or run from file explorer.

Script is not compiled is pure text.

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites

Can you post what we are supposed to get from the result? Because when I run it I'm guessing I don't have the required permissions so I just get an HTTP denial page. What does scan1.dta look like when you run the batch file by itself?

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

It appears I am incorrect in indicating Run() itself is the problem.
I have discovered what causes the symptoms described above.
Immediately prior to the Run() command I have a Progress meter:

   ProgressOn("Scanning Forward", "Scan Progress ...", "Working...")
            For $i = 0 To 140
			   ProgressSet($i)
   	           Sleep(100)
			Next
              ProgressSet(100, "Complete!")
            Sleep(500)
   ProgressOff()

If I remove this Progress() the Run() command works and I collect 750+ bytes without a problem.
When I reinstate Progress()the subsequent Run() fails.

So the bug is clearly some interaction between the two calls or my Progress() has an error.

 

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites

Post a full copy of your script, as there's obviously nothing wrong with Run and Progress and it must be something you're doing wrong.

When I say obviously nothing wrong, these commands have been used together before and you're the only person complaining that there's some kind of mysterious interaction between them.

Without knowing exactly how you're writing the script in all of it's parts we will not be able to troubleshoot your error. 

Also, I'm with anthonyjr2, why do you have a progress bar that just runs and indicates nothing?

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 comment
Share on other sites

Been away on another job for four days.

The machine has a scanning head which takes 14 seconds to complete a scan of a sample but there is no way at present of it feeding its position so the best I could come up with is a timed progress bar. 

;  MsgBox($MB_SYSTEMMODAL, "Scanning Status",$collectfilename)
  $DOS = Run(@ComSpec & " /c " & "scan.bat", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ;<< this works
;  $DOS = Run(@ComSpec & " /k " & "ncat 192.168.1.6 80 <scant >scanrep", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = _RunDOS("ncat 192.168.1.6 80 <scant.txt >scanrep")
;  ProcessWaitClose($DOS)
;  $Message = StdoutRead($DOS)
;  MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $Message)
   ProgressOn("Scanning Forward", "Scan Progress ...", "Working...")
            For $i = 0 To 140
               ProgressSet($i)
                  Sleep(100)
            Next
              ProgressSet(100, "Complete!")
            Sleep(500)
   ProgressOff()
    ;  $DOS = RunWait(@ComSpec & " /k " & "ncat 192.168.1.6 80 <getdata >scan1.dta", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = Run(@ComSpec & " /k " & "ncat 192.168.1.6 80 <getdata >scan1.dta", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = Run(@ComSpec & " /c " & "echo ?DT | ncat 192.168.1.6 80 > scan1.dta", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = Run(@ComSpec & " /k " & "data1.bat", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = RunWait(@ComSpec & " /k " & "data1.bat", $BatchDir, default, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = RunWait(@ComSpec & " /k " & "ncat 192.168.1.6 80 <getdata >scan1.dta", "", default, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = Run(@ComSpec & " /k " & "ncat 192.168.1.6 80 <getdata >scan2.dta", "", default, $STDERR_CHILD + $STDOUT_CHILD)
;;;;============================;;;;;;;;;;;;;;;;;;;;;;;;
;;  $DOS = RunWait(@ComSpec & " /k " & "data1.bat", "", default, $STDERR_CHILD + $STDOUT_CHILD)
;  $DOS = RunWait(@ComSpec & " /c " & "echo ?DT | nc 192.168.1.6 80", "", default, $STDERR_CHILD + $STDOUT_CHILD)
;  Local $scan1 = StdoutRead( $DOS, default, binary = true)
;  Local $iRc = _RunDos("sata1.bat")
;  If( $iRc == 0) Then
;     MsgBox(0,"stdoutread","error => " & @error)
;   EndIf
;  _ArrayDisplay( $scan1)
;  MsgBox (0,"stdoutread","this is camip" & $scan1)
;  _FileWriteFromArray( "C:\S3_GUI\ncat\newscan.dta", $scan1,1)
; Wait until the process has closed using the PID returned by Run.
;ProcessWaitClose($DOS)
; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : StdoutRead($iPID) = ' & StdoutRead($DOS) & @CRLF ) ;### Debug Console
;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : StdoutRead($iPID) = ' & StderrRead($DOS) & @CRLF ) ;### Debug Console
;
;  $DOS2 = Run(@ComSpec & " /k " & "data1.bat")
;  $DOS3 = Run(@ComSpec & " /k " & "data2.bat")
;  $DOS = Run(@ComSpec & " /c " & "RunDirect.exe", "", @SW_HIDE, $STDOUT_CHILD)
  _RunDos("RunDirect.exe") ; this just runs the $DOS2 & $DOS3 commands directly

The most powerful number in the Universe.  Zero.

Link to comment
Share on other sites

Been away on another job for four days.

The machine has a scanning head which takes 14 seconds to complete a scan of a sample but there is no way at present of it feeding its position so the best I could come up with is a timed progress bar.

 

This is egg-on-face time :'(

I did more hacking because I wasn't happy with my earlier conclusion.  It seems it is the initial "scan.bat" which is causing the failure. As yet I do not know why since it works as expected but then causes the failure of "data1.bat". I suspect there is a timing issue since they work from command line.

 

Spoiler

 

 

The most powerful number in the Universe.  Zero.

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

×
×
  • Create New...