This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Executing command on remote server fails
#1
I have two LM4 (firmware 20160714) where I have a simple script that executes a command on a remote server using ssh. This has always worked but now the script fails on one of the LM4.

This is the script:
Code:
function my_telegram_remote(message)
  log(message)
key = '/lib/genohm-scada/storage/id_rsa'
user = 'root'
ip = 'X.X.X.X'
port = 22

  cmd = '/home/emm/bin/telegram_simple.lua ' .."'" ..message .."'"
  log(cmd)
run = string.format('ssh -y -i %s %s@%s/%d %q', key, user, ip, port, cmd)
  log(run)
result = io.readproc(run) 
log(result) 
 
end

When I run this script on the good LM, I can see in the logfile on the remote machine that the connection is established. When I run the script on the other LM nothing shows up in the logfile.
I can ping the remote server from this LM.
The log(result) returns nothing.

Is there a way I can debug this to find out what is going on?
Reply
#2
You should check logs on the server side, to see whether LM has connected or not. Check that the message does not contain single quotes or the command will fail. As an alternative you can use HTTP instead of SSH.
Reply
#3
(17.01.2025, 11:49)admin Wrote: You should check logs on the server side, to see whether LM has connected or not. Check that the message does not contain single quotes or the command will fail. As an alternative you can use HTTP instead of SSH.

Nothing shows in the logfile of the remote server. So it looks like it doesn't connect...

Can you give an example of doing this with http, so I could try this?
Thanks.
Reply
#4
You can run a HTTP server or even a simple TCP server that will forward messages to telegram as needed. You can use any programming language to implement this. Don't forget to run this server automatically when the system starts.
Reply
#5
(17.01.2025, 12:19)admin Wrote: You can run a HTTP server or even a simple TCP server that will forward messages to telegram as needed. You can use any programming language to implement this. Don't forget to run this server automatically when the system starts.

I'm not quite sure I understand. 
Where would I have to run this HTTP server? On the remote server that I now ssh to there runs already an http server. How do I get it to forward messages to telegram? As you can guess I am not familiar with this stuff...

I used to run the following script on this LM before telegram changed their policy (and I could not upgrade the firmware):

Code:
require 'ssl.https'

local telegram_url = 'https://api.telegram.org/mybot/sendMessage?'

local chat_id = 'XXXXX'

function telegram(message)

  local data_str = 'chat_id=' .. chat_id .. '&text=' .. message..''

     local res, code, headers, status = ssl.https.request(telegram_url, data_str)
end
But since that didn' work anymore I started using the other script to run telegram on the remote server and that worked and it still does on my other LM.
Reply
#6
You have a separate device that runs your Lua script. You can create a TCP server that forwards messages.

Server code (change token and chat_id as needed):
Code:
require('socket')
require('ssl.https')

token = 'token' -- your token
chat_id = '1234567' -- your chat id

function telegram(message)
  local url = 'https://api.telegram.org/bot' .. token .. '/sendMessage'
  local data = 'chat_id=' .. chat_id .. '&text=' .. socket.url.escape(message)

  return ssl.https.request(url, data)
end

server = socket.bind('*', 8000)

while true do
  client = server:accept()
  client:settimeout(1)

  message, err = client:receive('*a')

  if message then
    telegram(message)
  end

  client:close()
end

Client code (change ip and message as needed):
Code:
require('socket')

ip = '127.0.0.1'
message = 'hello'

client = socket.tcp()
client:settimeout(5)

res, err = client:connect(ip, 8000)

if res then
  client:send(message)
end

client:close()
Reply
#7
(20.01.2025, 12:54)admin Wrote: You have a separate device that runs your Lua script. You can create a TCP server that forwards messages.

Server code (change token and chat_id as needed):
Code:
require('socket')
require('ssl.https')

token = 'token' -- your token
chat_id = '1234567' -- your chat id

function telegram(message)
  local url = 'https://api.telegram.org/bot' .. token .. '/sendMessage'
  local data = 'chat_id=' .. chat_id .. '&text=' .. socket.url.escape(message)

  return ssl.https.request(url, data)
end

server = socket.bind('*', 8000)

while true do
  client = server:accept()
  client:settimeout(1)

  message, err = client:receive('*a')

  if message then
    telegram(message)
  end

  client:close()
end

Client code (change ip and message as needed):
Code:
require('socket')

ip = '127.0.0.1'
message = 'hello'

client = socket.tcp()
client:settimeout(5)

res, err = client:connect(ip, 8000)

if res then
  client:send(message)
end

client:close()

Thanks, I will give this a try.
I still wonder why my existing script now fails while it has been working previously. Would a reboot help?
Reply


Forum Jump: