Command line sensor

Hi,

I’m trying to setup a command line sensor as shown in the code below. If I uncomment the second line (and comment the third line), it works as expected. When I run HA with the third line uncommented (and second commented), it doesn’t work.

- platform: command_line
  #command: 'nmap -p 5005 localhost | grep open &> /dev/null && echo success || echo fail'
  command: shell_command.sonos_check_controller
  name: 'sonos_controller_online_shell'

The “sonos_check_controller”-command reads exactly like the commented code.

sonos_check_controller: '/usr/bin/nmap -p 5005 localhost | grep open &> /dev/null && echo success || echo fail'

Has anyone struggled with this and managed to solve the issue?

Thanks,
Patrik

Work for me

sensor:
- platform: command_line
  #command: 'nmap -p 5005 localhost | grep open &> /dev/null && echo success || echo fail'
  name: Nmap test

Have you considered to use a binary sensor?

binary_sensor:
  platform: command_line
  name: Printer
  command: ping -c 1 192.168.1.10 &> /dev/null && echo success || echo fail
  payload_on: "success"
  payload_off: "fail"
1 Like

Am I incorrect or is this an example of a ping based device tracker?

It’s just an example for a binary command line sensor but yes, it checks if a system/hosts is online.

1 Like

I’ve been doing this for PCs and printers in my office with NMap and sensor templates. This approach looks a lot more reliable and with less overhead. I’m going to give this a try, @fabaff - thanks!

This is why I try to read all the new threads, even if it’s not something I think may relate to my setup. I usually end up learning something valuable!

As far as I remember is that example mentioned in the docs too. :wink:

Yes it is; at the bottom after the RASPlex example. MIssed that.

You’re correct in that I should probably switch to a binary sensor. I’ve also got a template connected to the sensor which I would probably be able to get rid of.

Anyways, I need to define which port to scan, which is the reason for using the nmap-command. I know for a fact that port 81 on 192.168.0.198 is closed (in fact, the camera I’m pointing on is powered off so the host is completely down). If I run the command in shell I get the expected result:

pi@raspberrypi:/home/hass/.homeassistant/extraconfig/sensors$ /usr/bin/nmap -p 81 198.168.0.198 | grep open &> /dev/null && echo success || echo fail
fail

In Home Assistant, the sensor reads “success”, but when I check the log it reads:

16-09-05 16:26:17 homeassistant.components.sensor.command_line: Timeout for command: /usr/bin/nmap -p 81 198.168.0.198 | grep open &> /dev/null && echo success || echo fail

Feels like there’s something very basic here that I’m not understanding? Nmap takes some time to initiate. Could that be the problem?

Thanks,
Patrik

Same issue here. Bash and PHP script work on the cli. Both Time Out in Hass.

  • platform: command_line
    name: WAN In
    command: “/usr/bin/php /home/homeassistant/.homeassistant/bandwidth.php In 16”
    unit_of_measurement: mbps
    scan_interval: 30

BASH version

A=$(/usr/bin/snmpget -v2c -c public 192.168.6.254 if$1Octets.$2 | /usr/bin/awk ‘{print $4}’)
/bin/sleep 2
B=$(/usr/bin/snmpget -v2c -c public 192.168.6.254 if$1Octets.$2 | /usr/bin/awk ‘{print $4}’)
/bin/echo $( /bin/echo “scale=2; (($B - $A) / 2) * 8 / 1024 / 1024” | /usr/bin/bc )

PHP version

<?php $router = "192.168.6.254"; $interface = $argv[2]; $time = 5; snmp_read_mib("/usr/share/snmp/mibs/ietf/IF-MIB"); $A = (int) str_replace("Counter32: ", "", snmpget($router, "public", "if{$argv[1]}Octets.$interface")); sleep($time); $B = (int) str_replace("Counter32: ", "", snmpget($router, "public", "if{$argv[1]}Octets.$interface")); if ($A > $B) { print(round((((2**32 - $A) + $B) / $time) * 8 / 1024 / 1024, 2)); } else { print(round((($B - $A) / $time) * 8 / 1024 / 1024, 2)); } ?>

Did you solve it?