pgm dcl &MsgId *char 7 dcl &RmtSys *char 256 value( 'CCCAMCV' ) dcl &RmtVrtDsp *char 10 value( 'OPERACAO' ) telnet rmtsys( &RmtSys ) rmtvrtdsp( &RmtVrtDsp ) dountil ( &MsgId *eq ' ' ) rcvmsg pgmq( *EXT ) msgtype( *DIAG ) msgid( &MsgId ) if ( &MsgId *eq 'CPF8902' ) do telnet rmtsys( &RmtSys ) leave enddo enddo return endpgm
Change the value for &RmtSys when you want a different system. Change the value of &RmtVrtDsp when you want a different device.
You should be able to compare messages in your joblog to the parms for RCVMSG to learn some of what's happening. Run TELNET from a command line a couple times and look at the message details for all messages it sends.
However, the code is not complete. It doesn't account for what may happen if you run it multiple times in the same job, especially if other TELNET commands have been run; and it doesn't do a good job of managing the messages in the *EXT message queue.
Part of the problem is that the information it uses comes from the job's *EXT message queue. Messages can get placed there by anything running in the job.
Another part is that you need to look at *DIAG (diagnostic) messages. Those are received in first-in/last-out sequence. The first *DIAG messages in the *EXT message queue may have been placed there hours (or days!) before, depending on how long the job has run.
Much more sophistication is possible, but everything has to start from somewhere.
Why not try telnet rmtsys(clientsys) without the remote display it will use the *DFT parameter and if remote system is setup to use Virtual devices it will create a QPADEVXXXX device in the QINTER Subsystem