Using a Tunnel to Take Over a Port
An alternative to the SEND / SENDHEX commands is to establish a tunnel between an SCOM port and an external port. In this configuration, all data sent into the SCOM will be output on the external port and all data on the external port will be sent out the SCOM.
Below is a simple example, which sets up an echo on COM2. For a more extensive example of taking over a port, see the intercept.lua script within the sample scripts folder of the development kit.
SocketLib = require("socket")
-- Use SCOM1 for commands and logs
local SocketSCOM1 = SocketLib.udp()
-- Use SCOM2 for the tunnel
local SocketSCOM2 = SocketLib.udp()
-- Setup the sockets
TargetIP = "127.0.0.1"
assert(SocketSCOM1:setsockname("*",0))
assert(SocketSCOM1:setpeername(TargetIP,scom.GetSCOMPort(1)))
assert(SocketSCOM1:settimeout(3))
assert(SocketSCOM2:setsockname("*",0))
assert(SocketSCOM2:setpeername(TargetIP,scom.GetSCOMPort(2)))
-- No time out on SCOM2
-- Create function to send a command and wait for a prompt
-- Returns the prompt on success, nil on failure
function WaitForPrompt(SocketSCOM_)
while true do
local Buffer = SocketSCOM_:receive()
if Buffer == nil then
print("Timed out")
return nil
end
local Start,Stop,Prompt = Buffer:find("(%[SCOM%d%])")
if Prompt ~= nil then
print("Prompt Received: ",Prompt)
return Prompt
end
end
return nil
end
-- Send a one-byte packet to SCOM2 so that it knows the IP address of the machine
-- running the Lua script
SocketSCOM2:send("\r")
-- Setup the tunnel on the SCOM2 side
SocketSCOM1:send("interfacemode scom2 tcom2 none\r")
assert(WaitForPrompt(SocketSCOM1))
-- Setup the tunnel on the COM2 side
SocketSCOM1:send("interfacemode com2 tscom2 none\r")
assert(WaitForPrompt(SocketSCOM1))
SocketLib.sleep(1)
-- Setup an echo loop
-- This will have the effect that if the user enters characters
-- on COM2, they will be echoed back
while true do
-- Receive characters from SCOM2
local Buffer = SocketSCOM2:receive(1)
print ("Buffer: ",Buffer)
-- Echo those characters back to SCOM2
SocketSCOM2:send(Buffer)
end
SCOM and Connectionless UDP
The UDP communication used on the SCOM ports is connectionless, which means that the SCOM side does not know the IP address of the Lua interpreter until the Lua interpreter has sent a byte to the SCOM. Therefore, no data will be received on an SCOM until a byte has been sent to it.
That's why in the example above, a one-byte packet is sent to SCOM2 before attempting to receive on the socket.