Ssh and Raspberry Pi

I’ve searched the forum and still not quite finding what I want so I’ll put the question to the experts :slight_smile:

I’m running this -> Ambient TV lights
It works rather well by the way :slight_smile: The problem I have is this:

I run them through home assistant to turn on the TV and lights [ambient light system] but I do not want to just push the button and cold turn it off as it runs off a raspberry pi and I don’t want to just turn it off I’d like to have the pi shut down first then turn off the lights.

The question I have is how can I send the ssh command to an ip address login with user ‘pi’ and password 'raspberry ’ then send this command ‘sudo shutdown -h now’?

Any help would be really appreciated, Thank you all!!

if you activate telnet on your raspberry pi you can use this:

and use this instead in the python script

#!/usr/bin/python3
import telnetlib
import time
HOST = "192.168.1.165"
telnet = telnetlib.Telnet(HOST)
telnet.read_until(b":",3)
telnet.write(b"pi\n")
telnet.read_until(b":",3)
telnet.write(b"raspberry\n")
telnet.read_until(b"pi@raspberrypi:",10)
telnet.write(b"sudo shutdown -h now\n")
telnet.read_until(b"pi@raspberrypi:",10)
telnet.write(b"exit\n")
telnet.close()

im using Home Assistant Supervised, so im not sure if/how its working on other installations but its worth a try, good luck

1 Like

Telnet :astonished:

To answer the OP, try:
ssh pi@ip_address sudo shutdown -h now

Then give the password and the R Pi will shut down.

1 Like

can you automate that so you can run it directly from ha without having to type the password manually?
if so I’m interested too

Have you tried this?

Yes, Google "passwordless ssh", then pick your preferred web site to explain
how it is done.

Essentially, you will need to log into your HA box, generate a pair of keys
(ssh-keygen) and copy the HA public (.pub) key to your destination box. Neither
box (HA nor destination) will need a reboot.

No passwords are involved.

It is actually fairly simple, despite carrying an aura of geeky trickiness.