Jump to content

[Resolved] Read CMD window for a string, then run if/then statement


Recommended Posts

I'm writting a script to send data through a telnet session to a server and run a general summary and output it to a text file then close the telnet and CMD window.

How can I code autoIT to say, if the command window string = "----more----" send the spacebar key, else continue on with the rest of the program?

I'm still pretty new to autoIT and cycling through the wiki and such, but not sure what syntax to be looking for. Some sort of read screen?

Thanks for any direction given.

-nick

Edited by xeroTechnologiesLLC
Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 237, 130, 192, 114)
$Button1 = GUICtrlCreateButton("Run", 72, 56, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  case $Button1
   run("CMD")
   Send(" telnet " & "###.###.###.###" & " -f" & " c:Output.txt" & @CR & "username" & @CR & "password" & @CR & "sum" & "{SPACE}" & "{SPACE}" & "{SPACE}" & "{SPACE}" & "{SPACE}" & "{SPACE}" & @CR & "exit" & @CR & "exit")
EndSwitch
WEnd
Exit

This little gem runs through quite a bit in the first cmd window, then stops and closes that then runs another on top of it that errors, then runs another that goes about 99% the way through and errors

but it all writes to the text file...

lol

It seems amazingly buggy for something so simple - imo, but i'm not sure what i'm doing wrong.

I have tried splitting the commands up into a line by line send - that gets even worse, opening multiple instances of the cmd window.

Link to comment
Share on other sites

Now I'm lost...

local $cmd = run(@ComSpec & " telnet " & $IPAddressExtracted & " -f C:output.txt")
  Send("hi")

As I understand this code - it should open a command prompt, telnet to the ip address and then type in the word 'hi' and output it all to a text file.

But when the code hits the Send line - the focus goes back to my program and tries to send the keys there...why?

I've been working on this for a few days and all I want to do is send a few keys to the CMD prompt.

I tried to create a TCP session to the CMTS server I'm trying to connect to but it wouldn't take the variable as input for the IP address.

I'm running out of routes to follow and would appreciate any help available.

Thanks in advance,

Nick.

Link to comment
Share on other sites

I decided to create a second script that was just telnetting to our CMTS, doing a login macro and running a basic command and exiting.

Oddly, standalone, that worked.

When I copied and pasted it back into my program's function - it worked. Not much was different other than the /k trigger.

Below is the code. I'm still working on the IF statements as they do not currently work.

func telnetOut($IPAddressExtracted, $upstreamExtracted, $upstreamShortExtracted)
;~   MsgBox(0, "", $IPAddressExtracted)
  local $cmd = run(@ComSpec & " /k ")
  WinActivate($cmd)
  Sleep(100)
  Send("telnet " & $IPAddressExtracted & " -f c:outTest.txt" & @CR, 1)
  Sleep(100)
  Send("nicholas.willcox" & @CR)
  Sleep(100)
  send("re11P3r_100" & @CR)
  Sleep(100)
  send("show cable modem c" & $upstreamExtracted & " | exc online" & @CR, 1)
  Sleep(100)
  send(" ")
  send(" ")
  send(" ")
  send(" ")
  send(" ")
  send(" ")
  send(" ")
  send(" ")
  send(@CR)
   If GUICtrlGetState($CMTSsumtotal) = $GUI_CHECKED Then
    Send("scmt" & @CR)
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(@CR)
   EndIf
   If guictrlgetstate($CMTSintsum) = $GUI_CHECKED Then
    send("show int sum" & @CR)
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(" ")
      send(@CR)
   EndIf
   If GUICtrlGetState($CMTSsnr) = $GUI_CHECKED Then
    send("show controllers c" & $upstreamExtracted & " | inc snr" & @CR)
     send(@cr)
   EndIf
  send(@CR)
  send(@CR)
  Send(" exit" & @CR)
  Sleep(1000)
  Send("e")
  Sleep(1000)
  Send(" exit" & @CR)
EndFunc

edit: addition

I opt for this route mainly because when I tried TCP functions - it would not connect to the cmts and would always say the ip or port was wrong - which the ip was a variable, shown above, and the port was always 23 - which is a basic telnet session that I know already connects just fine standalone.

If anyone has any thoughts on why it would not work - I'm open for information.

Edited by xeroTechnologiesLLC
Link to comment
Share on other sites

If you're trying to run commands in cmd's, you should really write to standard streams. heres a basic example:

$Pid = Run("<your program>",@WorkingDir, @sw_hide,0x1) ; pipe stdin
Stdinwrite($pid,"<your command>" & @LF) ; linefeed works like enter on a normal prompt.

Here's an example running a dir command:

$Pid = Run("cmd.exe",@WorkingDir, @sw_hide,0x1) ; pipe stdin
Stdinwrite($pid,"dir" & @LF) ; linefeed works like enter on a normal prompt.
Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

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...