Help with stripping a message to what I want

Yes, they do.

But the flow/code has a text node after this to show the time - all things being good.

And it was/did.

But when I stuck the machine on the new network that didn't have access to NTP, it gives the message in the text node but it is kind of corrupt.

So that you just said then may be good enough as
if(msg.payload.contains('no server suitable for synchronization found')) { ...
I can send a "There is not NTP" message and that's good enough to tell me that the NTP isn't available.

The problem is with this approach, next time you update the OS, that string might change to "no server found" then your flow breaks in a hard to track problem.

The err code from the exec is more definitive.


Ok, I hadn't thought of that.

I'll see what I can do.

Again: Thanks.


Ok, using your node with 2 outputs:
Yeah, that gives the message.

All I need to do is (hang on) change it to this:

const result = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?(\d+ \w{3} \d+:\d+:\d+)/s.exec(msg.payload)
if (result && result.length == 3) {
  msg.payload = {
    ip: result[1],
    time: result[2]
  return msg;

msg.payload = "No NTP found";
return msg;

Fair enough?

1 Like

What do you think the timestamp is telling you?

I want to make sure the network/machine can get to the NTP server.

I thought I had fixed it, but haven't - which explains some other problems I am having.

So I want a bit of code that I can run and it either tells me the time or says the NTP isn't available.

From what I have now with that latest bit of code for the function node I posted I get the message saying there is no NTP found.

I don't understand your question.

You can do that with your original exec node but instead of parsing whatever text it outputs, check the return code.

[{"id":"e19cf3ec21f8e946","type":"inject","z":"142f935612b6e391","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":100,"wires":[["52b29840836eea79"]]},{"id":"52b29840836eea79","type":"exec","z":"142f935612b6e391","command":"ntpdate -q","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":310,"y":100,"wires":[[],[],["da74aeb612ae8435"]]},{"id":"da74aeb612ae8435","type":"switch","z":"142f935612b6e391","name":"","property":"payload.code","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"neq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":100,"wires":[["c55a52eb20cb4065"],["7480e1450b3a5032"]]},{"id":"c55a52eb20cb4065","type":"debug","z":"142f935612b6e391","name":"Return 0 - server OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.code","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":80,"wires":[]},{"id":"7480e1450b3a5032","type":"debug","z":"142f935612b6e391","name":"Server NOTOK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.message","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":120,"wires":[]}]

Before you posted "I [only] want to make sure ..." I was going to suggest a modified Bash command to filter out unwanted text before it gets to Node-red:

ntpdate -q 2>/dev/null | sed -e /^server.*/d -e s/\ ntpdate.*//
1 Like

That is a good idea.

Never thought of that.

I guess I was hung up on getting a time value.

I may use that now instead as that is the important part.


I wanted to know whether you were trying the get the time from the server or from the local machine. I think the time is that of the local machine, not the server (though they may well be the same of course).

Well there's a whole other story about that.

I scan things from that machine.
I want to know the DNS, IP address, DHCP server, local IP address, check if some address blocking is working and...... if NTP works.

So I got a bit hung up on getting the time from the NTP server. Which that command does, I think.
Or at least it checks if the NTP server can be accessed. Until now I thought it could, but it can't.
(That's a whole other can of worms)

So I got the code working on a network that had NTP access.
Then I moved it to the other network - thinking it had - but as it turns out it doesn't.
So I built on that rather than - as @jbudd said - simply do the command and look at the return code.
Of course that is better.
(Alas that machine is now powered down for now.)
And as I have not got this clarified, I may have to go back and rewrite some other code on another machine that checks the NTP status on my network and is part of my network.

This The machine I am using for network testing is a Pi with a touch screen to check the rules are working, or not.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.