Skip to content

Instantly share code, notes, and snippets.

@faisalnjs
Last active December 9, 2025 06:53
Show Gist options
  • Select an option

  • Save faisalnjs/534f7c9d5a343412e9d06108336a70b9 to your computer and use it in GitHub Desktop.

Select an option

Save faisalnjs/534f7c9d5a343412e9d06108336a70b9 to your computer and use it in GitHub Desktop.
Reconnect Raspberry Pi to headless Wi-Fi network using flags, useful if the device does not automatically connect on boot.
#!/bin/bash
echo "[-] Running reconnect-pi.sh@faisaln..."
while getopts ":n:u:p:c" opt; do
case $opt in
n)
network_name="$OPTARG"
;;
u)
network_username="$OPTARG"
;;
p)
network_password="$OPTARG"
;;
c)
ca_cert_path="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo "[1] Validating current network..."
if ping -c 1 0 &> /dev/null; then
echo "[1] Connected to $(iwgetid -r || echo 'ethernet')."
if tailscale status &> /dev/null; then
echo "[2] Tailscale is already running. Skipping Tailscale setup..."
if ! iwgetid -r || iwgetid -r | grep -q "Faisal N"; then
echo "[3] Setting up headless network..."
if [[ -z "$network_name" ]]; then
while true; do
read -p "[3] Network name: " network_name
if [[ -n "$network_name" ]]; then
break
else
echo "[3] Network name required."
fi
done
fi
found=false
if command -v nmcli &> /dev/null; then
nmcli device wifi rescan >/dev/null 2>&1 || true
if nmcli -t -f SSID device wifi list | grep -xFq "$network_name"; then
found=true
fi
fi
if [ "$found" = false ]; then
if sudo iwlist wlan0 scan | grep -qw "$network_name"; then
found=true
fi
fi
if $found; then
echo "[3] $network_name network found. Creating..."
existing_connections=$(nmcli -t -f NAME connection show | grep -w "$network_name")
if [ -n "$existing_connections" ]; then
echo "[3] Existing connection for $network_name found. Attempting to connect..."
sudo nmcli connection up "$network_name" ifname wlan0
sleep 10
if nmcli -t -f NAME connection show --active | grep -qw "$network_name"; then
echo "[3] Connected to network $network_name."
exit 0
else
echo "[3] Removing existing connections for $network_name..."
echo "$existing_connections" | while IFS= read -r conn; do
sudo nmcli connection delete "$conn"
echo "[3] Deleted connection: $conn"
done
fi
fi
if [[ -z "$network_password" ]]; then
while true; do
read -sp "[3] Enter password for $network_name: " network_password
if [[ -n "$network_password" ]]; then
break
else
echo "[3] Network password required."
fi
done
fi
if [[ "$network_name" == *"wpa"* ]]; then
echo "[3] WPA network found."
if [[ -z "$network_username" ]]; then
while true; do
read -p "[3] Enter username for $network_name: " network_username
if [[ -n "$network_username" ]]; then
break
else
echo "[3] Network username required."
fi
done
fi
if [[ -n "$ca_cert_path" ]]; then
if [[ ! -f "$ca_cert_path" ]]; then
echo "[3] CA certificate file not found at $ca_cert_path. Exiting."
exit 1
fi
sudo bash -c 'cat <<EOF > /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="'"$network_name"'"
key_mgmt=WPA-EAP
eap=PEAP
identity="'"$network_username"'"
password="'"$network_password"'"
phase1="peaplabel=1"
phase2="auth=MSCHAPV2"
ca_cert="/etc/wpa_supplicant/ca-cert.pem"
}
EOF'
else
sudo bash -c 'cat <<EOF > /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="'"$network_name"'"
key_mgmt=WPA-EAP
eap=PEAP
identity="'"$network_username"'"
password="'"$network_password"'"
phase1="peaplabel=1"
phase2="auth=MSCHAPV2"
ca_cert="/etc/wpa_supplicant/empty-ca.pem"
}
EOF'
sudo touch /etc/wpa_supplicant/empty-ca.pem
fi
else
sudo bash -c 'cat <<EOF > /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="'"$network_name"'"
psk="'"$network_password"'"
}x
EOF'
fi
if [[ "$network_name" == *"wpa"* ]]; then
sudo nmcli connection add type wifi ifname wlan0 \
con-name "$network_name" ssid "$network_name" \
802-11-wireless.mode infrastructure \
802-11-wireless-security.key-mgmt WPA-EAP \
802-1x.eap peap \
802-1x.identity "$network_username" \
802-1x.password "$network_password" \
802-1x.phase2-auth mschapv2 \
$ca_opt \
connection.autoconnect yes
else
sudo nmcli connection add type wifi ifname wlan0 con-name $network_name ssid "$network_name" 802-11-wireless.mode infrastructure
sudo nmcli connection modify $network_name \
802-1x.identity "$network_username" \
802-1x.password "$network_password" \
802-11-wireless-security.psk "$network_password"
fi
sudo nmcli connection modify $network_name connection.autoconnect yes
echo "[3] Connecting to $network_name..."
sudo nmcli connection up $network_name ifname wlan0
sleep 10
if nmcli -t -f NAME connection show --active | grep -qw "$network_name"; then
echo "[3] Connected to network $network_name."
else
echo "[3] Failed to connect to network $network_name. Please check your credentials and try again."
exit 1
fi
else
echo "[3] $network_name network not found. Please ensure the network is available."
exit 1
fi
else
echo "[3] Already connected to headless network, skipping network setup..."
fi
fi
echo "[+] reconnect-pi.sh completed."
exit 0
else
echo "[1] No internet connection found. Connect to the internet and try again."
exit 1
fi
echo "[-] reconnect-pi.sh aborted."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment