-
Notifications
You must be signed in to change notification settings - Fork 13
/
wificonnect.cpp
55 lines (43 loc) · 1.05 KB
/
wificonnect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
// variables
std::string password;
std::string ssid;
int main(int argc, const char *argv[]){
// Get ssid and password from arguments
for (int i = 1; i < argc; i++){
if(strcmp(argv[i], "--ssid") == 0){
printf(argv[i + 1]);
ssid = argv[i + 1];
}
if(strcmp(argv[i], "--password") == 0){
password = argv[i + 1];
}
}
// check and make sure ssid is valid
if (ssid.length() < 1) return -1;
// open wpa_supplicant
std::ofstream wpaFile;
wpaFile.open("/etc/wpa_supplicant/wpa_supplicant.conf", std::ofstream::app);
if (wpaFile.is_open()){
printf("\nOpened!");
// Write new ssid and psk to file
wpaFile << "\n";
wpaFile << "# WIFIGUI SETUP \n";
wpaFile << "network={\n";
wpaFile << " ssid=\"" << ssid << "\"\n";
wpaFile << " psk=\"" << password << "\"\n";
wpaFile << "}\n";
wpaFile << "# END SETUP";
wpaFile.close();
// Force wlan0 reset
system("sudo ifup --force wlan0");
return 1;
}
else{
return -2;
}
}