Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Sample Telnet Script

Keywords:   telnet

Not sure if your interested but I wrote this so I thought I would pass it on for others to benefit from.

This improves on the telnet script to allow the wait subroutine to time out and also improves performance on reading large amounts of data coming across.

From: Matthew McGillis

AddExtender("WWWSK34I.DLL")
unixsocket=SOpen()
binbuf=BinaryAlloc(1000)
BinaryEODSet(binbuf,1000)
binaddr=IntControl(42,binbuf,0,0,0)
prompt="$ "
user="mmcgilli"
password="xxxxxxx"
hostname="hpds03.starbucks.net"

status=SConnect(unixsocket,hostname,23)
if status != @TRUE
  err=wxGetLastErr()
  msg=wxGetErrDesc(err)
  Message("Winsock Error A %err%",msg)
endif

waitfor="login:"
duration=strcat("login start ",timedate(),@CR)
GoSub wait
duration=strcat(duration,"login stop ",timedate(),@CR)
line=strcat(line,@CR,"result=",found)
;Message("result",line)
SSendLine(unixsocket,user)
waitfor="Password:"
duration=strcat(duration,"password start ",timedate(),@CR)
GoSub wait
duration=strcat(duration,"password stop ",timedate(),@CR)
line=strcat(line,@CR,"result=",found)
;Message("result",line)
SSendLine(unixsocket,password)
waitfor=prompt
duration=strcat(duration,"prompt start ",timedate(),@CR)
GoSub wait
duration=strcat(duration,"prompt stop ",timedate(),@CR)
line=strcat(line,@CR,"result=",found)
SClose(unixsocket)
BinaryFree(binbuf)
Message("result",line)
Message("Time",duration)
Exit

: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
	  ;if waitfor==prompt then Debug(1)
	  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

Article ID:   W12610
Filename:   Improved Telnet Sample.txt