# Help with stripping a message to what I want

**URL:** <https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993>\
**Category:** General\
**Created:** [3 September 2023 06:36 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993 "2023-09-03T06:36:37Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:51 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/21 "2023-09-03T07:51:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 September 2023 07:54 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/22 "2023-09-03T07:54:11Z")

</div>

> [@Trying\_to\_learn](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:55 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/23 "2023-09-03T07:55:00Z")

</div>

> [@Trying\_to\_learn](#):
>
> if(msg.payload.contains('no server suitable for synchronization found'))

Ok, I hadn't thought of that.

I'll see what I can do.

Again: Thanks.

But......

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

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 September 2023 08:33 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/24 "2023-09-03T08:33:29Z")

</div>

What do you think the timestamp is telling you?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 08:36 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/25 "2023-09-03T08:36:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 September 2023 08:59 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/26 "2023-09-03T08:59:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> I want to make sure the network/machine can get to the NTP server.

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

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbd3fe9e6dd9c1424d726b029ea54d4f9935ded7.jpeg)

```auto
[{"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 192.168.17.30","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:

```auto
ntpdate -q 192.168.17.39 2>/dev/null | sed -e /^server.*/d -e s/\ ntpdate.*//

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 09:03 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/27 "2023-09-03T09:03:09Z")

</div>

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.

Thanks.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 September 2023 09:22 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/28 "2023-09-03T09:22:25Z")

</div>

> [@Trying\_to\_learn](#):
>
> I don't understand your question.

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).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 09:28 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/29 "2023-09-03T09:28:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 September 2023 09:28 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/30 "2023-09-17T09:28:26Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993.md?page=1)
