36 lines
1.6 KiB
Python
36 lines
1.6 KiB
Python
# chassis_intru_scanner
|
|
# Author: Willy
|
|
import subprocess
|
|
import time
|
|
import argparse
|
|
|
|
# Phrase args
|
|
parser = argparse.ArgumentParser(description='This minimal script will poll the status of the chassis intrusion of a physical server using the "impi sdr list" command. If the chassis is found to be intruded upon, it will trigger a user-defined command or script. Using Systemd is recommended.')
|
|
parser.add_argument('--command', '-cmd', required=True, help='Path to your command or script. Example: "/home/user/myscript.sh"')
|
|
parser.add_argument('--start-delay', '-sd', type=float, required=True, help='Delay before initially starting the script in seconds. Set to 0 for no delay.')
|
|
parser.add_argument('--rescan-delay', '-rd', type=float, required=True, help='Delay after the chassis intrusion is triggered in seconds. Use this to prevent your command or script from being spammed. Set it to 0 for no delay.')
|
|
args = parser.parse_args()
|
|
|
|
# Vars
|
|
command = args.command
|
|
delay = args.start_delay
|
|
rescan_delay = args.rescan_delay
|
|
|
|
# Delay to start script
|
|
print(f"Start Delay: {delay} Seconds")
|
|
print(f"Rescan Delay: {rescan_delay} Seconds")
|
|
time.sleep(delay)
|
|
print("Started!")
|
|
|
|
# Script
|
|
while True:
|
|
output = subprocess.check_output('ipmitool sdr list &> /dev/null | grep "Chassis Intru" &> /dev/null', shell=True).decode()
|
|
|
|
for line in output.splitlines():
|
|
if 'Chassis Intru' in line:
|
|
status = line.split('|')[1].strip()
|
|
if status == '0x01':
|
|
print(f"{status} CHASSIS INTRUSION DETECTED! Running Command...")
|
|
subprocess.run(command, shell=True)
|
|
time.sleep(rescan_delay)
|