Edit Wi‑Fi Configuration Files Using sed and C Wrapper Scripts
This guide shows how to modify non‑standard configuration files such as wpa_supplicant.conf by using the sed command‑line tool and wrapping the commands in a C program to automate Wi‑Fi SSID and password updates.
Using sed to modify Wi‑Fi configuration
Example wpa_supplicant.conf file:
r/run/wpa_supplicant
update_config=1
ap_scan=1
network={
ssid="test"
psk="12345678"
scan_ssid=1
key_mgmt=WPA-PSK
priority=1
}Replace the SSID and password with LinuxZn and 88888888 respectively:
sed -i 's/ssid="[^"]*"/ssid="LinuxZn"/g' ./wpa_supplicant.conf
sed -i 's/psk="[^"]*"/psk="88888888"/g' ./wpa_supplicant.confCalling the same substitutions from a C program
The program builds the sed commands with snprintf, resets the configuration file, and executes the commands via system():
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define SHELL_CMD_MODIFY_WIFI_SSID "sed -i 's/ssid=\"[^\"]*\"/ssid=\"%s\"/g' ./wpa_supplicant.conf"
#define SHELL_CMD_MODIFY_WIFI_PSW "sed -i 's/psk=\"[^\"]*\"/psk=\"%s\"/g' ./wpa_supplicant.conf"
#define SHELL_CMD_RESET_WIFI_CONF "cp default_wpa_supplicant.conf wpa_supplicant.conf"
int main(void) {
char cmd[256] = {0};
/* Reset configuration to a known state */
system(SHELL_CMD_RESET_WIFI_CONF);
/* Set SSID */
snprintf(cmd, sizeof(cmd), SHELL_CMD_MODIFY_WIFI_SSID, "LinuxZn");
system(cmd);
memset(cmd, 0, sizeof(cmd));
/* Set password */
snprintf(cmd, sizeof(cmd), SHELL_CMD_MODIFY_WIFI_PSW, "88888888");
system(cmd);
return 0;
}Running either the shell commands directly or the compiled program updates wpa_supplicant.conf with the new SSID and password. Resetting the file first ensures the original formatting is preserved.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
