Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript and Winsock 1

Status
Not open for further replies.

bbh1

Programmer
Oct 20, 2004
2
US
Is it possible to use Winsock with a script to send a command to a remote host? I've been searching around on the web and it is very hard to find information on how to do this. I've found some sample code, but I don't know what kind of commands I can call on this winsock object (ie. what is the api?).

My goal is to have a client script to:
1) connect to the remote host
2) send a command... this will cause the remote host to run a command which takes approximately 5 minutes to complete.
3) capture any data sent back to the client and write to a file
4) client detects that server operation is done
5) client disconnects

is this possible with a script? or do I have to use something like VB? This is to be part of an automated process so I don't want to require any gui or user input.

Thanks in advance,
Beth

Here is some sample code that I found:

set winsock = createobject("MSWINSOCK.Winsock")
winsock.Remotehost = "127.0.0.1"
winsock.RemotePort = 695

 
Hello Beth,

It is indeed possible to do this. There are limitations however. Since the Winsock control is a "licensed control" one must pass a runtime license to the script host.

This works fine for an HTA or even an HTML page, because there is a special IE mechanism for doing it.

WSH has no such facility however. This means that such a script, whether WScript or CScript, will only run on machines that have a developer license installed, usually by installing Visual Studio or VB.

Don't give up though. I have had excellent luck employing the free Dimac w3Sockets component. Go to Dimac, select w3Sockets from the list in the left nav-area of the page under Free Products.

While designed for use from an ASP page, it works great in a .vbs or .wsf file. You didn't say what the remote server type is. w3Sockets supports Telnet as well as bare TCP sockets though. If you need to talk to an HTTP server, you'll need to send the HTTP protocol manually over a bare socket. But the Winsock control would leave you with that issue anyway.

I'm making the assumption that you want a blocking socket. w3Sockets can only operate in this fashion. For simple applications this is a boon. The Winsock control can't do this, and you must write event-handling logic to deal with its async nature.


I'm sure there are other free alternatives as well. I use this one because I need to deal with Telnet off and on, and contrary to a lot of sample code, Telnet <> TCP. Most Telnet servers will spew a lot of Telnet option negotiation sequences and such (and expect same back). w3Sockets can (optionally) deal with this for you "behind the scenes."


Trivial sample, dimac.wsf:
Code:
<job>
  <object id="w3sock" progid="socket.tcp" />
  <script language="vbscript">
    w3sock.DoTelnetEmulation = True
    w3sock.TelnetEmulation = "TTY"
    w3sock.Host = "localhost:2023"
    w3sock.Open
    w3sock.WaitFor "login:"
    w3sock.SendLine "George"
    w3sock.WaitFor "password:"
    w3sock.SendLine "XXXXX"
    w3sock.WaitFor ">"
    w3sock.SendLine "prompt $LREADY$G"
    w3sock.WaitFor "<READY>"
    w3sock.SendLine "dir"
    w3sock.WaitFor "<READY>"
    MsgBox w3Sock.Buffer
    w3sock.Close
  </script>
</job>
Here I tell w3Sockets I want to do Telnet, terminal type "TTY" (dumb as possible), and I log on.

Then I set the Windows Telnet server's command prompt to <READY> (by using $LREADY$G) because there will be > symbols in the text I want to capture.

Then I send a [tt]dir[/tt] command, and wait for the command prompt to tell me I have it all. Then I grab the results from the buffer and display it.

If you prefer to write old-style .vbs files, you'll need something like:

[tt]Dim w3sock
Set w3sock = CreateObject("socket.tcp")[/tt]
 
In case it is of interest, here's a capture of the TCP traffic between my sample script above and the Telnet server. Look at the "junk" sent to and fro, especially near the beginning of the session.

It also explains why my script connects to port 2023 instead of 23 (the Telnet port). I was shunting through TCPTap to see the traffic.


[sub][tt] TCPTap - TCP Traffic Capture Tool (0.10.1 beta)

Capture Mode: ASCII
Client Port: 2023
Server Port: 23
Server Host: localhost
Capture file created: C:\Documents and Settings\George\Desktop\w3sock.log

Capture initiated: 2:41:49 AM


02:41:52 Client connected. Port 2023 Host [IP 127.0.0.1]


02:41:52 Server connected. Port 23 Host "localhost" [IP 127.0.0.1]

02:41:52 <- 0000 FF FD 25 FF FB 01 FF FB 03 FF FD 27 FF FD 1F FF ÿý%ÿû.ÿû.ÿý'ÿý.ÿ
0010 FD 00 FF FB 00 ý.ÿû.
02:41:52 -> 0000 FF FC 25 ÿü%
02:41:52 -> 0000 FF FD 01 ÿý.
02:41:52 -> 0000 FF FD 03 ÿý.
02:41:52 -> 0000 FF FC 27 ÿü'
02:41:52 -> 0000 FF FC 1F ÿü.
02:41:52 -> 0000 FF FD 00 ÿý.
02:41:52 <- 0000 57 65 6C 63 6F 6D 65 20 74 6F 20 4D 69 63 72 6F Welcome to Micro
0010 73 6F 66 74 20 54 65 6C 6E 65 74 20 53 65 72 76 soft Telnet Serv
0020 69 63 65 20 0D 0A 0A 0D 6C 6F 67 69 6E 3A 20 ice ....login:
02:41:52 -> 0000 47 65 6F 72 67 65 0D 0A George..
02:41:52 <- 0000 47 65 6F 72 67 65 0A 0D 70 61 73 73 77 6F 72 64 George..password
0010 3A 20 :
02:41:52 -> 0000 XX XX XX XX XX 0D 0A XXXXX..
02:41:52 <- 0000 FF FD 18 ÿý.
02:41:52 -> 0000 FF FB 18 ÿû.
02:41:52 <- 0000 FF FA 18 01 FF F0 ÿú..ÿð
02:41:52 -> 0000 FF FA 18 00 54 54 59 FF F0 ÿú..TTYÿð
02:41:52 <- 0000 FF FA 18 01 FF F0 ÿú..ÿð
02:41:52 -> 0000 FF FA 18 00 54 54 59 FF F0 ÿú..TTYÿð
02:41:52 <- 0000 0D 0A 0D 0A 2A 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ....*===========
0010 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0020 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0030 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0040 3D 3D 3D 3D 0D 0A 57 65 6C 63 6F 6D 65 20 74 6F ====..Welcome to
0050 20 4D 69 63 72 6F 73 6F 66 74 20 54 65 6C 6E 65 Microsoft Telne
0060 74 20 53 65 72 76 65 72 2E 0D 0A 2A 3D 3D 3D 3D t Server...*====
0070 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0080 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0090 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
00A0 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 0D 0A 43 3A 5C ===========..C:\
00B0 44 6F 63 75 6D 65 6E 74 73 20 61 6E 64 20 53 65 Documents and Se
00C0 74 74 69 6E 67 73 5C 47 65 6F 72 67 65 3E ttings\George>
02:41:52 -> 0000 70 72 6F 6D 70 74 20 24 4C 52 45 41 44 59 24 47 prompt $LREADY$G
0010 0D 0A ..
02:41:52 <- 0000 70 72 6F 6D 70 74 20 24 4C 52 45 41 44 59 24 47 prompt $LREADY$G
0010 0D 0A 0D 0A 3C 52 45 41 44 59 3E ....<READY>
02:41:52 -> 0000 64 69 72 0D 0A dir..
02:41:52 <- 0000 64 69 72 0D 0A 20 56 6F 6C 75 6D 65 20 69 6E 20 dir.. Volume in
0010 64 72 69 76 65 20 43 20 69 73 20 53 79 73 74 65 drive C is Syste
0020 6D 0D 0A 20 56 6F 6C 75 6D 65 20 53 65 72 69 61 m.. Volume Seria
0030 6C 20 4E 75 6D 62 65 72 20 69 73 20 39 34 45 44 l Number is 94ED
0040 2D 31 46 45 42 0D 0A 0D 0A 20 44 69 72 65 63 74 -1FEB.... Direct
0050 6F 72 79 20 6F 66 20 43 3A 5C 44 6F 63 75 6D 65 ory of C:\Docume
0060 6E 74 73 20 61 6E 64 20 53 65 74 74 69 6E 67 73 nts and Settings
0070 5C 47 65 6F 72 67 65 0D 0A 0D 0A 31 32 2F 32 31 \George....12/21
0080 2F 32 30 30 31 20 20 30 35 3A 33 33 20 50 4D 20 /2001 05:33 PM
0090 20 20 20 3C 44 49 52 3E 20 20 20 20 20 20 20 20 <DIR>
00A0 20 20 2E 0D 0A 31 32 2F 32 31 2F 32 30 30 31 20 ...12/21/2001
00B0 20 30 35 3A 33 33 20 50 4D 20 20 20 20 3C 44 49 05:33 PM <DI
00C0 52 3E 20 20 20 20 20 20 20 20 20 20 2E 2E 0D 0A R> ....
00D0 31 32 2F 32 32 2F 32 30 30 31 20 20 30 32 3A 34 12/22/2001 02:4
00E0 31 20 41 4D 20 20 20 20 3C 44 49 52 3E 20 20 20 1 AM <DIR>
00F0 20 20 20 20 20 20 20 44 65 73 6B 74 6F 70 0D 0A Desktop..
0100 31 32 2F 31 37 2F 32 30 30 31 20 20 31 30 3A 31 12/17/2001 10:1
0110 33 20 50 4D 20 20 20 20 3C 44 49 52 3E 20 20 20 3 PM <DIR>
0120 20 20 20 20 20 20 20 46 61 76 6F 72 69 74 65 73 Favorites
0130 0D 0A 31 32 2F 31 37 2F 32 30 30 31 20 20 31 30 ..12/17/2001 10
0140 3A 31 33 20 50 4D 20 20 20 20 3C 44 49 52 3E 20 :13 PM <DIR>
0150 20 20 20 20 20 20 20 20 20 4D 79 20 44 6F 63 75 My Docu
0160 6D 65 6E 74 73 0D 0A 31 31 2F 30 38 2F 32 30 30 ments..11/08/200
0170 31 20 20 31 30 3A 30 32 20 41 4D 20 20 20 20 20 1 10:02 AM
0180 20 20 20 20 20 20 20 31 33 2C 38 32 34 20 50 45 13,824 PE
0190 44 2E 62 6F 78 0D 0A 31 31 2F 31 34 2F 32 30 30 D.box..11/14/200
01A0 31 20 20 30 36 3A 31 30 20 50 4D 20 20 20 20 3C 1 06:10 PM <
01B0 44 49 52 3E 20 20 20 20 20 20 20 20 20 20 53 74 DIR> St
01C0 61 72 74 20 4D 65 6E 75 0D 0A 30 39 2F 32 31 2F art Menu..09/21/
01D0 32 30 30 31 20 20 30 38 3A 32 34 20 50 4D 20 20 2001 08:24 PM
01E0 20 20 3C 44 49 52 3E 20 20 20 20 20 20 20 20 20 <DIR>
01F0 20 57 49 4E 44 4F 57 53 0D 0A 20 20 20 20 20 20 WINDOWS..
0200 20 20 20 20 20 20 20 20 20 31 20 46 69 6C 65 28 1 File(
0210 73 29 20 20 20 20 20 20 20 20 20 31 33 2C 38 32 s) 13,82
0220 34 20 62 79 74 65 73 0D 0A 20 20 20 20 20 20 20 4 bytes..
0230 20 20 20 20 20 20 20 20 37 20 44 69 72 28 73 29 7 Dir(s)
0240 20 20 20 33 2C 36 34 38 2C 34 38 37 2C 34 32 34 3,648,487,424
0250 20 62 79 74 65 73 20 66 72 65 65 0D 0A 0D 0A 3C bytes free....<
0260 52 45 41 44 59 3E READY>

02:42:02 Client port 2023 closed.


Capture terminated: 2:42:02 AM

Client Port Traffic Totals: 13 sent 77 bytes.
Server Port Traffic Totals: 9 xcvd 948 bytes.

- - - - - - - - - - - - - - - End of TCPTap capture report. - - - - - - - - - - - - - - -[/tt][/sub]
 
Just a though, what about remote scripting ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Very good suggestion, PHV. Might be just the thing for Beth.

In my own case the remote servers are often mainframes, Unix boxes, or even routers though. It's rare when I use Telnet from a Windows script to a Windows server - and those are typically cross-enterprise (somebody else's Windows Telnet server, via a VPN).
 
Just following this thread...I hope Beth got herself sorted...

Can anyone suggest how I can get something like w3sockets or toolsack socket for UDP instead of HTTP or TCP?

I need to query a server that will only accept UDP and can find no tools to let me do this via VBScript/WSH.

Also I don't have a runtime licence for Windows Winsock but if I did would that solve the problem?

Windows XP/VBScript
 
Catalyst (at offers a freeware version of their SocketWrench product. This may do what you need. Look under "downloads."

The Winsock control licensing issue may have been a little confusing. It has to do with the way ActiveX runtime licenses are managed. A licensed developer has to embed a runtime license into a developed product such as a program or even an Internet Explorer web page or HTA.

A runtime license isn't something you can "obtain" as such.

The issue is that WSH has no support whatsoever for ActiveX license embedding. The bottom line is that a WSH script using the Winsock control requires a developer license on every machine it is to run on. These are installable at the machine level, and they are installed with products like Visual Studio or Office Developer. Pretty clumsy things to deploy with your script though.

So the answer is to either deploy an unlicensed version of the Winsock control (I've never seen one released) or some other ActiveX wrapper for the Winsock API like Socketwrench, Toolsack Socket, etc.
 
Terrific! Yup I have a working UDP connection to the server I need. I know this as I'm getting a response which tells me I'm not sending it the correct query!

SocketWrench is giving me the conduit I need and now I think the issue is some sort of encoding problem :)

Many for your help. Job done here.
I will post encoding issue ion another thread as it's not relevant here.

Cheers and thanks again :)
 
Seems as though socketwrench is built for full-blown Visual Basic. Can it be used with .VBS scripting? Anyone have any examples?

I need to establish UDP connection through about 6 boxes, and listen at the end. While still recieving packets, send an 'UP' message every hour or so, but if 5 minutes pass without hearing from the sender, execute another script.

I've no idea where to start......and of course I've only time to spend, not money.

Code:
'On 'front' end
'send UDP packet to host
x=1
Do while x=1
????? 
sleep 10000
loop

Code:
'on 'Back' end
set incoming = true
Do while incoming = true
  'Listen for incoming packet,set incoming=false if not heard
  ???????
  count = count +1
  if count = 60
     'send 'all good' mail
     Blat strGoodMessage
     count = 0
   End if
loop

'Execute script when UDP drops

Set objShell = CreateObject("WScript.Shell")
objShell.Run(NoUDP.vbs)
 
bbh1,

Did you ever get this to work correctly? If so, can you post your vbs code? I'm working with the Dimac dll now, but can't seem to get it working right. Thought I'd ask you first.

Thanks,

CoachHog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top