Ping a port to check if device is online

The commandline sensor documentation tells you how often it updates.

Yes it may need quoting or something like that.

It is not working properly for me either.

Should we try nmap? What was the nmap command you were using?

I used this command in the windows version:

nmap 192.168.0.100 -p 21081
Starting Nmap 7.80 ( https://nmap.org ) at 2019-12-02 08:17 GMT Standard Time
Nmap scan report for 192.168.0.100
Host is up (0.079s latency).

PORT      STATE SERVICE
21081/tcp open  unknown
MAC Address: AB:CD:12:34:56:78 (Espressif)

Nmap done: 1 IP address (1 host up) scanned in 0.69 seconds

Thanks for your help on this by the way :slight_smile:

It seems that the internal HA parser for the command line sensor uses redirection also, and you can’t nest it. So the solution is to instance another bash.

  - platform: command_line
    name: Plex Service
    command: /bin/bash -c "(echo > /dev/tcp/192.168.10.16/32400) > /dev/null 2>&1 && echo Up || echo Down"
    value_template: '{{ value }}'
2 Likes

Hello !

I just tried your code with a Smart TV to tell if it is powered on or off. It works fine when it’s turned on (the sensor state returns “Up”), but it returns “Unknown” when the TV is powered off. I tried it within the terminal in HA and it returns “Down” correctly. What could that be ?

Thanks for your help !!

Never mind, just figured this out. The command was taking longer than the default timeout value, therefore it was not able to return a good state. Just increased the command timeout and it now works fine:

command_timeout: 150

Got this working! Thanks for the novel (to me):
command: /bin/bash -c "(echo > /dev/tcp/HOST/PORT) > /dev/null 2>&1 && echo ON || echo OFF"

In case anyone else runs into the same problem as I did, HOST either needs to be an IP address OR FQDN (i.e. HOST.DOMAIN). In my case it was confusing because at the HA command line HOST only worked, but not when used as above as a sensor. But I eventually figured out that HOST.local does work.

1 Like

this is something I have been tring to work out - on and off for some time, but when I put the following code into my configuration file.

command_line:
- sensor:
- platform: command_line
name: splunk_ping
command: (echo > /dev/tcp/192.168.1.39/8000) > /dev/null 2>&1 && echo “true” || echo “false”

I get the folllowing error[quote=“chrischambers, post:28, topic:151859, full:true”]
this is something I have been tring to work out - on and off for some time, but when I put the following code into my configuration file.

command_line:
- sensor:
- platform: command_line
name: splunk_ping
command: (echo > /dev/tcp/192.168.1.39/8000) > /dev/null 2>&1 && echo “true” || echo “false”

I get the folllowing error

what am I missing ? as I would then like to create triggers and automation off the results
[/quote]

what am I missing ? as I would then like to create triggers and automation off the results

Impossible to tell if you don’t post your code properly.

You could use the ping integration.

I have looked at the ping integration, but it looks like it only pings an IP address and not a port.

and sorry I am confused I through I posted the code OK, can you please give me a example and I will repost the right way

That is what ping means.

Ping operates by means of Internet Control Message Protocol (ICMP) packets. Pinging involves sending an ICMP echo request to the target host and waiting for an ICMP echo reply . The program reports errors, packet loss, and a statistical summary of the results, typically including the minimum, maximum, the mean round-trip times, and standard deviation of the mean.

No ports involved.