RunAProgram

From Pickwiki
Jump to: navigation, search

HomePage>>HowTos>>RunAProgram

The problem: you want a program to run on a windows PC as a result of something that happens on your Windows or Unix-based server. It's usually less of an issue to have a Unix server run something on a Unix client, or Windows server to Windows client - there's plenty of built in ways for that to work. So we'll just consider a Windows PC with a network connection (LAN,WAN or VPN) to a Unix server.

For this to work "something", some program, has to be running on the PC.

0) TerminalEmulators

The most common "something" is a terminal emulator and they pretty much all allow you to send an escape sequence and fire off a program. If you want the program to run in response to something the user did on the server, this is the most straightforward way to do it. Also, if you have a serial connection, this is probably the only option.

  • wIntegrate:
Use the WIN.PCRUN command installed on the server, CALL WIN.PCRUN(COMMAND,ARGS)

To execute a command and return to PICK/BASIC immediately:

PRINT CHAR(27):CHAR(2):"<":command:CHAR(13)

To execute a command and wait for the command to terminate before returning to PICK/BASIC:

PRINT CHAR(27):CHAR(2):">":command:CHAR(13)

See AccuTerm help for details: From the Help Introduction, see AccuTerm Programming. Also see Help on Object Bridge.

More examples are at AccuTermFileTransfer.

?
  • Powerterm:
PRINT CHAR(27):"$tS":COMMAND:CHAR(27):"\"
  • Netterm:

Start/run the program specified by COMMAND.

CRT CHAR(27):"[]":COMMAND:CHAR(27):"[1*

Send the URL to the client's WWW browser for processing.

CRT CHAR(27):'[]':URL:CHAR(27):'[0*'

Start/run the program specified by COMMAND and wait till it terminates. COMMAND can contain both the program to run and command line arguments.

CRT CHAR(27):"[]":COMMAND:CHAR(27):"[9*"

Start the program associated with the extension contained within FILENAME.

CRT CHAR(27):"[]":FILENAME:CHAR(27):"[12*


1) Doit

Doit is a small utility program that installs a tray applet on the client. The server sends it the path of a file to open.

http://www.chiark.greenend.org.uk/~sgtatham/doit/


2) Install rcmd

Either from the windows resource kit, or purchase it from mks. You can then issue an rcmd from the unix side.


http://www.dynawell.com/support/ResKit/winnt.asp

http://www.mkssoftware.com/support/kb/faqs.asp?product=Toolkit&type=tech#art140


3) Install or turn on a telnet server

W2K/XP come with a telnet server, or you can purchase one. Then use http://expect.nist.gov to script the login/command process.

http://www.goodtechsys.com/


4) Install an SSH server

Just like Telnet, only secure!

http://tech.erdelynet.com/cygwin-sshd.html

5a) Write a small server program

Have the program listen on a predefined port. There are obvious security issues here, but with a little handwaving we can move past them :-)

Recent U2 products can create socket connections from basic and connect to the server. Sample code for this is at BasicSocketClient.

Here's a stripped down example of a server in Python:

urlserver.py

import SocketServer
import os

class UrlHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        url=self.rfile.readline(512)
        print "opening "+url
        os.system("start "+url)

server=SocketServer.TCPServer( ('', 79), UrlHandler)
server.serve_forever()

On the pc, type "python urlserver.py" to start the server up, then from another machine, "telnet pc-address 79" and then type the url to go to - the client will open up a browser and go to that address.

5b) ...and here's another in VBScript

urlserver.vbs

Option Explicit

' Heavily cribbed from the amazing 4K webserver at:
' http://www.interclasse.com/scripts/4KWebServer.php

Dim oSck
On Error Resume Next
Err.Number=0
Set oSck=WScript.CreateObject("MSWinsock.Winsock","s_")
Select Case Err.Number
    Case 0
    case &H80040112
        echo "No licence found. Install Visual Studio"
    case &H80020009
        echo "ActiveX Winsock not in the registry, check if installed or use regsvr32"
    Case else
        echo "Error " & Err.Number & " - &H" & Hex(Err.Number) & " - " & Err.Description
End Select
On Error Goto 0

oSck.LocalPort=79
oSck.Listen
echo "Listening on port " & oSck.LocalPort
Do
    ' Sleep for 2 seconds
    WScript.sleep 2000
Loop

Sub echo(S)
    WScript.Echo Now & " " & S
End Sub

Sub s_ConnectionRequest(Byval requestID)
    oSck.Close
    echo oSck.RemoteHostIP & " - Connection request:" & requestID
    oSck.Accept requestID
End Sub

Sub s_Close
    oSck.Close
    echo "closed - re-listening"
    oSck.Listen
End Sub

Sub s_[[SendComplete]]
    oSck.close
    oSck.Listen
End Sub

Sub s_Error(ByVal N, D, ByVal C, ByVal S, ByVal F, ByVal H, Y)
    echo "Error" & N & " - " & D
    oSck.Close
    oSck.Listen
End Sub

Sub s_DataArrival(Byval b)
    Dim S,sh

    S=""
    oSck.GetData S
    S=left(S,len(S)-2)
    S=left(S,512)
    echo S

    set sh = createobject("wscript.shell")
    on error resume next
    sh.run "cmd /c start " & S
    on error goto 0
    
    oSck.Close
    oSck.Listen
End Sub

On the PC, type "cscript urlserver.vbs" to start the server up, then from another machine, "telnet pc-address 79" and then type the url to go to - the client will open up a browser and go to that address.

6) Have a polling program on the client

If the client and the server share access to a directory (via samba or NFS for example), the server can drop a file in the common directory and a program running on the client can watch for new files. When one is found it can read the file and perform some action based on the contents. Here's a VB example:

Private Sub Form_Load()
    txtScheduleNum.Enabled = False
    cboCategory.Enabled = False
    cmdOK.Enabled = False
    cmdPreview.Enabled = False
    Label1.Enabled = False
    Label3.Enabled = False
    
    Me.Show
    ' Start the poll right away
    Timer1_Timer
End Sub

Private Sub [[Timer1_Timer]]()
    TxtLog.ForeColor = vbBlack
    LogMsg "Checking c:\batch"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set dir = fso.GetFolder("C:\BATCH")
     
    For Each file In dir.Files
        If LCase(Right(file.Name, 3)) = "pdf" Then
            Timer1.Enabled = False
            TxtLog.Enabled = False
            fstr = file.Name
            hpsname = Left(fstr, Len(fstr) - 3) & "hps"
            justname = Left(fstr, Len(fstr) - 3)
            LogMsg fstr
            Label1.Enabled = True
            Label3.Enabled = True
            cboCategory.Enabled = True
            cmdOK.Enabled = True
            cmdPreview.Enabled = True
            ' Enable some controls and do something with the file
            txtScheduleNum.SetFocus
            Exit For
        End If
    Next
End Sub

7) Consider using a solution created specifically to solve the problem

(Ad) The following link goes to the Nebula R&D forum where an offering is discussed and a link is provided to a blog entry for more information. As of the time of this writing the software is fully usable but is not being offered as a product simply for lack of demand. That situation can change with a compelling business case. http://nebula-rnd.com/forum/index.php/topic,51.0.html (/Ad)

A general description of this approach, not specific to the Nebula solution, would be to write a PC program in your favourite language (.Net, Java, Flex for example), that runs on the PC and makes periodic calls to a Pick program using whatever technology makes sense for your Pick variety. In the U2 world, this would be UniObjects, though a web services call that hides the connection details from the client, and be much more likely to work through firewalls, would make more sense.

Note: this is not exactly the problem we are trying to solve on this page. The problem is "how do I run a program on the client side, on demand from a Pick program?" The polling solutions presented suffer from a lag between something happening on the server and a program running on the client.

Running a program on the server from outside

Connectivity to MV is discussed many places in this wiki and any language can be used to execute BASIC business rules and return a result. A unique freeware program called MV Exec uses mv.NET to run any query, any command, any program on a MV server from any PC.

TODO: Expand on this topic with info about D3 compile to executable with G option, UDT, notes about unique jBASE exe/dll design...