How to get the IP address of hotspot client
cmb.py
#!/usr/bin/env python3
# Thanks to
# Rui Santos & Sara Santos - Random Nerd Tutorials
# and Anthony - stackoverflow
# for the ideas
import sys
import requests
import subprocess
import time
# Wi-Fi credentials
ssid = 'YOUR-SSID'
password = 'YOUR-PASSWORD'
# Your phone number in international format (including the + sign)
phone_number = 'YOUR-PHONE-NUMBER'
# Your callmebot API key
api_key = 'YOUR-API-KEY'
# Wi-Fi functions
def what_wifi():
process = subprocess.run(['nmcli', '-t', '-f', 'ACTIVE,SSID', 'dev', 'wifi'], stdout=subprocess.PIPE)
if process.returncode == 0:
wifis = process.stdout.decode('utf-8').strip().splitlines()
for wifi in wifis:
if "yes" in wifi:
return(wifi.split(':')[1])
else:
return ''
def is_connected_to(ssid: str):
return what_wifi() == ssid
def scan_wifi():
process = subprocess.run(['nmcli', '-t', '-f', 'SSID,SECURITY,SIGNAL', 'dev', 'wifi'], stdout=subprocess.PIPE)
if process.returncode == 0:
return process.stdout.decode('utf-8').strip().split('\n')
else:
return []
def is_wifi_available(ssid: str):
return ssid in [x.split(':')[0] for x in scan_wifi()]
def connect_to(ssid: str, password: str):
if not is_wifi_available(ssid):
# print(ssid, ": not available")
return False
subprocess.call(['nmcli', 'd', 'wifi', 'connect', ssid, 'password', password])
return is_connected_to(ssid)
def connect_to_saved(ssid: str):
if not is_wifi_available(ssid):
# print(ssid, ": not available")
return False
subprocess.call(['nmcli', 'c', 'up', ssid])
return is_connected_to(ssid)
def send_message(phone_number, api_key, message):
# Set the URL
url = f'https://api.callmebot.com/whatsapp.php?phone={phone_number}&text={message}&apikey={api_key}'
# Make the request
response = requests.get(url)
# check if it was successful
if (response.status_code == 200):
print('Success!', flush=True)
else:
print('Error: ')
print(response.text, flush=True)
def get_IP():
wifi_IP = subprocess.check_output(['hostname', '-I'], encoding="utf-8")
if wifi_IP is not None:
return(wifi_IP)
return("No IP")
try:
while True:
if is_connected_to(ssid):
print("conectado a "+ssid, flush=True)
time.sleep(180)
elif is_wifi_available(ssid):
print(ssid+" disponible", flush=True)
if connect_to_saved(ssid):
print("enviar IP", flush=True)
message = get_IP()
print(message, flush=True)
send_message(phone_number, api_key, message)
else:
print("No se ha conectado", flush=True)
time.sleep(60)
else:
time.sleep(180)
except Exception as e:
print('Error:', e)
cmb.sh
#!/bin/bash
echo "cmb starts:"
python3 cmb.py
echo $?
echo ":cmb.piExit "
cmb.service
[Unit]
Description=Run cmb.sh script on Boot with Retry
After=network.target
[Service]
WorkingDirectory=/home/pi/work
ExecStart=/home/pi/work/cmb.sh
StandardOutput=append:/home/pi/work/cmb.out
StandardError=append:/home/pi/work/cmb.err
Restart=always
User=root
[Install]
WantedBy=multi-user.target
Enable your service
1.
Create your service file. (cmb.service)
2.
Copy it to /etc/systemd/system:
sudo cp cmb.service /etc/systemd/system/
3.
Update systemd’s internal data:
sudo systemctl daemon-reload
4.
Enable your service
sudo systemctl enable cmb
5.
Start it
sudo systemctl start cmb
How to register your phone with CallMeBot
https://www.callmebot.com/blog/free-api-whatsapp-messages/