Ping a port to check if device is online

Its possible when your target-port is a tcp-port. And almost all ports a running over tcp (http, ssh, ftp, smtp, …).

What you want is not a ping (it runs over ICMP and this “portless”).
But you can send a tcp “SYN” and wait for an “SYN ACK” - it should be returned immediately.
btw this is how nmap checking for open ports.

I dont know much about home assistant (Iam HA-newbie), but you can play around with nmap- and/or nc-command (netcat). If not, I can write a small bash-script in the next days.

If windows nmap works why not use nmap in linux?

Also I learned a neat trick when researching this. You can do the following:

echo > /dev/tcp/host/port

for example the machine 192.168.178.254 on my lan runs a web server on port 80, but nothing on port 81.

nick@server:~$ echo > /dev/tcp/192.168.178.254/80
nick@server:~$ echo > /dev/tcp/192.168.178.254/81
-bash: connect: Connection refused
-bash: /dev/tcp/192.168.178.254/81: Connection refused

You can script that easy enough.

Credit due: Test if telnet port is active within a shell script - Unix & Linux Stack Exchange

That would the job alright but since I’m running Hassio on a pi 4 I don’t think you can just run any linux command.
I don’t think telnet is part of it for example and neither does the /dev/tcp command above.

Have you actually tried

echo > /dev/tcp/ip-address/15000 

to your device, or are you just assuming it won’t work?

Doesn’t give any output but if I use wrong port (21080) it does say refused

core-ssh:~# echo > /dev/tcp/192.168.0.100/21081
core-ssh:~# echo > /dev/tcp/192.168.0.100/21080
-bash: connect: Connection refused
-bash: /dev/tcp/192.168.0.100/21080: Connection refused

Still not exactly sure how I can use that in my automation unless it is in a binary sensor or something similar

Please don’t post photos of your text. They have this thing called copy and paste.

Try this as a commandline sensor

# Example configuration.yaml entry
sensor:
  - platform: command_line
    name: Konnected Up
    command:  (echo > /dev/tcp/192.168.x.x/15000) > /dev/null 2>&1 && echo "true" || echo "false"

Thanks for that just tried it but looks like this::
image
I wonder if it needs extra single quotes?
I can confirm via putty I get true which means it does work but HA sensor reports it as false
And how often does this check? Just once when HA server is starting? I would need this be run every hour

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.