Remote Program Execution in Visual Basic

So, I’m trying to write a program in Visual Basic that will execute a batch file on 5 remote PCs on a network. Does anyone know how I can do this? When I try to run the batch files now, I can access them over the network, but they execute on my PC, not the satellite PCs. I need some code that will let me access the remote PCs, either all at once or one at a time, and run the command to start the BAT on the remote PCs. Can anyone help me with this?

Well, there are several ways to do this. One is to write a VB app that will run in IIS on the target machine. The application will create a web page that you can go to to trigger the job. However, this requires setting up web servers on the machines, and you probably dont want to have 5 web servers :slight_smile:

Another way would be to write a program that uses Winsock. On the machines where you want to run the batch you set up a “server” program that opens a specific port and waits for a command. It should start with Windows. On your machine you have a “client” program that can connect to that program on that port and send it the command. You’ll probably also want a password of some sort. There’s quite a number of Winsock tutorials online.

Can you point me towards any tutorials you think are better than others?

I do not believe you can have the files execute w/o something running on the other end.

MSDN has plenty of info.

I specialize in VB 6.0 (not .NET), so what I say will mostly apply to that.

The Winsock control is probably the best way to do this. the Inet will work too, but is more geared for getting documents off the WWW. Both only come with Profesional and Enteprise Editions. I haven’t actually done this yet, but I will try to give a simple tutorial based on the subs.

On the server:

  1. Set the LocalPort and Protocal properties to what ever.
    For each client:
  2. Set the RemoteHost, RemoteHostIP, and the RemotePort properties to the client.
  3. Call Connect to connect to the remote computer; Wait for the Connect event.
  4. Call SendData to tell the client go.
  5. Wait for the SendComplete event.
  6. If there is a response in ur packet format, wait for that.
  7. Call Close

On each client:

  1. Set the LocalPort and Protocal properties to what ever.
  2. Set the RemoteHost, RemoteHostIP, and the RemotePort properties to the server.
  3. Call Listen, wait for the ConnectionRequest event, then call Accept to connect.
  4. Wait for the DataArrival event.
  5. Call GetData to get the data, then parse it (own code).
  6. If there is a response in ur packet format, call SendData to send the response.
  7. Run the file.

I don’t think Shell() runs bats, but if you add Shell32.dll to your references, you can use the Shell object to run anything. (Verbs)

Ah, but this is windows we are talking about!
Of corse you can run programs on a remote machine without installing anything on it! Thats how the blaster worm got so big! You can see if by going on any 2k box and using the command shutdown -i (-i is for interface, you can use --help if you wanna do it from the terminal).
I have a similar situation at my school, and we solved it in a couple of ways.
One of them was dameware (http://www.dameware.com/). Its not VB, but basically it lets you control lots of computers on your network.
The second way I solved the problem through VB was very simple, but probably not the most efficient. I simply wrote a program that would check a text file on the file-server ever minute or so, and if there was a 1 in it, it would ‘lock’ the computer it was on, and if it was a 0, it would ‘un lock’ the computer.
I realize this is an extremely inefficient way of doing things, but it was only took me a couple of hours to do.
I’m not sure if either of these solutions will work for you, but I thought I might as well mention them.
If you do get something working with winstock I would be interested in seeing it (if you dont mind), because I want to start playing around with it too (to write a similar applications).

Ok, you need a daemon (that’s “server” for you windows folks) listening on a TCP port, for commands to execute. Upon receiving them, it executes them.

Or, you can specialize the program, to wait on a TCP port for a textfile, keep reading until reaching a special character, save that into a .bat file, then execute…

But of course, security issues :slight_smile:

I have had to do something like this before, but I didn’t use Visual Basic.

When I had to have a program run on multiple systems, I write small batch files and install them on all the target systems. These batch files go onto the network and get a list of commands to do, and then execute them.

ex: ---------------------------
@ECHO OFF
NET USE * /d
NET USE N: \inferno\list /Y
C:
CD
COPY N:\1.txt C:\1.bat
CALL C:\1.bat
DEL C:\1.bat
REM Command Done

I would put the requisite commands into 1.txt on a server of sorts. Inferno is my server, and list is a special share where I put 1.txt.