Monday, January 22, 2018

Using plink.exe and SSH to run a remote CL command on IBM i


Overview
If you want to connect to an IBMi system via SSH and plink.exe, run CL system commands and possibly get message responses from the call, the process can get interesting because of the system command and program call syntax causing issues. As it turns out it's not all that hard to format a command line with parameters for running via plink. 

H
ere’s what you can do if you can’t write a CL command wrapper and want to use plink.exe to run IBMi programs or commands:

Original - Problem - This one passes the numeric parameters as numeric values, but we want them to be character. The command interpreter thinks they are numeric parameters
plink -ssh "user@1.1.1.1" -pw pass1 /bin/system 'CALL PGM(LIB/PLINKTESTC) PARM("999   " "222   ")'

Fixed with special characters (,), / and ’ all escaped with a backslash \ and with double quotes around the entire command.
plink -ssh "user@1.1.1.1" -pw pass1 /bin/system “CALL PGM\(LIB\/PLINKTESTC\) PARM\(\’999\’  \’222\\)”

An easier option might be to build a CL command wrapper

Sample CL command wrapper which handles all padding and conversion of data to proper typesplink -ssh "user@1.1.1.1" -pw pass1 /bin/system 'RJSDEVHS/PLINKTEST PARM1(999) PARM2(222)'


Providing Feedback from plink.exe
You can feedback CL message responses via SNDPGMMSG if you plan to send back any CL responses from plink.exe to be consumed by the caller.

Sample IBMi Code

PLINKTESTC - CL Program
 PGM        PARM(&PARM1 &PARM2)                         
                                                        
 DCL        VAR(&PARM1) TYPE(*CHAR) LEN(10)             
 DCL        VAR(&PARM2) TYPE(*CHAR) LEN(10)             
                                                        
 SNDPGMMSG  MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA('PARM1' +
              |> &PARM1)                                
 SNDPGMMSG  MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA('PARM2' +
              |> &PARM2)                                
                                                        
 ENDPGM
                                                 

PLINKTEST - CL Command Wrapper
CMD        PROMPT('plink test')                        
PARM       KWD(PARM1) TYPE(*CHAR) LEN(10) PROMPT(PARM1)
PARM       KWD(PARM2) TYPE(*CHAR) LEN(10) PROMPT(PARM2)

No comments: