SSH Integration - Connect your servers to Home Assistant

Ok, I think a better solution would be to allow your user to execute the shutdown command without password.

Open the sudoers config file:

sudo visudo

Add the following line at the end of the file:

your_username  ALL=(root) NOPASSWD: /usr/sbin/shutdown

Now you should be able to run sudo /usr/sbin/shutdown without password.

2 Likes

Going to try if this works, thanks!

The logs says:

2023-08-14 16:34:08.902 DEBUG (MainThread) [custom_components.ssh] frigatessh: /sbin/shutdown -r now => , [‘Call to Reboot failed: Interactive authentication required.’], 1

It does the same from shell. I’ll see if you got your command right.

— edit

Right!!

I now understand. This file creates the possibility to execute an elevated command without entering the password. I’ll edit the command in the integration. thanks

How do you get a web terminal?

Hi
I have installed the integration for connecting to a windows device via SSH. The integration connected well but all sensors are in unknown state except Network Status and SSH Status, that show Connected.

Additionally the Power command is disabled. I think this is on purpose as I read the documentation but I do know how to enable it.

Could you help me?

Adrian,

This is fantastic work! Thanks!

Are you able to make it available alongside regular HA integrations, as in via Submit your work | Home Assistant Developer Docs …?

Thanks
Alex

Great package ! Thanks !

I created a sensor command to view available update through apt:

- command: apt-get -s -o Debug::NoLocking=true upgrade |grep -c ^Inst
  scan_interval: 1440
  sensors:
    - type: number
      icon: mdi:download-box-outline
      name: Available updates
      key: available_updates

I also created a action command to update my packages:

- command: sudo /usr/bin/apt-get update; sudo /usr/bin/apt-get -y upgrade
  name: Packages update
  key: packages_update
  icon: mdi:arrow-down-bold-hexagon-outline

I must be missing something REALLY basic… Where do you define commands/sensors? What file and where?

Update: ok, it’s the CONFIGURATION button… duh! Hopefully this will help someone else.
image

But most of it is working now:
image

On to the next challenge…

I also have a Docker container running a voice inference server (that uses the GPU).
I can get lots of info out of the log in real time using this command

docker logs --follow 6c3f7e83827b

Now the hard part… I want to extract the variable length sentence
“Tell me a story that takes lots of GPU cycles to infer.” from the logs:

[2023-12-08 23:39:52 +0000] [99] [DEBUG] WHISPER: ASR transcript:  Tell me a story that takes lots of GPU cycles to infer.
[2023-12-08 23:39:52 +0000] [99] [DEBUG] WHISPER: Inference took 593.489 ms
[2023-12-08 23:39:52 +0000] [99] [DEBUG] WHISPER: Inference speedup: 9x

The beginning delimiter is “transcript:” and the end is [ on the next line.
I’ve tried piping the output of the ‘docker logs’ command to get the Inference speedup number with

docker logs --tail 1 6c3f7e83827b | awk '{ print substr($0, length($0)-2) }'

But, it doesn’t seem like that command allows piping. I also tried sending it to a file with

docker logs --tail 1 6c3f7e83827b > file.out

but that file was empty.
If I can find the right command to extract the info from the last 3 lines in the log and isolate the info when running the command, I can create sensors that show the values. Heres an example of the command that isolates the GPU Memory used:

nvidia-smi --query-gpu=memory.used --format=csv,noheader | awk '{ print substr( $0, 1, length($0)-4 ) }

So, my linux skills are lacking and I’m at an impasse… for now :sleepy:

Whew. brutal. “Docker log” doesnt output to stdout, so you have to jump thru hoops…

- command: >-
    docker logs  --tail 3 6c3f7e83827b 2>&1 |awk 'sub(/.*script\: */,""){f=1}
    f{if ( sub(/ *\[.*/,"") ) f=0; print}'
  scan_interval: 5
  timeout: 2
  sensors:
    - type: text
      maximum: 200
      name: STT Result
      key: STT_result
      icon: mdi:chart-line
- command: >-
    docker logs  --tail 2 6c3f7e83827b 2>&1 |grep -oP '(?<=took).*(?=ms)'| awk
    '{$1=$1};1'
  scan_interval: 5
  sensors:
    - type: number
      unit_of_measurement: ms
      name: STT Infer time
      key: STT_time
      icon: mdi:chart-line
- command: >-
    docker logs  --tail 1 6c3f7e83827b 2>&1 |awk '{ print substr($0,
    length($0)-2) }'
  scan_interval: 5
  timeout: 2
  sensors:
    - type: text
      maximum: 200
      name: STT Infer Speed up
      key: STT_speedup
      icon: mdi:chart-line

and the final result looks like this. I’m very happy w/ this!

Hopefully this helps someone else.

I honestly don’t understand how come this integration doens’t get tons and tons of stars in HACS. It is by far the best way to push SSH commands imo!

Hi,
This is my 2 cents on nvidia gpu and some other sensors. I use docker with nvidia toolkit.

- command: nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader
  scan_interval: 60
  sensors:
    - type: number
      name: GPU Temperature
      key: temperature
      separator: ","
      unit_of_measurement: °C
- command: nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits
  scan_interval: 60
  sensors:
    - type: number
      name: GPU load
      key: gpu_load
      unit_of_measurement: "%"
- command: nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits
  scan_interval: 60
  sensors:
    - type: number
      name: GPU ram used
      key: used_memory
      unit_of_measurement: MiB
- command: nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits
  scan_interval: 60
  sensors:
    - type: number
      name: GPU ram total
      key: total_memory
      unit_of_measurement: MiB
- command: nvidia-smi --query-gpu=memory.free --format=csv,noheader,nounits
  scan_interval: 60
  sensors:
    - type: number
      name: GPU ram free
      key: free_memory
      unit_of_measurement: MiB
- command: nvidia-smi --query-gpu=fan.speed --format=csv,noheader,nounits
  scan_interval: 60
  sensors:
    - type: number
      name: GPU fan
      key: fan_speed
      unit_of_measurement: "%"
- command: nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits
  sensors:
    - type: number
      name: GPU utilization
      key: utilization_gpu
      unit_of_measurement: "%"
- command: nvidia-smi --query-gpu=utilization.memory --format=csv,noheader,nounits
  sensors:
    - type: number
      name: GPU mem utilization
      key: memory_utilization
      unit_of_measurement: "%"
- command: who --count | awk -F "=" 'NR>1 {print $2}'
  scan_interval: 60
  sensors:
    - type: number
      name: Logged in users
      icon: mdi:account-multiple
- command: cat /proc/uptime | awk '{print $1}'
  scan_interval: 180
  sensors:
    - type: number
      name: Uptime
      unit_of_measurement: d
      value_template: "{{ value | float  // 86400 }}"
      icon: mdi:server
- command: who | awk '{print $1}'
  scan_interval: 60
  sensors:
    - type: text
      name: Logged user
      icon: mdi:account
1 Like

I have added your sensors. Thanks for that! Haven’t been able to test the update button yet since there are no updates ;-). Unfortunately I cannot seem to get the sensor going. It keeps reporting Unknown. When I execute the command inside the regular SSH putty window it reports 0. Any clue? Using Bookworm debian on raspi 4.

This is honestly exceptional work. I couldn’t figure it out for the life of me how to send commands to turn off an external display (remote lovelace dashbaord connect to a pi). Your integration helped solve that in literal minutes. Now I can attach a motion sensor to turn on/off a monitor. Amazing stuff! Thanks so much

Can you share your code here. Maybe someone will find that useful.

To answer my own question, it reports unknown when there are No updates available. When there are, it’ll report a number. It would be great if it would report 0 instead of ‘unknown’, but don’t know how to do that.

Hi,
it would nice to also have the “uptime” available as a sensor.
BR
Ben

1 Like

If you add the following to the configuration, it’ll show up as a sensor:

  • command: uptime -p
    sensors:
    • type: text
      name: uptime
      key: uptime

Hey hopefully this integration is still maintained, I just moved over to this from SSH Sensor, which hasn’t been maintained for three years and stopped working with 2024.7.0.

Anyway I had to slightly rewrite one of my sensors (uptime) moving from that unmaintained sensor. Here it is, if it helps!

- command: uptime -s
  scan_interval: 60
  sensors:
    - type: text
      name: uptime
      key: uptime
      value_template: >-
        {%- set line = value.split("\r\n")[0] -%}
        {%- set lastboot = as_timestamp(strptime(line, "%Y-%m-%d  %H:%M:%S")) -%}
        {%- set now = as_timestamp(now()) -%}
        {%- set uptime = now - lastboot | int -%}
        {%- set years = uptime // 31536000 -%}
        {%- set months = (uptime % 31536000) // 2592000 -%}
        {%- set days = (uptime % 2592000) // 86400 -%}
        {%- set hours = (uptime % 86400) // 3600 -%}
        {%- set minutes = (uptime % 3600) // 60 -%}
        {{ '%dy ' % years if years else '' }}{{ '%dm ' % months if months else '' }}{{ '%dd ' % days if days else '' }}{{ '%dh ' % hours if hours else '' }}{{ '%dm ' % minutes if minutes else '' }}
1 Like

hello everyone. tell me how to integrate the nload command and get a graph of the network connection speed

Hello, I’m trying to setup this integration but I’m facing an issue. when I try with this configuration :

I get Host is offline

I have enabled debug logs but it doesn’t give me much more information:

2024-08-05 19:32:37.296 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: network_interface => None
2024-08-05 19:32:37.297 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: mac_address => None
2024-08-05 19:32:37.297 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: wake_on_lan => None
2024-08-05 19:32:37.297 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: hostname => None
2024-08-05 19:32:37.297 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: machine_type => None
2024-08-05 19:32:37.298 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: os_name => None
2024-08-05 19:32:37.298 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: os_version => None
2024-08-05 19:32:37.298 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: device_name => None
2024-08-05 19:32:37.298 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: device_model => None
2024-08-05 19:32:37.299 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: manufacturer => None
2024-08-05 19:32:37.299 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: serial_number => None
2024-08-05 19:32:37.299 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: cpu_name => None
2024-08-05 19:32:37.299 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: cpu_cores => None
2024-08-05 19:32:37.299 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: cpu_hardware => None
2024-08-05 19:32:37.300 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: cpu_model => None
2024-08-05 19:32:37.300 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: total_memory => None
2024-08-05 19:32:37.300 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: free_memory => None
2024-08-05 19:32:37.300 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: cpu_load => None
2024-08-05 19:32:37.301 DEBUG (SyncWorker_54) [custom_components.ssh.config_flow] 192.168.1.202: processes => None
2024-08-05 19:32:37.304 WARNING (MainThread) [custom_components.ssh.config_flow] Host is offline

I know that the host (192.168.1.202) is not down and that it is reachable from homeassistant because I can ssh to it manually from a terminal when I’m using Home Assistant Community Add-on: SSH & Web Terminal.

Any ideas what the issue could be ?