Program to shutdown and power on Homelab servers via ssh
This program is intended to be used on a Raspberry Pi as it takes use of its GPIO pins
If you would like to skip the manual setup steps then copy and paste this command in the terminal:
It will install the program to the /home/pi
directory. It will also install dependencies and a cron job to run the program automatically upon boot. If the install script runs properly you should see the LCD display Shutdown: A
on line one, and Power On: B
on line two after a few minutes.
Before the program may be run you must first intall required depencies. To install them run these commands:
sudo apt update
sudo apt install git
sudo apt install python3
sudo apt install python3-pip
sudo apt install rpi.gpio
sudo pip3 install adafruit-circuitpython-charlcd
To clone the git hub repository run this command:
Create a cron job. On the Rasperry Pi this is simple.
- As the PI user run this command in the terminal
crontab -e
. This will allow you to add a schedualed task. - Append this to the crontab file
@reboot sleep 5 && cd /{path_to_HomelabShutdown}/HomelabShutdown/ && python3 ./main.py
. This command will run the program upon reboot of the Raspberry Pi.
On most menus, the LCD will request a keypad input from you. Here are your options after you have typed your input:
- Press the
#
key to enter your input - Press the
*
Key to reset your input
On the main menu after boot up, when prompted whether to run the Shudown or Power on program you have an additional option:
- Enter
D
to shutdown the PI.
On the Shutdown Program, when prompeted for the password, you have an additional option:
- Enter
DDD
to exit back to the main menu.
This program uses entries on a CSV file to gather information needed for logging, remote conections, and to run remote commands.
Headers for the CSV file are:
Machine_Name, IPAddress, RemoteUser, Shutdown Command
Entries are required to be in this order or the program will not run properly
Putting Shutdown Commands in CSV file:
- Commands must use the full command path
- For example:
/bin/ls
rather thanls
- You can find the path by running:
whereis {command}
The default shutdown CSV file is shutdown.csv
however this may be changed in mainShutdown.py
Most operations are recorded to the log in logs.txt
This log records the time and date the program was run
Additionally, the log will record how long from program start an entry was made
If your Shutdown Commands do not work the first place to look is in the log
In order to connect to a remote machine and run a shutdown command you will need to perform these steps
- Create a new user on the remote machine to connect to and run the shutdown command
- Make an SSH key pair for the machine this program is running on
- Copy your public SSH key to the remote machine user that you are connecting to
- On the remote machine give the remote user sudo privilages to run your shutdown command without a password
You must use SSH keys or the program will not be able to connect to the remote machine. You can create a new SSH Key by running this command on most linux distrobutions
ssh-keygen -a 100 -t ed25519
If you are unsure of how to do work with ssh keys then this is a good video.
It is highly recommended that you create a new user who's only permision is to run your shutdown command
If you want to perform a shutdown command on the local machine just use the loopback address 127.0.0.1
in the IP address section of the csv file. An example of this can be found in the test.csv file.
If you want to actually shutdown or reboot the local machine make sure to add a delay of at least one minute before shutdown so that the program has time to close out.
All local machine commands should be placed at the end of the csv file.
Just like a remote machine you need to place your public ssh key in the authorized_keys
file of the local machine.
When attempting to run a shutdown using the keypad, a pin will be required. By default the pin is 1234
.
To change this you will have to edit the authentification.py
file.
- Look for a variable named
pin
- The default entry for this line is
pin='1234'
This program uses entries on a CSV file to gather information needed for logging, pinging machines, and running power on commands.
Headers for the CSV file are:
Machine_Name, IPAddress, PowerOn Command
Entries are required to be in this order or the program will not run properly
Putting PowerOn Commands in CSV file:
- Commands must use the full command path
- For example:
/bin/ls
rather thanls
- You can find the path by running:
whereis {command}
The default PowerOn CSV file is powerOn.csv
however this may be changed in mainPowerOn.py
- PowerOn will retrive information from one row in the
powerOn.csv
file - PowerOn will ping the Remote Machine's IP address, retrived from
powerOn.csv
- If PowerOn is able to ping the IP it will move to the next row in
powerOn.csv
- If PowerOn is not able to ping the IP it will run the power on command, retrived from the
powerOn.csv
then go to the next row