Skip to content

pbiswell/lockwhengone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LockWhenGone

A script that automatically locks, shuts down, reboots, or runs a script on your computer when your phone, or other devices, become undetectable. More details in the Purpose section.

Found this useful? Please consider starring this repo, or donating at the sponsorship links in the sidebar, or by clicking button below. Thank you.

ko-fi

Table of Contents

Purpose

The intended purpose of this script is to check every X seconds whether your phone(s) or other devices are detectable, and if they aren't, then lock, shutdown, reboot, or run a script on the computer automatically. I initially made this so that if I leave home, my computer will lock itself. It checks for devices that are on the local network, connected by Bluetooth, or by USB. It can also ping IPs or load URLs. Devices are split into 3 groups, ones which should never be connected, ones which must always be connected, and a group of devices where at least 1 must always be connected(configurable).

I only use Linux, so this is only currently planned to run on Linux. I will test and add more commands compatible with other Linux distributions when I distro hop.

Installation Guide

This guide assumes you already know how to input commands into the terminal, and know where your home directory is.

Requirements

By default, this script uses lsusb (for USB devices), bluetoothctl (for Bluetooth devices), ping (for ip addresses), curl (for URLs), and arp-scan (for the local network). Some or all of these may be installed on your system already.

Note: arp-scan requires root. If you can't or don't want to run the script as root, then the next best option is ping.

You can install them with your preferred package manager, for example with Ubuntu:

# Remove packages that you don't need
sudo apt install usbutils bluez iputils-ping curl arp-scan

Repos:

usbutils: https://github.com/gregkh/usbutils

bluez: https://github.com/bluez/bluez

iputils-ping: https://github.com/iputils/iputils

curl: https://github.com/curl/curl

arp-scan: https://github.com/royhills/arp-scan

Download the files

Make a new directory, then download the files to it. The commands below create lockwhengone directory in your home directory.

mkdir ~/lockwhengone
curl -L http://github.com/pbiswell/lockwhengone/archive/main.tar.gz | tar zxf - -C ~/lockwhengone --strip-components=1
cd ~/lockwhengone

Edit Device Files

You can add devices to the device list files some-required.txt, all-required.txt, or not-allowed.txt, depending on how important the devices are.

How to get your device information is detailed in the Add Your Devices section.

All devices must be on a new line, and start with the device type followed by a colon (eg. usb:XXXX:XXXX). Comment lines (lines starting with #) are ignored.

some-required.txt

Add devices to this list when only some must be found, by default 1. (You can change MIN_DEVICES in config.conf)

If the number of found devices is below the minimum required amount, and the list is not empty, the exit action will be triggered.

For example, you have 2 phones but don't always have both with you. Or a device which may sometimes be connected by Bluetooth, or sometimes be connected to the local network.

all-required.txt

Only add devices to this list which must always be found. If any device listed in this file is not findable, the exit action will be triggered.

For example, a mobile phone which is always with you, or a USB security key which you always disconnect when you leave.

not-allowed.txt

Only add devices to this list which should never be found. If any device listed in this file is findable, the exit action will be triggered.

For example, a designated device that only connects in order to trigger the exit action.

Add Your devices

You must add your chosen devices to device files listed in the Edit Device Files section.

Adding USB Devices

Find your connected USB devices by running command:

lsusb

Example output:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 046d:c514 Logitech, Inc. Cordless Mouse

If you decide to add the 046d:c514 Logitech, Inc. Cordless Mouse from the example output, you can add any of these lines to your chosen device file:

usb:046d:c514
# or
usb:046d:c514 Logitech, Inc. Cordless Mouse
# or
usb:Logitech, Inc. Cordless Mouse

Adding Bluetooth Devices

Connect your Bluetooth device to your computer, then run the command:

bluetoothctl devices Connected

Example output:

$ bluetoothctl devices Connected
Device E7:FC:C6:D5:5E:F6 My Phone
Device CA:EC:2F:67:FE:6D John's cowboy hat

If you decide to add the E7:FC:C6:D5:5E:F6 My Phone from the example output, you can add any of these lines to your chosen device file:

bluetooth:E7:FC:C6:D5:5E:F6
# or
bluetooth:E7:FC:C6:D5:5E:F6 My Phone
# or
bluetooth:My Phone

Note: The Bluetooth device must be connected to your computer by Bluetooth to be detected. The device just being nearby is unfortunately not enough.

Adding ping addresses

You can add IP addresses or domains to ping. Non-local addresses may take a long time to respond.

You could use ping:google.com as a sanity check, assuming Google never goes down.

If you want to ping 192.168.1.1, for example, you would add this line to your chosen device file:

ping:192.168.1.1

Note: Some devices can and do block pings, for any reason.

Adding website addresses

By default, this checks that a URL returns a 200, 301, or 302 HTTP status code, which indicates success or redirection.

You can check a URL with this command:

curl -si https://www.google.com | head -n 1

Example output:

$ curl -si https://www.google.com | head -n 1
HTTP/2 200

You can use this to check if a website is up, or webpage exists. You could use this to remotely lock or shutdown your computer, by making a specific webpage unavailable on a website you control. However, this could fail if the computer's internet connection goes down, which may also be what you want.

If you wanted to check, for example, that https://paulbiswell.co.uk was returning a 200 HTTP success code, you would add this line to your chosen device file:

url:https://paulbiswell.co.uk

Adding network devices (required root)

Run this command to find your network devices:

# Change to your local network
sudo arp-scan 192.168.1.0/24

Example output:

$ sudo arp-scan 192.168.1.0/24
Starting arp-scan 1.10.0 with 256 hosts (https://github.com/royhills/arp-scan)
192.168.1.1	5b:f2:c8:fa:aa:a4	(Unknown)
192.168.1.2	b1:bd:57:8c:4b:e5	(Unknown)
192.168.1.3	1d:7b:ee:bd:bb:75	(Unknown)

If you decide to add 192.168.1.1 5b:f2:c8:fa:aa:a4 from the example output, you can add this line to your chosen device file:

network:5b:f2:c8:fa:aa:a4

Run the script

Once you have entered your devices in the device list file(s), you can run the script with this command:

./lockwhengone.sh

Example output:

$ ./lockwhengone.sh
Success: Completed all checks without triggering exit action.
Sleeping for 30 seconds before running again. Press CTRL+C to exit script.

Command for verbose output:

./lockwhengone.sh -v

CLI Options

Help

Displays help information

./lockwhengone.sh --help

Version

Displays the version number

./lockwhengone.sh --version

Verbose Output

Show verbose/detailed output

./lockwhengone.sh --verbose

Custom Config

Use a custom config file

./lockwhengone.sh -c [FILE]
# For example:
./lockwhengone.sh -c "~/myconfig.conf"

Disable Coloured Output

Disable the coloured output

./lockwhengone.sh -d

Config

Exit Action (EXIT_ACTION)

The action that is triggered based on devices found/not found.

Name: EXIT_ACTION

Default value: lock

Options: lock shutdown reboot script debug

lock: Locks the computer (Default)

shutdown: Shuts down the computer

reboot: Reboots the computer

script: Runs the script in EXIT_SCRIPT

debug: Only outputs a trigger message

Exit Script (EXIT_SCRIPT)

The script to run when EXIT_ACTION=script

Name: EXIT_SCRIPT

Default value: (no value)

Change this to the location of the script file you want to run.

Shutdown Command (SHUTDOWN_COMMAND)

The command to run when EXIT_ACTION=shutdown

Name: SHUTDOWN_COMMAND

Default value: "shutdown -h now"

Change this to your favourite shutdown command.

Reboot Command (REBOOT_COMMAND)

The command to run when EXIT_ACTION=reboot

Name: REBOOT_COMMAND

Default value: "reboot"

Change this to your favourite reboot command.

Lock Command (LOCK_COMMAND)

The command to run when EXIT_ACTION=lock

Name: LOCK_COMMAND

Default value: "xdg-screensaver lock"

Change this to the desktop lock command (may change depending on distro).

Minimum Devices (MIN_DEVICES)

The mimimum amount of devices to be found in some-required.txt

Name: MIN_DEVICES

Default value: 1

Note: If some-required.txt has no devices listed, this is ignored.

USB Scan (USB_SCAN)

The command to run when searching for connected USB devices.

Name: USB_SCAN

Default value: "lsusb"

Network Scan (NETWORK_SCAN)

The command to run when searching for network devices.

Name: NETWORK_SCAN

Default value: "arp-scan 192.168.1.0/24"

Note: The default command requires root permissions.

Bluetooth Scan (BLUETOOTH_SCAN)

The command to run when searching for connected Bluetooth devices.

Name: BLUETOOTH_SCAN

Default value: "bluetoothctl devices Connected"

Pinging IPs or Domains (PING_SCAN)

The command to run when pinging IPs or domains.

Name: PING_SCAN

Default value: "ping -c 1 "

Note: The default command only pings 1 time.

URL Checking (WEBSITE_SCAN)

The command to run when checking web page response codes.

Name: WEBSITE_SCAN

Default value: "curl -si "

To Do

  • Add retries for ping: and url:
  • More detailed documentation
  • Add devices from command line

Changelog

  • v0.0.1

    Initial release

About

A script that automatically locks your computer when you're away, or when your phone or other device is undetectable

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages