Winbatch - Telnet Connection & Sending Commands

Started by wattsupnow, February 12, 2016, 07:54:48 AM

Previous topic - Next topic

wattsupnow

I have been using the following script for the last 4 to 5 years running on Windows 7 64bit PC, the script is started via windows task scheduler. For years now its been working just fine. but this week it has start to fail on this area. Has there been a telnet option in winbatch that has been updated? Or is there another way to improve this script???


;===================================
;AddExtender("WWINT44I.DLL") ;-- Networking Extender
;AddExtender("WWWSK44I.DLL") ;-- Winsocket Extender
;AddExtender("WWPST44I.DLL") ;-- Postie Extender
;===================================
; 64BIT EXTENDERS
AddExtender("WWINT64I.DLL") ;-- Networking Extender
AddExtender("WWWSK64I.DLL") ;-- Winsocket Extender
;===================================
;===================================
:TELENT

        ALPHA_PROMPT = "USER1"
        ALPHA_PASSWORD = ""
        ALPHA_LOC = "172.33.1.10"
        ALPHA_PROMPT = "F2#"
        ALPHA_COM = "SUBMIT WH:EM943_2525.COM"

IF USE_TELNET == 0
RETURN
END IF

unixsocket=SOpen()

binbuf=BinaryAlloc(1000)
BinaryEODSet(binbuf,1000)
binaddr=IntControl(42,binbuf,0,0,0)

TELNET_STATUS=SConnect(unixsocket,ALPHA_LOC,23)

IF TELNET_STATUS != @TRUE
  err=wxGetLastErr()
  msg=wxGetErrDesc(err)
  ERROR = strCat("ERROR ", msg, timedate(),@CR)
LOG(LOG_PATH,APP_NAME,"TELNET ERROR: " , error)
STATUS = 1
GOTO LEAVE_DODGE
ENDIF

;WAIT AND SUBMIT USERNAME
waitfor="Username:"
GOSUB wait
line=strcat(line,@CR,"result=",found)
SSendLine(unixsocket,ALPHA_USER)

IF strlen(ALPHA_PASS) > 0 || ALPHA_PASS != ""
;WAIT AND SUBMIT PASSWORD (IF NEEDED UNCOMMENT)
waitfor="Password:"
duration=strcat(duration,"password start ",timedate(),@CR)
GOSUB wait
duration=strcat(duration,"password stop ",timedate(),@CR)
line=strcat(line,@CR,"result=",found)
SSendLine(unixsocket,ALPHA_PASS)
ENDIF

;WAIT AND SUBMIT JOB
waitfor=ALPHA_PROMPT
GOSUB wait
line=strcat(line,@CR,"result=",found)
SSendLine(unixsocket,ALPHA_COM)

LOG(LOG_PATH,APP_NAME,"TELNET: " , strcat("JOB SUBMITTED:",ALPHA_COM))

;WAIT AND SUBMIT LOGOFF
waitfor=ALPHA_PROMPT
GOSUB wait
line=strcat(line,@CR,"result=",found)
SSendLine(unixsocket,"LOGOFF")
SClose(unixsocket)

BinaryFree(binbuf)
RETURN

:wait
  line=""
  list="500,100,10,3,1"
  timeout=1000
  timmer=timeout
  pointer=1
  found=0
 
  WHILE timmer && (! found)
    FOR count=1 to ItemCount(list,",")
      size=ItemExtract(count,list,",")
      IF sOK2Recv(unixsocket,size)
sRecvBinary(unixsocket,binaddr,size)
stringsize=0;
raw=""
WHILE stringsize <= size
  raw=strcat(raw,BinaryPeekStr(binbuf,stringsize,size-stringsize))
  stringsize=stringsize+strlen(raw)
  IF stringsize < size
    stringsize=stringsize + 1
  ENDIF
ENDWHILE
GOSUB scandata
line=strcat(line,raw)
timmer=-1
BREAK
      ENDIF
    NEXT
    IF timmer== -1
      timmer=timeout
    ELSE
      IF strindex(line,waitfor,strlen(line)-(2*strlen(waitfor)),@FWDSCAN)
found = 1
      ENDIF
      timmer=timmer-1
    ENDIF
  ENDWHILE
RETURN
;===================================
;===================================
:scandata
      cindex=strindex(raw,num2char(255),0,@FWDSCAN)
      while cindex
if cindex+1 <= StrLen(raw)
  command=strsub(raw,cindex+1,1)
else
  command=num2char(sRecvNum(unixsocket,1))
endif
if char2num(command)>250 && char2num(command)<255
  if cindex+2 <= StrLen(raw)
    option=strsub(raw,cindex+2,1)
  else
    option=num2char(sRecvNum(unixsocket,1))
  endif
  if char2num(option)
    SSendString(unixsocket,strcat(num2char(255),num2char(252),option))
  endif
  raw=strcat(strsub(raw,1,cindex-1),strsub(raw,cindex+3,-1))
else
  raw=strcat(strsub(raw,1,cindex-1),strsub(raw,cindex+2,-1))
endif
if char2num(command)==255 && cindex+1 <= StrLen(raw)
    raw=strcat(strsub(raw,1,cindex-1),strsub(raw,cindex+1,-1))
endif
cindex=strindex(raw,num2char(255),cindex,@FWDSCAN)
   EndWhile
return
;===================================

td

You need to provide more detail than just 'start to fail on this area'.  What are the symptoms of the failure?  Are you getting WinBatch or Extender errors?  How have you tried to debug the problem?   What has changed on your system running the script that may impact the operation of the script?  Why do you think something needs to be updated in WinBatch  because your script is failing?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

wattsupnow

Quote from: td on February 12, 2016, 09:19:05 AM
You need to provide more detail than just 'start to fail on this area'.
Sorry was in a rush to a meeting, wanted to get this posted before.

Quote
  What are the symptoms of the failure?
No symptoms of the failure, no error codes or anything

Quote
Are you getting WinBatch or Extender errors?
None at all

Quote
How have you tried to debug the problem?
Yes, I have a debug (TRACE) of every run. Now I did notice in the  trace log around line 624 and 641 are looking strange to me.

Quote
What has changed on your system running the script that may impact the operation of the script?
Nothing as far as I am aware of - auto updates are turned off

Quote
  Why do you think something needs to be updated in WinBatch  because your script is failing?
Since I had this code for years now, thought there might be a better way of doing telnet and sending commands

I will attach the TRACE log and the .wbt


wattsupnow


td

Something has changed on your system, your network, or a target of your script; otherwise,  your script would still be working.   You still haven't indicated exactly what is failing and  'something strange' isn't particularly informative.  Post just the area of your trace that has the 'something strange' part so there is something to work with.   A 548 line script with many external dependencies that make it unexecutable isn't helpful by itself.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

wattsupnow

Quote from: td on February 12, 2016, 10:38:09 AM
Something has changed on your system, your network, or a target of your script; otherwise,  your script would still be working.   You still haven't indicated exactly what is failing and  'something strange' isn't particularly informative.  Post just the area of your trace that has the 'something strange' part so there is something to work with.   A 548 line script with many external dependencies that make it unexecutable isn't helpful by itself.

TD,
your right something on the host system has changed. I created this small script for testing. It is started with the Task Scheduler on Windows 7 and runs every 5 mins. When on the host system, lunching the exe from the desktop, the script will lunch the telnet client and send the commands, when I have it scheduled, the script will start but WILL NOT lunch the telnet client. I hope this helps.


        ; vars
ALPHA_USER = "USER1"
ALPHA_COM = "JOBS"
       
        ; start telnet client interface
        AppWaitClose("E-Term32.exe",@FALSE)
Run("E-Term32.exe", "")
TIMEDELAY(1)
SendKey("%ALPHA_USER%")
sendkey("{ENTER}")
TIMEDELAY(1)
SendKey("%ALPHA_COM%")
sendkey("{ENTER}")
TIMEDELAY(1)
SendKey("LO")
sendkey("{ENTER}")
TIMEDELAY(1)
exit

td

Getting a scheduled task to interact with the desktop can be a bit tricky.  You need to have it run as a  user and only run when the user is logged on.  You also need to determine whether or not the script needs to run with highest available privileges or not.   Using run instead of ShellExecute may cause problems depending on whether or not the application being started requires full admin privileges or not and at what level the scheduled task is set to run at.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

wattsupnow

after doing some more testing, with a clean install of windows 7, the scheduler and all works. So I am thinking a windows update messed everything up. Thank you for your help!!

td

It isn't the first time Windows Update was suspected of causing a problem...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade